Question

In: Computer Science

Compare the State Pattern and the Strategy Pattern. What is the main difference? Explain their uses...

Compare the State Pattern and the Strategy Pattern. What is the main difference? Explain their uses and provide an example of where you would use the State Pattern

Solutions

Expert Solution

`Hey,

Note: Brother if you have any queries related the answer please do comment. I would be very happy to resolve all your queries.

Strategy design pattern in Java is used to encapsulate related set of algorithms to provide runtime flexibility to client. Client can choose any algorithm at runtime, without changing Context class, which uses Strategy object. Some of the popular example of Strategy pattern is writing code, which uses algorithms e.g. encryption, compression or sorting algorithm.

On the other hand, State design pattern allows an object to behave differently at different state. Since real world object often has state, and they behave differently at different state, e.g. a Vending Machine only vend items if it's in hasCoin state, it will not vend until you put the coin on it.

You can now clearly see the difference between Strategy and State pattern, there intent is different. State pattern helps object to manage state, while Strategy pattern allows client to choose different behaviour. Another difference, which is not easily visible is, who drives change in behaviour.

In case of Strategy pattern, it's client, which provides different strategy to Context, on State pattern, state transition is managed by Context or State itself. Also, if you are managing state transition in State object itself, it must hold reference of Context e.g. Vending Machine, so that it can call setState() method to change current state of Context.

On the other hand, Strategy object never held reference of Context, it's client which passes Strategy of there choice to Context.

Kindly revert for any queries

Thanks.


Related Solutions

Explain and compare the main fiscal policy response of the United State and Australia to the...
Explain and compare the main fiscal policy response of the United State and Australia to the pandemic.
Please compare the mean and the median. What is the main difference between the two? Please...
Please compare the mean and the median. What is the main difference between the two? Please construct an example of when it would be helpful to calculate the mean, and another example illustrating when it would be helpful to calculate the median. Posts should be approximately 100 words.
define diuretics and explain their main uses. what are the four main categories of fiuretics ?...
define diuretics and explain their main uses. what are the four main categories of fiuretics ? Explain the action of each type of diuretic categories. What are the common antidysrhythmic drugs and what is the action of each on the heart.
Explain the difference between a “pull” promotional strategy and a “push” promotional strategy. Under what conditions...
Explain the difference between a “pull” promotional strategy and a “push” promotional strategy. Under what conditions should each strategy be used?
What is the main difference between notes payable and bonds payable? What is the main difference...
What is the main difference between notes payable and bonds payable? What is the main difference between a bond and a share of stock? What does it mean to issue bonds at Par? Discount? Premium? What is the contract rate and the market rate for bonds? How do you compute total bond interest expense when a bond is sold at a discount? Explain your answer. How do you compute bond interest expense when a bond is sold at a premium?...
Explain the difference between a market penetration strategy and a market development strategy.
Explain the difference between a market penetration strategy and a market development strategy.
When one uses an ANOVA, explain the difference between “main effects” and “interaction effects”. Give examples...
When one uses an ANOVA, explain the difference between “main effects” and “interaction effects”. Give examples of each.
In 200 words Explain the four indicators of the balanced scorecard, and state what a strategy...
In 200 words Explain the four indicators of the balanced scorecard, and state what a strategy map is?
a)(5%) Briefly explain the difference between hardness and hardenability.b)(5%) What is the main difference between eutectic,...
a)(5%) Briefly explain the difference between hardness and hardenability.b)(5%) What is the main difference between eutectic, eutectoid and peritectic reactions? c)(10%) A cylindrical piece of 5140 steel heated to γ region and OQ. If the hardness at the surface must be ≥ 35HRC, what is the maximum allowable dia.? Explain.
What is the difference between a Need and a want? State and explain the three (3)...
What is the difference between a Need and a want? State and explain the three (3) basic economic concepts.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT