samedi 29 octobre 2016

State pattern java

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.

Aucun commentaire:

Enregistrer un commentaire