<< Chapter < Page
  Design patterns   Page 1 / 1
Chapter >> Page >
The State Design Pattern models objects that changes state, i.e. change behavior as a result of what has happened to them. This is also called "dynamic reclassification".

Objects are often discussed in terms of having a "state" that describes their exact conditions in a given time, based upon the values of their properties. The particular values of the properties affect the object's behavior. For instance, one can say that the exact behavior of an object's getColor() method is different if the "color" property of the given object is set to "blue" instead of "red" because getColor() returns a different value in the two situations.

Furthermore, the object may make decisions at run time as to exactly what to do dependent upon the values its properties possess. For instance, if the sky is blue (sky.setColor(Color.blue)) , then the sun should be visible.

public boolean sunIsVisible() { if(getColor()==Color.blue) {return true; }else { return false;} }

One issue with the above solution is that it is a hard-coded logic solution, not an architected solution. The sky does not intrinsically behave a certain way if it is blue, but rather it should figure out what to do in that situation.

Wouldn't it be better if the sky intrinsically acted properly if it were blue? One could imagine two objects: a SkyBlue and a SkyNonBlue . The SkyBlue class' sunIsVisible() method would always return true while the SkyNonBlue version would always return false.

What one needs now is the ability for a sky object to dynamically (i.e. at run time) change its class to/from SkyBlue and SkyNonBlue . What we'd like to accomplish is called " dynamic reclassification ".

We've seen code that does change its specific behavior depending on what particular strategy was installed. So, the setColor() method could install a strategy that would always return true if its sunIsVisible() method were to be called.

But does the user of the Sky class care about the stratregy?

Of course not. The user only cares that it does its job.

The state design pattern is a fully encapsulated, self-modifying strategy design pattern.

UML Class Diagram of the State Design Pattern

One design pattern that is used very often in conjunction with the state pattern is the Null Object Pattern .

Notice these things about the pattern:

  1. Any methods whose behaviors depend on the state of the object are simply delegated on in to the state, and handled there. Thus you will see the same methods in the context as in the states. Since the states are separate objects from the context, all the properties of the context need to have accessor methods that are at least package visible.
  2. The " Context " object needs to add a " set " accessor method so the states can modify which state is the active state. This method would be package visible so as to encapsulate the behavior away from the sight of the user.

Questions & Answers

