In State pattern a class behavior changes based on its state. This type of design pattern comes under behavior pattern.
In State pattern, we create objects which represent various states and a context object whose behavior varies as its state object changes.
State.java
State.java
package SMD6;
public interface State {
public void doAction(Patient cureness);
}
Patient.java
package SMD6;
public class Patient {
private State state;
public Patient() {
state = null;
}
public State getState() {
return state;
}
public void setState(State state) {
this.state = state;
}
}
Cured.java
package SMD6;
public class Cured implements State {
public void doAction(Patient cureness) {
System.out.println("CuredState \n Patient is cured and he paying Bill");
cureness.setState(this);
}
}
NotCured.java
package SMD6;
public class NotCured implements State{
public void doAction(Patient cureness) {
System.out.println("NotCured State \n Patient is not cured and he is going under diagnosed again");
cureness.setState(this);
}
}
StateMain.java
package SMD6;
public class StateMain {
public static void main(String[] args) {
Patient cureness = new Patient();
Cured cured = new Cured();
cured.doAction(cureness);
NotCured notcured = new NotCured();
notcured.doAction(cureness);
}
}
Comments
Post a Comment