I'm trying to apply State pattern on certain program. But can't find out what to do with this one..
Short Description :
There's security system that works like :
To get to the "treasure" you have to pull the lever inside a room, but the door leading to the room has to be closed. Then a Control System is opened.
You can insert a key and get to the treasure, but before you do that, you have to place lever to the original position otherwise you get trapped...
The code of Class I should apply State pattern to :
public class ControlSystem {
private State state1, state2;
private boolean st1, st2;
public ControlSystem() {
state1 = new Door(true);
state2 = new Lever(false);
st1 = true;
st2 = false;
}
public void open() {
state1 = new Door(true);
state1.handle();
st1 = true;
}
public void close() {
state1 = new Door(false);
state1.handle();
st1 = false;
}
public void pull() {
state2 = new Lever(true);
state2.handle();
st2 = true;
}
public void push() {
state2 = new Lever(false);
state2.handle();
st2 = false;
}
public void info() {
if (!st1 && st2) {
System.out.println("Control System is opened");
if (!st2) {
System.out.println("You can take the treasure");
} else {
System.out.println("Oww... You get trapped");
}
}
}
}
The handle()
method just prints the actual state of certain thing.
I know that the condition in method info()
is nonsense, but I let it be that way so I could get help from you guys how to edit it....
Aucun commentaire:
Enregistrer un commentaire