Interpreter Pattern - PowerPoint PPT Presentation

About This Presentation
Title:

Interpreter Pattern

Description:

Title: Interpreter Pattern Author: Vassil Last modified by: Hristov Created Date: 1/21/2006 7:52:17 PM Document presentation format: On-screen Show – PowerPoint PPT presentation

Number of Views:156
Avg rating:3.0/5.0
Slides: 26
Provided by: Vass73
Learn more at: https://www.devbg.org
Category:

less

Transcript and Presenter's Notes

Title: Interpreter Pattern


1
Interpreter Pattern
Design Patterns in C
www.devbg.org/patternscourse/
????? ???????
mailatvassil.info ??????????? ??????????? ?
???????? http//www.aubg.bg/
2
??????????
  • ???????? ?? ???????
  • ???????
  • ????????? ?? ???????
  • ???????? ??????????
  • ??????? ??? ????????????
  • ?? ? ?????? ???????
  • ??????????

3
???????? ?? ???????
  • ?????, ????? ?????? ???????? ?????? ?? ???????
    ????????
  • ????????? ??
  • ????, ???? ????? ?? ?????? ???? ??????
  • ??????? ?? ??????? ????????
  • ?????? ??????? ?? ????????? ??????? ?? ????????,
    ???? ?????????? ????????? ????????? ???? ???????

4
???????? ?? ???????
  • ????????????? ?? ???????? ?? ??????????? ?
    ?????????
  • ???????? ?? ????, ????? ?????? ????? ???????
    ??????. ?? ???? ???? ???????????? ???????, ?????
    ????????? ?????????? ?? ????????.

5
???????
  • ????? ?????? ??? ??????, ?? ????? ? ???????? ??
    ?? ??????? ???? ???????
  • ??????????? ?? ???????? ??? ?????????? ?? ??????
    ? ????????? ?? ?????????

6
????????? ?? ???????
  • ??????????? ?? ????? ?? ???????? ??? ?????? ??
    ???????? ???????
  • AbstractExpression
  • ????????? ???????????? ????? Interpret, ????? ?
    ??? ?? ?????? ????? ? ???????????? ???????????
    ?????.
  • TerminalExpression
  • ???????????? ?????????? ?? Interpret ??????????
    ?? ???????? ??????? ? ???????????.
  • ?????????? ? ????????? ?? ????? ????? ?? ?????
    ????? ?????? ? ???????????.
  • NonterminalExpression
  • ????? ???? ? ????????? ?? ????? ???? ??????? R
    R1 R2 ... Rn ? ???????????.
  • ??????? ????????? ?? ??? AbstractExpression ??
    ????? ?? ????????? ?? R1 ?? Rn.

7
????????? ?? ???????
  • ???????????? ?????????? Interpret ?? ?? ????????
    ??????? ? ???????????. Interpret ?????????? ??
    ??????? ?????????? ?? ???????????? ??????????????
    ????????? R1 ?? Rn.
  • Context
  • ??????? ?????????? ???????? ?? ??????????????.
  • Client
  • ????????? (??? ??????) ?????????? ???????????
    ????? ?????????????? ????????? ????????? ? ?????,
    ????????? ?? ???????????. ????????????
    ??????????? ????? ?? ??????? ?? ????????? ??
    NonterminalExpression ? TerminalExpression
    ???????.

8
????????? ?? ???????

9
????????? ?? ???????
  • ??????????? ??????? ??????? ?? ????????? ???????.
    ???? ?????????? ??????????? ??????? ? ??? ???????
  • ?????? 1 ????????? ??????
  • ?????? 2 ?????? ???????

10
???????? ??????????
  • ????????? ??????

11
???????? ??????????
  • ?????????????? ?? ????????
  • ?????? ?????????? ????, ?????? ????? ??? ????????
  • (? ??????? RegularExpression)
  • ??????? ??????????, ?????????????? ??????? ??????
  • (? ??????? LiteralExpression, AlternationExpressio
    n, SequenceExpression, ? RepetitionExpression)

