State design pattern falls under the category of Behavioural patterns. Assume that we have an object and its behavior is largely dependent on the state of its internal variables.
State pattern allows, object to behave in different ways depends on the internal state. State pattern is defined in the book Gang of four is,
Allows an object to alter its behaviour when its internal state changes. The object will appear to change its class.
Consider the following example,
Person.java
1 | package tips.pattern.state; |
4 | private String character; |
6 | public Person(String name, String character) { |
8 | this .character = character; |
11 | public void giveMeMoney() { |
12 | if (character.equals( "GoodCharacter" )) { |
13 | System.out.println( "Yes, take all my money" ); |
14 | } else if (character.equals( "BadCharacter" )) { |
15 | System.out.println( "No, I dont have anything with me" ); |
16 | } else if (character.equals( "OtherCharacter" )) { |
17 | System.out.println( "I will give the money tmrw" ); |
We have modeled a Person
object passing his name and a string representing his character, which may be a Good Character or Bad Character or some Other Character. If someone comes to him and asks for money by calling thePerson.giveMeMoney()
method, his behavior is entirely dependant on his character. Technically, his behavior is tightly coupled with the internal variable character.
(
Image Credit )
The above design has one great dis-advantage. Suppose we want to add further characters that represent some other different behavior of a
Person
, then we end up in adding if-else clauses, which certainly is not very good from a design perspective because of changes being done in the existing code.
So, how to provide support for different characteristic behaviors for a person without making code changes? Here comes theState pattern which just do that. The State pattern mandates to model behavior as interfaces rather than representing them as Strings or some other types. For example, since character is the changing behavior for a Person
, let us encapsulate it by having something like the following,
Character.java
1 | package tips.pattern.state; |
3 | public interface Character { |
4 | public void giveMeMoney(); |
See, we have modeled a person’s Character as an interface. Now, let us see the implementation for different characteristic feature of a person, say GoodCharacter and BadCharacter.
GoodCharacter.java
1 | package tips.pattern.state; |
3 | public class GoodCharacter implements Character { |
5 | public void giveMeMoney() { |
6 | System.out.println( "Yes, take all my money" ); |
BadCharacter.java
1 | package tips.pattern.state; |
3 | public class BadCharacter implements Character { |
5 | public void giveMeMoney() { |
6 | System.out.println( "No, I dont have anything with me" ); |
Now, let us see the new Person
class which accepts character in the form of an interface rather than as a String. Given below is the code snippet for the same.
Person2.java
1 | package tips.pattern.state; |
5 | private Character character; |
7 | public Person2(String name, Character character) { |
9 | this .character = character; |
12 | public void giveMeMoney() { |
13 | character.giveMeMoney(); |
16 | public static void main(String[] args) { |
17 | Person2 object = new Person2( "John" , new GoodCharacter()); |
19 | object = new Person2( "John" , new BadCharacter()); |
Since now the character is represented as an interface, there wont be any more code changes if there is a need to add new characteristic behavior. All we need to do is to define the new character class implementing the Character
interface.
The following two tabs change content below.
3 comments:
kobe byrant shoes
mlb jerseys
michael kors outlet store
cheap true religion jeans
oakley sunglasses
nike huaraches
links of london
cheap mlb jerseys
adidas nmd
michael kors handbags clearance
cheap jordan shoes
kobe shoes
michael kors handbags
yeezy boost
nike huarache
michael kors handbags sale
michael kors handbags
nike kobe sneakers
nike huarache
air jordan retro
tiffany and co jewellery
ralph lauren online
http://www.uggoutlet.uk
www0626
canada goose outlet
longchamp bags
barbour jackets
san francisco 49ers jerseys
pandora charms
indianapolis colts jerseys
air jordan shoes
oakley sunglasses
pandora charms
givenchy handbags
yeezy boost 350
michael kors purses
vans shoes
retro jordans
christian louboutin shoes
supreme new york
golden goose sneakers
nike air max 270
michael kors outlet
off white jordan 1
Post a Comment