State模式將對象行為的變化封裝成具有統一接口的狀態
<!
<!
下面是自動門的狀態圖
<!
<!
抽象狀態DoorState提供open()和close()兩個方法
package com
public interface DoorState {
void open();
void close();
}
Context為AutoDoor
package com
public class AutoDoor {
private DoorState state;
private DoorState open = new Open(this);
private DoorState close = new Close(this);
public AutoDoor() {
this
}
public void setSate(DoorState state) {
this
}
public DoorState getOpenState() {
return open;
}
public DoorState getCloseState() {
return close;
}
public void open() {
state
}
public void close() {
state
}
}
ConcreteState有兩個
狀態Open
package com
public class Open implements DoorState {
private AutoDoor door;
public Open(AutoDoor door) {
this
}
public void close() {
System
door
}
public void open() {
System
}
}
狀態Close
package com
public class Close implements DoorState {
private AutoDoor door;
public Close(AutoDoor door) {
this
}
public void close() {
System
}
public void open() {
System
door
}
}
由兩個具體狀態類可知
From:http://tw.wingwit.com/Article/program/Java/hx/201311/26679.html