Design Patterns : Elements of Reusable Object-Oriented Software


Design Patterns: Elements of Reusable Object-Oriented Software



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

Design Patterns: Elements of Reusable Object-Oriented Software 
341 
difficult to modify and extend. The State patternoffers a better way to 
structure state-specific code. The logic thatdetermines the state 
transitions doesn't reside in monolithicif or switch statements but instead 
is partitionedbetween the State subclasses. Encapsulating each state 
transition andaction in a class elevates the idea of an execution state 
to fullobject status. That imposes structure on the code and makes itsintent 
clearer. 
2.
It makes state transitions explicit.
When an object defines its current state 
solely in terms of internaldata values, its state transitions have no 
explicit representation;they only show up as assignments to some variables. 
Introducingseparate objects for different states makes the transitions 
moreexplicit. Also, State objects can protect the Context frominconsistent 
internal states, because state transitions are atomicfrom the Context's 
perspective

they happen by rebinding 
one
variable (the Context's State 
object variable), notseveral [dCLF93]. 
3.
State objects can be shared.
If State objects have no instance variables

that 
is, the state theyrepresent is encoded entirely in their type

then contexts 
can sharea State object. When states are shared in this way, they 
areessentially flyweights (see Flyweight (218)) with nointrinsic state, 
only behavior. 
Implementation 
The State pattern raises a variety of implementation issues: 
1.
Who defines the state transitions?
The State pattern does not specify which 
participant defines thecriteria for state transitions. If the criteria are 
fixed, then theycan be implemented entirely in the Context. It is generally 
moreflexible and appropriate, however, to let the State 
subclassesthemselves specify their successor state and when to make 
thetransition. This requires adding an interface to the Context thatlets 
State objects set the Context's current state explicitly. 
Decentralizing the transition logic in this way makes it easy tomodify or 
extend the logic by defining new State subclasses. Adisadvantage of 
decentralization is that one State subclass will haveknowledge of at least 
one other, which introduces implementationdependencies between 
subclasses. 
2.
A table-based alternative.
In 
C++ Programming Style
[Car92], 
Cargilldescribes another way to impose structure on state-driven code: 
Heuses tables to map inputs to state transitions. For each state, atable 
maps every possible input to a succeeding state. In effect,this approach 



Download 4,06 Mb.

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