Design Patterns : Elements of Reusable Object-Oriented Software


Design Patterns: Elements of Reusable Object-Oriented Software



Download 4,06 Mb.
Pdf ko'rish
bet148/288
Sana07.04.2022
Hajmi4,06 Mb.
#535140
1   ...   144   145   146   147   148   149   150   151   ...   288
Bog'liq
GOF Design Patterns

Design Patterns: Elements of Reusable Object-Oriented Software 
212 
name space for classes. Recently, however, the C++ standardization 
committee added name spaces to the language [Str94], which will let you 
expose just the public subsystem classes. 
Sample Code 
Let's take a closer look at how to put a facade on a compiler subsystem. 
The compiler subsystem defines a {BytecodeStream} class that implements a stream 
of Bytecode objects. A Bytecode object encapsulates a bytecode, which can specify 
machine instructions. The subsystem also defines a Token class for objects that 
encapsulate tokens in the programming language. 
The Scanner class takes a stream of characters and produces a stream of tokens, 
one token at a time. 
class Scanner { 
public: 
Scanner(istream&); 
virtual ~Scanner(); 
virtual Token& Scan(); 
private: 
istream& _inputStream; 
}; 
The class Parser uses a ProgramNodeBuilder to construct a parse tree from a 
Scanner's tokens. 
class Parser { 
public: 
Parser(); 
virtual ~Parser(); 
virtual void Parse(Scanner&, ProgramNodeBuilder&); 
}; 
Parser calls back on ProgramNodeBuilder to build the parse tree incrementally. 
These classes interact according to the Builder (110) pattern. 
class ProgramNodeBuilder { 


Design Patterns: Elements of Reusable Object-Oriented Software 
213 
public: 
ProgramNodeBuilder(); 
virtual ProgramNode* NewVariable( 
const char* variableName 
) const; 
virtual ProgramNode* NewAssignment( 
ProgramNode* variable, ProgramNode* expression 
) const; 
virtual ProgramNode* NewReturnStatement( 
ProgramNode* value 
) const; 
virtual ProgramNode* NewCondition( 
ProgramNode* condition
ProgramNode* truePart, ProgramNode* falsePart 
) const; 
// ... 
ProgramNode* GetRootNode(); 
private: 
ProgramNode* _node; 
}; 
The parse tree is made up of instances of ProgramNode subclasses such as 
StatementNode, ExpressionNode, and so forth. The ProgramNode hierarchy is an 
example of the Composite (183) pattern. ProgramNode defines an interface for 
manipulating the program node and its children, if any. 
class ProgramNode { 
public: 
// program node manipulation 
virtual void GetSourcePosition(int& line, int& index); 
// ... 
// child manipulation 
virtual void Add(ProgramNode*); 
virtual void Remove(ProgramNode*); 
// ... 
virtual void Traverse(CodeGenerator&); 



Download 4,06 Mb.

Do'stlaringiz bilan baham:
1   ...   144   145   146   147   148   149   150   151   ...   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