Title: Interpreter Pattern
1Interpreter 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?? ? ?????? ???????
- ???????????
- ???????? ????????? ?? ????????? ??????
- ??? ???? ?? ????? ??????? ? ??????????? ??
???????? ???? ???? ????, ??? ????????? ? ?????
??????? ????? ?? ????????? ?? ????????? ? ????
????? ?????? ?? ?????????.
23Interpreter
????????
24??????????
- ?? ?? ?????? ????????, ????? ???? ?????????? ??
??????? ????????????? ?? ????????? ???????????
???????? ?? ?????? ?????? ?????
25?????????? ??????????
- ???? ????, ?????? ????, ???? ???????, ????
????????, ??????? ?? ??????, ????????, 2005, ISBN
954-685-352-6