Design Patterns : Elements of Reusable Object-Oriented Software


Design Patterns: Elements of Reusable Object-Oriented Software



Download 4,06 Mb.
Pdf ko'rish
bet224/288
Sana07.04.2022
Hajmi4,06 Mb.
#535140
1   ...   220   221   222   223   224   225   226   227   ...   288
Bog'liq
GOF Design Patterns

Design Patterns: Elements of Reusable Object-Oriented Software 
333 
reflect achange in their subject. For example, if an operation 
involveschanges to several interdependent subjects, you might have 
toensure that their observers are notified only after 
all
thesubjects have 
been modified to avoid notifying observers more thanonce. 
ChangeManager has three responsibilities: 
1.
It maps a subject to its observers and provides an interface 
tomaintain this mapping. This eliminates the need for subjects to 
maintainreferences to their observers and vice versa. 
2.
It defines a particular update strategy. 
3.
It updates all dependent observers at the request of a subject. 
The following diagram depicts a simple ChangeManager-based implementation 
ofthe Observer pattern. There are two specialized 
ChangeManagers.SimpleChangeManager is naive in that it always updates all 
observers ofeach subject. In contrast, DAGChangeManager handles 
directed-acyclicgraphs of dependencies between subjects and their 
observers. ADAGChangeManager is preferable to a SimpleChangeManager when 
an observerobserves more than one subject. In that case, a change in two 
or moresubjects might cause redundant updates. The DAGChangeManager 
ensuresthe observer receives just one update. SimpleChangeManager isfine 
when multiple updates aren't an issue. 


Design Patterns: Elements of Reusable Object-Oriented Software 
334 
ChangeManager is an instance of the Mediator (305) pattern. In general there 
is only one ChangeManager, and it is knownglobally. The Singleton (144) 
pattern would beuseful here. 
9.
Combining the Subject and Observer classes.
Class libraries written in 
languages that lack multiple inheritance(like Smalltalk) generally don't 
define separate Subject and Observerclasses but combine their interfaces 
in one class. That lets youdefine an object that acts as both a subject 
and an observer withoutmultiple inheritance. In Smalltalk, for example, 
the Subject andObserver interfaces are defined in the root class Object, 
making themavailable to all classes. 
Sample Code 
An abstract class defines the Observer interface: 
class Subject; 
class Observer { 
public: 
virtual ~ Observer(); 
virtual void Update(Subject* theChangedSubject) = 0; 
protected: 
Observer(); 
}; 
This implementation supports multiple subjects for each observer. Thesubject 
passed to the Update operation lets the observerdetermine which subject changed 
when it observes more than one. 
Similarly, an abstract class defines the Subject interface: 
class Subject { 
public: 
virtual ~Subject(); 
virtual void Attach(Observer*); 
virtual void Detach(Observer*); 
virtual void Notify(); 
protected: 
Subject(); 
private: 
List *_observers; 
}; 



Download 4,06 Mb.

Do'stlaringiz bilan baham:
1   ...   220   221   222   223   224   225   226   227   ...   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