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 : :
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){
...
}
}