high Dublin divino through Higginbotham
Shubham Reply
How does exchange rate affect the demand and supply?
austine
1.it allows producers to learn and invent 2.it allows agent to take advantage of economic scale 3.it allow agents to focus in areas of
oyebola Reply
can someone explain the motive of money to me?
Alemogho Reply
ask the question you don't know
ojo
opportunity cost
Sylvo
the motive of money is to pay for goods or services. it's also for transaction purposes bet it ordinary or unforseen
Silke
Money helps to reduce our scarcity problems. It can satisfy your wants to an extent
Okwori
How dose economics use science methods to explains it problems
Edmond Reply
according to Jhon Robinson the main problem of economics is what to produce? how to produce? whom to produce? economics tries to solve these problem scientifically by using its theories which are scientific in nature,basically micro and macro theories which just explain what the problem is...
Lavkesh
and now the solving techinique is also changed. only theories or its matematical +statistical version is not sufficient so the economist had developed Econometrics which includes economic theory+mathematical economics+statistics, for solving economic problem. This method treats economics as a pure
Lavkesh
..pure science so this technique is based on pure scientific methods.
Lavkesh
Economics is the scientific study of human behavior so there for science is used to solve economics problems using economics theories
Florence
economic is not higly subject of taminadu ...
Mutha
measurement of elasticity of demand
John Reply
Price elasticity of demand= Percentage change in quantity demanded/ Percentage change in price
Jale
For example, suppose that a 10 percent increase in the price of an ice-cream cone causes the amount of ice cream you buy to fall by 20 percent. We calculate your elasticity of demand as
Jale
Price elasticity of demand =20 percent /10 percent = 2
Jale
In this example, the elasticity is 2, reflecting that the change in the quantity demanded is proportionately twice as large as the change in the price.
Jale
what is economic
Samuel Reply
economics is a social science that studies the relationship of human behavior between ends and scare...
Lawrence
Why is economics as a subject is so important to human being?
austine
it's significant in study of managing available resources to satisfy human needs and wants
mohamed
economics is the science of wealth
Aamir
it is also said that it is the science of scarcity
Aamir
why study economic
Neo
Economic helps to meet our needs and wants nd to make choices.
Abraham
careers or jobs associated with economics
Neo
Economics is more than choices, scarcity, resources etc. It has a lot to do with psychology, social psychology. Understand motivation for consumer decisons.
TOM
okey
Neo
How does commercial banks create credit
Penda Reply
what is equilibrium price?
Stella Reply
happen when demand curve and supply curve meet
Enockz
intersection point of demand and supply curve
Sarjeel
the equilibrium price is that price at which the quantity demanded is equal to the quantity supplied... i.e the price at which what people want to buy is equal to what the sellers want to sell.
Che
what is difference between demand and quantity demand
Sarjeel
the price which is prevailing in the market ,it is the price at which the consumer is getting maximum satisfaction and the producer is getting maximum profit.
Rather
at equilibrium price demand for commodity is equal to supply of commodity..
Rather
dear sareel, quantity demand means the quantity of commodity which is demanded at a particular price e,g , your demand is for 4 oranges when price of orange is 5,, on the other hand demand means the whole quantity price relationship, quantity demand is shown on a particular point on demand curve,
Rather
sarjeel sbb, reply please...
Rather
what is elasticity of demand
Evans
what is Economics?
Ahmed
Economics can be defined as a social sciences that study human behavior as a relationship between end and scarce mean which have alternative use
Azeez
What is taxation
Azeez
economics is the study of scarcity
Suleiman
dear Suleiman how?
Enockz
economics is a subject matter that studies different economic activities as directed towards maximization of income at level of individual and maximization of social welfare at the level of society as a whole
Rather
economics studies us how to allocate scarce resources to full fill the maximum human wants as the resources have alternative uses..and r very scare to fullfill the human wants as the wants of humans are unlimited
Rather
demand is what a buyer is able and willing to buy at a specific price where by quantity demanded is amount of demand the buyer is able and willing to buy at a specific price.
daudi
thanks che..
Stella
how does commercial banks create credit
Penda
write models of Aggregate Supply?
abdi Reply
models of aggregate supply?
abdi
Then what's the law of elasticity?
austine
wat happen if money circulation failed to be controlled in any county?
Enockz Reply
the amount of money in circulation increases and cause aggregate demand to rise. Thus, hyperinflation.
Che
what's heteroscandacity
Crowther
price mechanism refers to the manner which price of goods and services affect the demand and supply of goods and services
Abaide
heteroscandacity: Is the circumstance where there is unequal variances across the observations.
Enockz
what is price mechanisms?
Che Reply
price mechanism refers to the manner which price of goods and services affect the demand and supply of goods and services
Abaide
what is demand
kabeer Reply
desire and ability to pay for a commodity is called demand.
Rather
i desire to become Prime Minister of India , is it demand ?please answer---
Rather
demand refers to the quantity demanded of a commodity that consumers are willing and able to buy at a particular price over a given period of time
Abigail
or the willingness power of a commoditythat is backed up by the ability to pay
Abigail
u r 100% right..
Rather
is ability to purchase a perticular goods at any price at a given time.
Neema
tanz Rather
Abigail
ur desire to become a prime minister of lndia is nt demand because u have nt made any efforts or willingness, it is called mere wish or lets say mere desire
Abigail
Thank you so much dear Abigail Mante for giving answer to my question..
Rather
U are welcome
Abigail
it's ok,dear..
Rather
The term ceteris paribus implies that
Oyelekan
Demand is the quantity demanded of a commodity at which a consumer is willing and able to purchase at various prices over a given period of time.
Suleman
ceteris parbus explains the law of demand,which states that at a higher price quantity demanded of a commodity is low and at a lower price quantity demanded of a commodity is high all other things being equal.
Abigail
U know right girl
Suleman
kk
Abigail
ceteris paribus is an economics terms used in situations where we considered other factors to remain unchanged
Ndeh
Thanks for the answer
Oyelekan
please how does disposable income affect demand?
Che
hell. somebody must tell me what is the meaning if cetrasparibus
Darling
plz. guys
Darling
the quantity of a commodity that a consumer ready willing and able to purchase at a particular price and in a particular period of time is known as demand
The
ceterusparibus means constant i,e,(other things do not change or other things remains constant)
Rather
wht
Bilal
right answer yawer saab
Sarjeel
things like what@Rather
Oyelekan
bilal sheikh why u has been written what
Sarjeel
e,g demand is determined by various factors like price of commodity, uses of good, expected changes in near future, nature of commodity , but when we learn price elasticity of demand , we keep other determinants of demand constant(i,e , we do not expect any change in price of good in near future,etc
Rather
i think u understand the term ceterus paribus..
Rather
bcz I can not understand
Bilal
what
Sarjeel
ok bro
Bilal
wht is other factors
Bilal
income of consumer, taste,
Sarjeel
Gold. Medlist in economics and. Lecturer of economics
HAZRAT
hello..what's are the factors affecting cross elasticity of demand.? at least five factors
Elzevery
The time it takes to get a product to market. That time limit has a name call "Just in Time."
William
thanks guys
kabeer
hey guys what is inelastic demand
Abaide
when there is no change in quantity demanded due to change in price then elasticity of demand is inelastic
Rather
Hi sucker how are you...
HAZRAT
thanks for the answer
Abaide
Any question about economics
HAZRAT
yes. what is the main cause of Heteroscedasticity
Abaide
hi
Habib
waht is elasticity of demand
Sarparah
elasticity of demand means change in quantity demanded divided by change in any of its determinent ,e,g , price ed= % change in quantity demanded/ % change in price.
Rather
elasticity of demand is the degree of responsiveness of quantity demanded of a commodity to a change in price of the commodity in question ,price of other related commodity n the consumers income
Abigail
yes
Kumako
ok
Abigail
guys what is monopoly
kabeer
A single seller in the market.
Tahir
what are the importance of economics
Eric Reply
what causes the demand curve to shift.
Stephen
other factors except price.it include test of a consumer fashen presence of substitute advatisment
Neema
factors other than price
Rather
Its helps to determine the price
Mavis
state three factors that affect demand and supply
VICENT Reply
1,price of the product 2,expectations in near future 3,uses of product
Rather
1.advertisement 2. expectation of future change in price 3.consumers income
Abigail
1.alternative products on market. 2.changes on price 3.consumer's expectations
Anusia
what is cheque
adedayo Reply
resources of exchanging of money to any where
Dinesh
it is a writen order to a bank to pay a specific sum of money to a person
Richard
it can can be described as when the demand is insufficient
Amaan Reply
Got questions? Join the online conversation and get instant answers!
QuizOver.com Reply

Get the best Algebra and trigonometry course in your pocket!





Source:  OpenStax, Design patterns. OpenStax CNX. Jun 04, 2009 Download for free at http://cnx.org/content/col10678/1.2
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Design patterns' conversation and receive update notifications?

Ask