Presentation is loading. Please wait.

Presentation is loading. Please wait.

STATE PATTERN Presented by Bharavi Mishra Bharavi Mishraise2007006.

Similar presentations


Presentation on theme: "STATE PATTERN Presented by Bharavi Mishra Bharavi Mishraise2007006."— Presentation transcript:

1 STATE PATTERN Presented by Bharavi Mishra Bharavi Mishraise2007006

2 Simple problem We have to automate a Chocolate machine. In this Chocolate machine,if any one wants to buy a chocolate, will drop a coin in machine and than push nob to get the chocolate.

3 State Diagram Out of Chocolate Has coin Chocolate Sold No coin Insert coin Eject coin Push nob Dispense Choco>0 Choco=0

4 General Class Diagram Test insertcoin(); turnnob(); ejectcoin(); choco public choco(int count) public void insertcoin() public void ejectcoin() public void turnnob() public void dispense()

5 General Solution public class choco { final static int sold_out=0; final static int sold_out=0; final static int no_quarter=1; final static int no_quarter=1; final static int has_quarter=2; final static int has_quarter=2; final static int sold=3; final static int sold=3; int state=sold_out; int state=sold_out; int count=0; int count=0; /** Creates a new instance of choco */ /** Creates a new instance of choco */ public choco(int count) { public choco(int count) { this.count=count; this.count=count; if(count>0){ if(count>0){ state=no_quarter; state=no_quarter; } }

6 Insert coin function public void insertcoin(){ if(state==has_quarter){ if(state==has_quarter){ System.out.println("u can,t insert another coin"); System.out.println("u can,t insert another coin"); } else if(state==no_quarter){ else if(state==no_quarter){ System.out.println("u inserted a coin"); System.out.println("u inserted a coin"); state = has_quarter; state = has_quarter; } else if(state==sold_out){ else if(state==sold_out){ System.out.println("u can't inserted a coin,machine is soldout"); System.out.println("u can't inserted a coin,machine is soldout"); } else if(state==sold){ else if(state==sold){ System.out.println("please wait,u have got one "); System.out.println("please wait,u have got one "); } }

7 Eject coin public void ejectcoin(){ if(state==has_quarter){ if(state==has_quarter){ System.out.println("coin returned"); System.out.println("coin returned"); state=no_quarter; state=no_quarter; } else if(state==no_quarter){ else if(state==no_quarter){ System.out.println("u have,t inserted a coin"); System.out.println("u have,t inserted a coin"); } else if(state==sold){ else if(state==sold){ System.out.println("Sorry,u have already got one"); System.out.println("Sorry,u have already got one"); } else if(state==sold_out){ else if(state==sold_out){ System.out.println("u can't eject, u have't inserted a coin"); System.out.println("u can't eject, u have't inserted a coin"); } }

8 Turn nob public void turnnob(){ if(state==has_quarter){ if(state==has_quarter){ System.out.println("u turned"); System.out.println("u turned"); state=sold; state=sold; dispense(); dispense(); } else if(state==no_quarter){ else if(state==no_quarter){ System.out.println("u turned but there is no coin"); System.out.println("u turned but there is no coin"); } else if(state==sold){ else if(state==sold){ System.out.println("turninf twice does,t get more then one"); System.out.println("turninf twice does,t get more then one"); } else if(state==sold_out){ else if(state==sold_out){ System.out.println("u turnned but no chokolate"); System.out.println("u turnned but no chokolate"); } }

9 Dispense public void dispense(){ if(state==has_quarter){ if(state==has_quarter){ System.out.println("no chocolate dispensed"); System.out.println("no chocolate dispensed"); } else if(state==no_quarter){ else if(state==no_quarter){ System.out.println("u must have to pay first"); System.out.println("u must have to pay first"); } else if(state==sold){ else if(state==sold){ System.out.println("a chocolate comes rooling out the sllot"); System.out.println("a chocolate comes rooling out the sllot"); count=count-1; count=count-1; if(count==0){ if(count==0){ System.out.println("sorry!here is no chocolate"); System.out.println("sorry!here is no chocolate"); state =sold_out; state =sold_out; }

10 Cont… else{ state=no_quarter; state=no_quarter; } } else if(state==sold_out){ else if(state==sold_out){ System.out.println("no chocolate dispensed"); System.out.println("no chocolate dispensed"); } } }

11 Test Class public class test { /** Creates a new instance of test */ /** Creates a new instance of test */ /** /** * @param args the command line arguments * @param args the command line arguments */ */ public static void main(String[] args) { public static void main(String[] args) { // TODO code application logic here // TODO code application logic here choco c1 = new choco(2); choco c1 = new choco(2); c1.insertcoin(); c1.insertcoin(); c1.turnnob(); c1.turnnob(); c1.ejectcoin(); c1.ejectcoin(); c1.insertcoin(); c1.insertcoin(); c1.turnnob(); c1.turnnob(); c1.insertcoin(); c1.insertcoin(); c1.turnnob(); c1.turnnob(); } }

12 Enhancement Out of Chocolate Has coin Chocolate Sold No coin Insert coin Eject coin Push nob Dispense Choco>0 Choco=0 Winner Push nob for winner Dispense Choco>0 Choco=0

13 Problem If we want to add one more state Winner. Than we must have to modify each and every function. That is violation of open closed principle. That is the base of oops.

14 Solution Use State Design Pattern instead of Traditional one. Use State Design Pattern instead of Traditional one.

15 Sold out Class ChocoMAchine Class public Chocomachine(int numbercho) public void ejectcoin() public void insertcoin() public void ejectcoin() public void setstate(state st) ….. ….. Test insertcoin(); turnnob(); ejectcoin(); State Interface public void insertcoin(); public void insertcoin(); public void ejectcoin(); public void ejectcoin(); public void turnnob(); public void turnnob(); public void dispense(); public void dispense(); Has coin Class No coin class Sold Class

16 Enhancement Test insertcoin(); turnnob(); ejectcoin(); State Interface public void insertcoin(); public void insertcoin(); public void ejectcoin(); public void ejectcoin(); public void turnnob(); public void turnnob(); public void dispense(); public void dispense(); Has coin Class Sold Class ChocoMAchine Class public Chocomachine(int numbercho) public void ejectcoin() public void insertcoin() public void ejectcoin() public void setstate(state st) ….. ….. Sold out Class No coin class Winner

17 State interface public interface state { public void insertcoin(); public void insertcoin(); public void ejectcoin(); public void ejectcoin(); public void turnnob(); public void turnnob(); public void dispense(); public void dispense(); }

18 Has coin Class public class Hasquarter implements state { Chocomachine ch1; Chocomachine ch1; /** Creates a new instance of Noquarter */ /** Creates a new instance of Noquarter */ public Hasquarter(Chocomachine ch1) { public Hasquarter(Chocomachine ch1) { this.ch1=ch1; this.ch1=ch1; } public void insertcoin(){ public void insertcoin(){ System.out.println("u can't insrt another coin"); System.out.println("u can't insrt another coin"); } public void ejectcoin(){ public void ejectcoin(){ System.out.println("coin returned"); System.out.println("coin returned"); ch1.setstate(ch1.getnoquarter()); ch1.setstate(ch1.getnoquarter()); } public void turnnob(){ public void turnnob(){ System.out.println("u turned "); System.out.println("u turned "); ch1.setstate(ch1.getsold()); ch1.setstate(ch1.getsold()); } public void dispense(){ public void dispense(){ System.out.println("no chocolate dispense"); System.out.println("no chocolate dispense"); }}

19 No coin class public class Noquarter implements state{ Chocomachine ch1; Chocomachine ch1; /** Creates a new instance of Noquarter */ /** Creates a new instance of Noquarter */ public Noquarter(Chocomachine ch1) { public Noquarter(Chocomachine ch1) { this.ch1=ch1; this.ch1=ch1; } public void insertcoin(){ public void insertcoin(){ System.out.println("u insrted a coin"); System.out.println("u insrted a coin"); ch1.setstate(ch1.gethasquarter()); ch1.setstate(ch1.gethasquarter()); } public void ejectcoin(){ public void ejectcoin(){ System.out.println("u have't inserted a coin"); System.out.println("u have't inserted a coin"); } public void turnnob(){ public void turnnob(){ System.out.println("u turned but here is no coin"); System.out.println("u turned but here is no coin"); } public void dispense(){ public void dispense(){ System.out.println("u must have to pay first"); System.out.println("u must have to pay first"); }}

20 Sold Class public class Sold implements state{ Chocomachine ch1; Chocomachine ch1; /** Creates a new instance of Noquarter */ /** Creates a new instance of Noquarter */ public Sold(Chocomachine ch1) { public Sold(Chocomachine ch1) { this.ch1=ch1; this.ch1=ch1; } public void insertcoin(){ public void insertcoin(){ System.out.println("Please wait,u have got one already"); System.out.println("Please wait,u have got one already"); } public void ejectcoin(){ public void ejectcoin(){ System.out.println("Sorry,u have turned yhe nob"); System.out.println("Sorry,u have turned yhe nob"); }

21 Cont.. public void turnnob(){ System.out.println(" turning twice does,t get u anoyher chocolate"); System.out.println(" turning twice does,t get u anoyher chocolate"); } public void dispense(){ public void dispense(){ ch1.release(); ch1.release(); if(ch1.getcount()>0){ if(ch1.getcount()>0){ ch1.setstate(ch1.getnoquarter()); ch1.setstate(ch1.getnoquarter()); } else{ else{ System.out.println("u must have to pay first"); System.out.println("u must have to pay first"); ch1.setstate(ch1.getsoldout()) ; ch1.setstate(ch1.getsoldout()) ; } }}

22 Sold out Class public class Soldout implements state { Chocomachine ch1; Chocomachine ch1; /** Creates a new instance of Soldout */ /** Creates a new instance of Soldout */ public Soldout(Chocomachine ch1) { public Soldout(Chocomachine ch1) { this.ch1=ch1; this.ch1=ch1; } public void insertcoin(){ public void insertcoin(){ System.out.println("u can,t inseart a coin,the machine is sold out"); System.out.println("u can,t inseart a coin,the machine is sold out"); } public void ejectcoin(){ public void ejectcoin(){ System.out.println("u can,t eject,u have,t inserted a coin"); System.out.println("u can,t eject,u have,t inserted a coin"); } public void turnnob(){ public void turnnob(){ System.out.println(" u turn but there is no chocolate"); System.out.println(" u turn but there is no chocolate"); } public void dispense(){ public void dispense(){ System.out.println("no chocolate dispense"); System.out.println("no chocolate dispense"); }}

23 ChocoMAchine Class public class Chocomachine { state soldoutstate; state soldoutstate; state soldstate; state soldstate; state hascoinstate; state hascoinstate; state nocoinstate; state nocoinstate; state st = soldoutstate; state st = soldoutstate; int count=0; int count=0; /** Creates a new instance of Chocomachine */ /** Creates a new instance of Chocomachine */ public Chocomachine(int numbercho) { public Chocomachine(int numbercho) { soldoutstate =new Soldout(this) ; soldoutstate =new Soldout(this) ; soldstate = new Sold(this); soldstate = new Sold(this); hascoinstate = new Hasquarter(this); hascoinstate = new Hasquarter(this); nocoinstate = new Noquarter(this); nocoinstate = new Noquarter(this); this.count=numbercho; this.count=numbercho; if(numbercho>0){ if(numbercho>0){ st=nocoinstate; st=nocoinstate; } }

24 Cont… public void insertcoin(){ public void insertcoin(){ st.insertcoin(); st.insertcoin(); } public void ejectcoin(){ public void ejectcoin(){ st.ejectcoin(); st.ejectcoin(); } public void turnnob(){ public void turnnob(){ st.turnnob(); st.turnnob(); st.dispense(); st.dispense(); } public void setstate(state st){ public void setstate(state st){ this.st=st; this.st=st; } public void release(){ public void release(){ System.out.println("A chocolate rooling out the slot"); System.out.println("A chocolate rooling out the slot"); if(count!=0){ if(count!=0){ count=count-1; count=count-1; } }

25 Cont.. public state gethasquarter(){ st=hascoinstate; st=hascoinstate; return (st); return (st); } public state getnoquarter(){ public state getnoquarter(){ st=nocoinstate; st=nocoinstate; return(st); return(st); } public state getsoldout(){ public state getsoldout(){ st=soldoutstate; st=soldoutstate; return(st); return(st); } public int getcount(){ public int getcount(){ return count; return count; } public state getsold(){ public state getsold(){ st=soldstate; st=soldstate; return (st); return (st); }

26 Test Class public class test { /** Creates a new instance of test */ /** Creates a new instance of test */ public test() { public test() { } /** /** * @param args the command line arguments * @param args the command line arguments */ */ public static void main(String[] args) { public static void main(String[] args) { // TODO code application logic here // TODO code application logic here Chocomachine choco=new Chocomachine(2); Chocomachine choco=new Chocomachine(2); choco.insertcoin(); choco.insertcoin(); choco.turnnob(); choco.turnnob(); choco.ejectcoin(); choco.ejectcoin(); choco.insertcoin(); choco.insertcoin(); choco.ejectcoin(); choco.ejectcoin();

27 Pattern name :- State pattern. State pattern. Class :- Behavioral Pattern. Behavioral Pattern. Intent :- Allow an object to alter its behavior Allow an object to alter its behavior when its internal state changes. The when its internal state changes. The object Will appear to change its class. object Will appear to change its class.

28 Also known as :- Object for state pattern. Object for state pattern.Motivation:- 1: State pattern is useful when there is an object that can be in one of several states, with different behavior in each state. 1: State pattern is useful when there is an object that can be in one of several states, with different behavior in each state. 2:To simplify operations that have large conditional statements that depend on the object’s state. 2:To simplify operations that have large conditional statements that depend on the object’s state.

29 As: if (myself = happy) then if (myself = happy) then { eatIceCream(); eatIceCream(); …. …. } else if (myself = sad) then { goToPub(); goToPub(); …. …. } else if (myself = ecstatic) then { dohelp(); dohelp(); …. …. }

30 Applicability:- An object’s behavior depends on its state, and it must change its behavior at run-time depending on that state. An object’s behavior depends on its state, and it must change its behavior at run-time depending on that state. Operations have large, multipart conditional statements that depend on the object’s state. This state usually represented by one or more constants. The State pattern puts each branch of the conditional in a separate class. Operations have large, multipart conditional statements that depend on the object’s state. This state usually represented by one or more constants. The State pattern puts each branch of the conditional in a separate class.

31 Structure Context State ConcreatState1 ContreateState2 ConcreateState3 Handle() State->Handle() Client

32 Participant:- Context::- Context::- 1-Provide interface for client. 1-Provide interface for client. 2-Maintain an instance of a concretestate subclass 2-Maintain an instance of a concretestate subclass that define current state. that define current state. State::- State::- Defines an interface for encapsulating the behavior Defines an interface for encapsulating the behavior associated with a particular state of the context associated with a particular state of the context ConcreteStateclass::- ConcreteStateclass::- Each subclass implements behavior associated with Each subclass implements behavior associated with a state of the context a state of the context

33 Collaboration:- A context may pass itself as an argument to the A context may pass itself as an argument to the State object handling the request. This lets the State object handling the request. This lets the State object access the context if necessary. State object access the context if necessary. Context delegates state-specific requests to the current ConcreteState object. Context delegates state-specific requests to the current ConcreteState object. Context is the primary interface for clients. Clients can configure a context with State objects. Once a context is configured, its clients don’t have to deal with the State objects directly. Context is the primary interface for clients. Clients can configure a context with State objects. Once a context is configured, its clients don’t have to deal with the State objects directly. Either Context or the ConcreteState subclasses can decide which state succeeds another and under what circumstances. Either Context or the ConcreteState subclasses can decide which state succeeds another and under what circumstances.

34 Consequences It localizes state-specific behavior and partitions behavior for different states. It localizes state-specific behavior and partitions behavior for different states. It makes state transitions explicit. It makes state transitions explicit. State objects can be shared. State objects can be shared.

35 Possible Issues If there are many different states that all need to have transition functions to one another, the amount of transition code required could be massive. If there are many different states that all need to have transition functions to one another, the amount of transition code required could be massive.Switch(State) case A: … case B: … etc…

36 Known Uses and Related Patterns Popular interactive drawing programs use the State pattern to perform specific operations based on which State(tool) is selected. Popular interactive drawing programs use the State pattern to perform specific operations based on which State(tool) is selected. The Flyweight (195) pattern explains when and how State objects can be shared. The Flyweight (195) pattern explains when and how State objects can be shared. State Objects are often Singletons(127). State Objects are often Singletons(127).

37 Thanks……


Download ppt "STATE PATTERN Presented by Bharavi Mishra Bharavi Mishraise2007006."

Similar presentations


Ads by Google