Design Patterns : Elements of Reusable Object-Oriented Software


Design Patterns: Elements of Reusable Object-Oriented Software



Download 4,06 Mb.
Pdf ko'rish
bet192/288
Sana07.04.2022
Hajmi4,06 Mb.
#535140
1   ...   188   189   190   191   192   193   194   195   ...   288
Bog'liq
GOF Design Patterns

Design Patterns: Elements of Reusable Object-Oriented Software 
284 
We give details of just the BooleanExp,VariableExp, and AndExp classes here. 
ClassesOrExp and NotExp are similar to AndExp.The Constant class represents the 
Boolean constants. 
BooleanExp defines the interface for all classes that definea Boolean expression: 
class BooleanExp { 
public: 
BooleanExp(); 
virtual ~BooleanExp(); 
virtual bool Evaluate(Context&) = 0; 
virtual BooleanExp* Replace(const char*, BooleanExp&) = 0; 
virtual BooleanExp* Copy() const = 0; 
}; 
The class Context defines a mapping from variables toBoolean values, which we 
represent with the C++ constantstrue and false. Context has thefollowing 
interface: 
class Context { 
public: 
bool Lookup(const char*) const; 
void Assign(VariableExp*, bool); 
}; 
A VariableExp represents a named variable: 
class VariableExp : public BooleanExp { 
public: 
VariableExp(const char*); 
virtual ~VariableExp(); 
virtual bool Evaluate(Context&); 
virtual BooleanExp* Replace(const char*, BooleanExp&); 
virtual BooleanExp* Copy() const; 
private: 
char* _name; 
}; 
The constructor takes the variable's name as an argument: 
VariableExp::VariableExp (const char* name) { 
_name = strdup(name); 

Evaluating a variable returns its value in the current context. 


Design Patterns: Elements of Reusable Object-Oriented Software 
285 
bool VariableExp::Evaluate (Context& aContext) { 
return aContext.Lookup(_name); 

Copying a variable returns a new VariableExp: 
BooleanExp* VariableExp::Copy () const { 
return new VariableExp(_name); 

To replace a variable with an expression, we check to see if thevariable has the 
same name as the one it is passed as an argument: 
BooleanExp* VariableExp::Replace ( const char* name, BooleanExp& exp ) { 
if (strcmp(name, _name) == 0) { 
return exp.Copy(); 
} else { 
return new VariableExp(_name); 


An AndExp represents an expression made by ANDing twoBoolean expressions together. 
class AndExp : public BooleanExp { 
public: 
AndExp(BooleanExp*, BooleanExp*); 
virtual ~ AndExp(); 
virtual bool Evaluate(Context&); 
virtual BooleanExp* Replace(const char*, BooleanExp&); 
virtual BooleanExp* Copy() const; 
private: 
BooleanExp* _operand1; 
BooleanExp* _operand2; 
}; 
AndExp::AndExp (BooleanExp* op1, BooleanExp* op2)
{ _operand1 = op1; _operand2 = op2; } 
Evaluating an AndExp evaluates its operands and returnsthe logical "and" of the 
results. 
bool AndExp::Evaluate (Context& aContext) { 
return _operand1->Evaluate(aContext) && _operand2->Evaluate(aContext); 




Download 4,06 Mb.

Do'stlaringiz bilan baham:
1   ...   188   189   190   191   192   193   194   195   ...   288




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish