Design Patterns : Elements of Reusable Object-Oriented Software


Design Patterns: Elements of Reusable Object-Oriented Software



Download 4,06 Mb.
Pdf ko'rish
bet96/288
Sana07.04.2022
Hajmi4,06 Mb.
#535140
1   ...   92   93   94   95   96   97   98   99   ...   288
Bog'liq
GOF Design Patterns

Design Patterns: Elements of Reusable Object-Oriented Software 
128 
it to 0. The accessor returns the product. But first it checks to make sure 
the product exists, and if it doesn't, the accessor creates it. This 
technique is sometimes called 
lazy initialization
. The following code shows 
a typical implementation: 
class Creator { 
public: 
Product* GetProduct(); 
protected: 
virtual Product* CreateProduct(); 
private: 
Product* _product; 
}; 
Product* Creator::GetProduct () { 
if (_product == 0) { 
_product = CreateProduct(); 

return _product; 

4.
Using templates to avoid subclassing.
As we've mentioned, another potential 
problem with factory methods is that they might force you to subclass just 
to create the appropriate Product objects. Another way to get around this 
in C++ is to provide a template subclass of Creator that's parameterized 
by the Product class:
class Creator { 
public: 
virtual Product* CreateProduct() = 0; 
}; 
template  
class StandardCreator: public Creator { 
public: 
virtual Product* CreateProduct(); 
}; 
template  
Product* StandardCreator::CreateProduct () { 
return new TheProduct; 



Design Patterns: Elements of Reusable Object-Oriented Software 
129 
With this template, the client supplies just the product class

no 
subclassing of Creator is required.
class MyProduct : public Product { 
public: 
MyProduct(); 
// ... 
}; 
StandardCreator myCreator; 
5.
Naming conventions.
It's good practice to use naming conventions that make 
it clear you're using factory methods. For example, the MacApp Macintosh 
application framework [App89] always declares the abstract operation that 
defines the factory method as Class* DoMakeClass(), where Class is the 
Product class. 
Sample Code 
The function CreateMaze builds and returns a maze. One problem with this function 
is that it hard-codes the classes of maze, rooms, doors, and walls. We'll introduce 
factory methods to let subclasses choose these components. 
First we'll define factory methods in MazeGame for creating the maze, room, wall, 
and door objects: 
class MazeGame { 
public: 
Maze* CreateMaze(); 
// factory methods: 
virtual Maze* MakeMaze() const 
{ return new Maze; } 
virtual Room* MakeRoom(int n) const 
{ return new Room(n); } 
virtual Wall* MakeWall() const 
{ return new Wall; } 
virtual Door* MakeDoor(Room* r1, Room* r2) const 
{ return new Door(r1, r2); } 
}; 



Download 4,06 Mb.

Do'stlaringiz bilan baham:
1   ...   92   93   94   95   96   97   98   99   ...   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