Design Patterns : Elements of Reusable Object-Oriented Software


Design Patterns: Elements of Reusable Object-Oriented Software



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

Design Patterns: Elements of Reusable Object-Oriented Software 
335 
void Subject::Attach (Observer* o) { _observers->Append(o); } 
void Subject::Detach (Observer* o) { _observers->Remove(o); } 
void Subject::Notify () { 
ListIterator i(_observers); 
for (i.First(); !i.IsDone(); i.Next()) { 
i.CurrentItem()->Update(this); 


ClockTimer is a concrete subject for storing andmaintaining the time of day. It 
notifies its observers every second.ClockTimer provides the interface for 
retrieving individualtime units such as the hour, minute, and second. 
class ClockTimer : public Subject { 
public: 
ClockTimer(); 
virtual int GetHour(); 
virtual int GetMinute(); 
virtual int GetSecond(); 
void Tick(); 
}; 
The Tick operation gets called by an internal timer atregular intervals to provide 
an accurate time base. Tickupdates the ClockTimer's internal state and callsNotify 
to inform observers of the change: 
void ClockTimer::Tick () { 
// update internal time-keeping state 
// ... 
Notify(); 

Now we can define a class DigitalClock that displays thetime. It inherits its 
graphical functionality from a Widgetclass provided by a user interface toolkit. 
The Observer interface ismixed into the DigitalClock interface by inheriting 
fromObserver. 
class DigitalClock: public Widget, public Observer {
public:
DigitalClock(ClockTimer*); 
virtual ~DigitalClock(); 
virtual void Update(Subject*); 
// overrides Observer operation 


Design Patterns: Elements of Reusable Object-Oriented Software 
336 
virtual void Draw(); 
// overrides Widget operation
// defines how to draw the digital clock 
private: 
ClockTimer* _subject; 
}; 
DigitalClock::DigitalClock (ClockTimer* s) { 
_subject = s; 
_subject->Attach(this); 

DigitalClock:: DigitalClock () { 
_subject->Detach(this); 

Before the Update operation draws the clock face, it checksto make sure the 
notifying subject is the clock's subject: 
void DigitalClock::Update (Subject* theChangedSubject) { 
if (theChangedSubject == _subject) { 
Draw(); 


void DigitalClock::Draw () { 
// get the new values from the subject 
int hour = _subject->GetHour(); 
int minute = _subject->GetMinute(); 
// etc. 
// draw the digital clock 

An AnalogClock class can be defined in the same way. 
class AnalogClock : public Widget, public Observer { 
public: 
AnalogClock(ClockTimer*); 
virtual void Update(Subject*); 
virtual void Draw(); 
// ... 



Download 4,06 Mb.

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