12
???????? ??????????
raining (dogs cats) raining dogs raining
cats dogs raining cats and dogs It is raining
cats and dogs
13
???????? ??????????
  • ?????? 2 ?????? ???????
  • ?????????? ?? ?????????? ?? ????? ?????.
    ????????? ??
  • ?????? (AND/ OR/ NOT)
  • ??????????
  • ?????????

14
???????? ??????????

15
???????? ??????????
  • ??????????? ?? ?????????
  • ?????? BooleanExp

public abstract class BooleanExp public
BooleanExp() public abstract bool
Evaluate(Context aContext) public abstract
BooleanExp Replace(string name, BooleanExp
bExp) public abstract BooleanExp Copy()
16
???????? ??????????
  • ?????? VariableExpression

public class VariableExp BooleanExp
public VariableExp(string name)mname
name public override bool Evaluate(Context
aContext) return aContext.Lookup(this) pub
lic override BooleanExp Replace(string name,
BooleanExp bExp) if (name.Equals(mname))
return bExp.Copy() else return new
VariableExp(mname)
17
???????? ??????????

public override BooleanExp Copy() return
new VariableExp(mname) public string
mname public class Context Hashtable
values public Context() values new
Hashtable()
18
???????? ??????????

public bool Lookup(VariableExp
varExp) bool result (bool)valuesvarExp.m
name return result public void
Assign(VariableExp varExp, bool
bval) values.Add(varExp.mname,
bval)
19
????????????
  • ?????? ???????, ??????????? ???? ???????
    ?????????????

20
??????? ??? ????????????
  • ????????? ?? ???????????? ??????????? ?????
  • ?????????????? (????????) ?? ? ??????????
    ?????????. ???????? ?? ???????? ???????, ????
    ???????? ?????? ??? ????? (???????? ??????????
    ????????).
  • ?????????? ?? Interpret ??????????
  • ?????? ????? ????? ?? ???????????? Interpret
    ?????????? ???? ?? ?? ???????? ??????? Visitor.
    ??????? ??? ?? ?????? ?? ???????? ????????
    ??????????????.
  • ????????? ?? ???????? ??????? ???? ???????
    FlyWeight
  • ?????????, ????? ????????? ???????? ?????????
    ???????? ?? ????? ????? ??????, ?? ???????? ???
    ????????? ?????????? ????????? ?? ??????? ??????.

21
?? ? ?????? ???????
  • ??????????
  • ??????????? ?? ??????? ? ????????? ?????
  • ??? ???? ?? ????????? ???????, ???? ???????????
    ????? ????? ?? ???????? ??? ???????? ???????????
  • ????????? ?? ?????????
  • ????????? ?? ????????????? ?? ????? ?????? ?????,
    ????? ???????? ????????????? ?? ??????? ?
    ???????????????? ?????????? ?? ???????
  • ???????? ?? ???? ?????? ?? ?????????????? ??
    ??????
  • ?????, ???? ???????? ?? ???? ???????? ??
    ?????????????? ???????

22
?? ? ?????? ???????
  • ???????????
  • ???????? ????????? ?? ????????? ??????
  • ??? ???? ?? ????? ??????? ? ??????????? ??
    ???????? ???? ???? ????, ??? ????????? ? ?????
    ??????? ????? ?? ????????? ?? ????????? ? ????
    ????? ?????? ?? ?????????.

23
Interpreter
????????
24
??????????
  1. ?? ?? ?????? ????????, ????? ???? ?????????? ??
    ??????? ????????????? ?? ????????? ???????????
    ???????? ?? ?????? ?????? ?????

25
?????????? ??????????
  • ???? ????, ?????? ????, ???? ???????, ????
    ????????, ??????? ?? ??????, ????????, 2005, ISBN
    954-685-352-6
Write a Comment
User Comments (0)
About PowerShow.com