State pattern java

design-patterns, java, state-pattern

Solution

Before addressing your question, I prefer reviewing the idea of the pattern and propose you some little modifications in your code.

State pattern allows an object to alter its behavior when its internal state changes.

In your case, Idle and Washing are good candidates as states and WashingMachine a good candidate to bear the state object.

However, three remarks :

1) Methods provided by the states should be some actions which implementations differ according to under which state the object is.

In your declaration :

public interface WashingMachineState {    
   public void openLid();
   public void closeLid();
   public void start();
   public void stop();
   public void washing();
  } 

`washing()` is not a action but a state. It is the `start()` action that changes the state from idle to washing.

In the state pattern, the object with a state is named the context. In your case, the context is `WashingMachine`.

2) in the state pattern, the idea is that the context wants to perform some actions which the behavior changes according to the current state. To achieve that, the context delegates its processings to its current state instance. It avoid having many if - else if in the context (for each processing), and it allows also to reduce the complexity of the context because when you use the state pattern, you get families of behaviors : :

behaviors when we are in the idle state are located in the `IdleState` class.

behaviors when we are in the washing state are located in `WashingState` class.

and so for...

To perform the actions, state instances need the context (`WashingMachine`). To address this question, you have two ways of doing :

Either storing the `WashingMachine` object as a field in the state instance or passing this as an argument when the context `WashingMachine` object delegates to the state the processing.

I propose you to use the stateless way. So, when the `startWashing()` operation is called on a `WashingMachine` instance, the `WashingMachine` instance should delegate the processing to `state.startWashing()` by passing itself such as `state.startWashing(this)`. The state should provide as parameter a `WashingMachine` :

public interface WashingMachineState {    
   void openLid(WashingMachine machine);
   void closeLid(WashingMachine machine);
   void pushStartBtn(WashingMachine machine);
   void pushStopBtn(WashingMachine machine);
} 

3) Actually you defined two states : idle and washing. These should be completed with a `stopping` state because some operations on the machine (opening the door, pushing the start btn for example...) have a specific behavior when the machine is in the "is stopping" state. Note that with only two states, you may also wonder if the pattern is relevant.

Now, I can answer to your question.

I want to know when switching between state between idle to washing the implementation there can be two ways which is saw over net

1.`WashingMachine` class implements State interface and switch state from Idle to washing or vice versa based on some condition

2.Idle and Washing class has `WashingMachine` as member variable.

`WashingMachine` and `WashingMachineState`s are collaborating but different things. So they have to not rely on the same interface. Adding `WashingMachine` object as fields of state subclasses is a possibility. As explained, you can also pass the `WashingMachine` as parameter of the State methods.

Note that it is not directly the `WashingMachine` that performs the switch from a state to another one. This is performed by the state. And states should invoke `WashingMachine.changeState()` to perform it.

The `WashingMachine` could be :

public class WashingMachine {

   private WashingMachineState state;

   public WashingMachine() {
     this.state =  new Idle();
   }        

   protected void changeState(WashingMachineState state) {
     this.state = state;      
   }

   public void openLid(){
     state.openLid(this);
   } 

   public void closeLid(){
     state.closeLid(this);         
   } 
   public void pushStartBtn(){
     state.pushStartBtn(this);
   } 

   public void pushStopBtn(){
     state.pushStopBtn(this);
   } 

   public State getState() {
      return state;
   }

 }

Explanations about modifications on `WashingMachine` :

`changeState` is more meaningful as `setState` when using state pattern.

`changeState(State)` could use the `protected` modifier to decrease the visibility of this method and of course state subclasses should be in the same package than `WashingMachine`. It is a implementation detail enabled in Java. With other OOP languages, you have other alternatives of course.

About switching from idle to washing, I think that it should be possible only in the `IdleState` state as `pushStartBtn()` is invoked.

Here is an example :

public class IdleState implements State {    
   public void openLid(WashingMachine machine){
       ...
   }
   public void closeLid(WashingMachine machine){
       ...
   }
   public void pushStartBtn(WashingMachine machine){
      //do processing with machine to begin effectively the washing
         ...
      machine.changeState(new WashingState());
   }

   public void pushStopBtn(WashingMachine machine){
       ...
   }
 } 

Problem

I am learning design pattern in java I was doing through some of links.I am trying to design a washing machine by state pattern I have a query regarding the implementation of state design pattern ``` public interface State { public void openLid(); public void closeLid(); public void start(); public void stop(); public void washing(); } public class Idle implements State{ //implementing overidden methods ....... } public class Washing implements State { //implementing overidden methods ....... } public class WashingMachine { State state; public WashingMachine(State state) { this.state = new Idle(); } public State getState() { return state; } public void setState(State state) { this.state = state; } } ``` I want to know when switching between state between idle to washing the implementation there can be two ways which is saw over net 1.WashingMachine class implements State interface and switch state from Idle to washing or vice versa based on some condition 2.Idle and Washing class has WashingMachine as member variable. Please any one can suggest I am a bit confused about the implementation part.

Original source