Design Patterns : Elements of Reusable Object-Oriented Software


Design Patterns: Elements of Reusable Object-Oriented Software



Download 4,06 Mb.
Pdf ko'rish
bet231/288
Sana07.04.2022
Hajmi4,06 Mb.
#535140
1   ...   227   228   229   230   231   232   233   234   ...   288
Bog'liq
GOF Design Patterns

Design Patterns: Elements of Reusable Object-Oriented Software 
343 
Sample Code 
The following example gives the C++ code for the TCP connectionexample described 
in the Motivation section. This example is asimplified version of the TCP protocol; 
it doesn't describe thecomplete protocol or all the states of TCPconnections.
8
First, we define the class TCPConnection, which provides aninterface for 
transmitting data and handles requests to change state. 
class TCPOctetStream; 
class TCPState
class TCPConnection { 
public: 
TCPConnection(); 
void ActiveOpen(); 
void PassiveOpen(); 
void Close(); 
void Send(); 
void Acknowledge(); 
void Synchronize(); 
void ProcessOctet(TCPOctetStream*); 
private: 
friend class TCPState; 
void ChangeState(TCPState*); 
private: 
TCPState* _state; 
}; 
TCPConnection keeps an instance of the TCPStateclass in the _state member variable. 
The classTCPState duplicates the state-changing interface ofTCPConnection. Each 
TCPState operation takes aTCPConnection instance as a parameter, lettingTCPState 
access data from TCPConnection andchange the connection's state. 
class TCPState { 
public: 
virtual void Transmit(TCPConnection*, TCPOctetStream*); 
virtual void ActiveOpen(TCPConnection*); 
virtual void PassiveOpen(TCPConnection*); 
virtual void Close(TCPConnection*); 
virtual void Synchronize(TCPConnection*); 


Design Patterns: Elements of Reusable Object-Oriented Software 
344 
virtual void Acknowledge(TCPConnection*); 
virtual void Send(TCPConnection*); 
protected: 
void ChangeState(TCPConnection*, TCPState*); 
}; 
TCPConnection delegates all state-specific requests to itsTCPState instance 
_state.TCPConnection also provides an operation for changing thisvariable to a 
new TCPState. The constructor forTCPConnection initializes the object to 
theTCPClosed state (defined later). 
TCPConnection::TCPConnection () { 
_state = TCPClosed::Instance(); 

void TCPConnection::ChangeState (TCPState* s) { 
_state = s; 

void TCPConnection::ActiveOpen () { 
_state->ActiveOpen(this); 

void TCPConnection::PassiveOpen () { 
_state->PassiveOpen(this); 

void TCPConnection::Close () { 
_state->Close(this);

void TCPConnection::Acknowledge () { 
_state->Acknowledge(this); 

void TCPConnection::Synchronize () { 
_state->Synchronize(this); 

TCPState implements default behavior for all requestsdelegated to it. It can also 
change the state of aTCPConnection with the ChangeState operation.TCPState is 
declared a friend of TCPConnection togive it privileged access to this operation. 
void TCPState::Transmit (TCPConnection*, TCPOctetStream*) { } 



Download 4,06 Mb.

Do'stlaringiz bilan baham:
1   ...   227   228   229   230   231   232   233   234   ...   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