《大話設(shè)計(jì)模式》閱讀筆記和總結(jié)诞挨。原書(shū)是C#編寫(xiě)的扫沼,本人用Java實(shí)現(xiàn)了一遍,包括每種設(shè)計(jì)模式的UML圖實(shí)現(xiàn)和示例代碼實(shí)現(xiàn)浴韭。
目錄:設(shè)計(jì)模式
Github地址:DesignPattern
說(shuō)明
定義:狀態(tài)模式(State)客叉,當(dāng)一個(gè)對(duì)象的內(nèi)在狀態(tài)改變時(shí)允許改變其行為诵竭,這個(gè)對(duì)象看起來(lái)像是改變了其類(lèi)。
UML圖:
代碼實(shí)現(xiàn):
State類(lèi)兼搏,抽象狀態(tài)類(lèi)卵慰,定義一個(gè)接口以封裝與Context的一個(gè)特定狀態(tài)相關(guān)的行為。
abstract class State{
public abstract void Handle(Context context);
}
ConcreteState類(lèi)向族,具體狀態(tài)呵燕。每一個(gè)子類(lèi)實(shí)現(xiàn)一個(gè)與Context的一個(gè)狀態(tài)相關(guān)的行為。
class ConcreteStateA extends State {
@Override
public void Handle(Context context) {
context.setState(new ConcreteStateB());
}
}
class ConcreteStateB extends State {
@Override
public void Handle(Context context) {
context.setState(new ConcreteStateA());
}
}
Context類(lèi)件相,維護(hù)一個(gè)ConcreteState子類(lèi)的實(shí)例,這個(gè)實(shí)例定義當(dāng)前的狀態(tài)氧苍。
class Context{
private State state;
// 定義Context的初始狀態(tài)
public Context(State state){
this.state = state;
}
public State getState() {
return state;
}
public void setState(State state) {
this.state = state;
System.out.println("當(dāng)前狀態(tài):"+state);
}
// 對(duì)請(qǐng)求做處理夜矗,并設(shè)置下一狀態(tài)
public void Request(){
state.Handle(this);
}
}
客戶(hù)端代碼
public class StatePattern {
public static void main(String[] args){
Context context = new Context(new ConcreteStateA());
context.Request();
context.Request();
context.Request();
context.Request();
}
}
運(yùn)行結(jié)果
當(dāng)前狀態(tài):com.shijc.dp.state.ConcreteStateB@4554617c
當(dāng)前狀態(tài):com.shijc.dp.state.ConcreteStateA@74a14482
當(dāng)前狀態(tài):com.shijc.dp.state.ConcreteStateB@1540e19d
當(dāng)前狀態(tài):com.shijc.dp.state.ConcreteStateA@677327b6
示例
例子:程序員的一天往往是比較固定的,上午的時(shí)候精神百倍让虐,中午餓了犯困紊撕,需要午休;下午精神還不錯(cuò)赡突,如果能準(zhǔn)時(shí)下班必是極好的对扶;但是如果加班到晚上,傍晚比較疲憊惭缰,如果加班到很晚浪南,那就困極了睡著了。用程序模擬這個(gè)過(guò)程漱受。
UML圖:
代碼實(shí)現(xiàn):
抽象狀態(tài)類(lèi)络凿,定義一個(gè)抽象方法"寫(xiě)程序"
public abstract class WorkState {
public abstract void WriteProgram(Work w);
}
上午工作狀態(tài)
public class ForenoonState extends WorkState {
@Override
public void WriteProgram(Work w) {
if (w.getHour()<12){
System.out.println("當(dāng)前時(shí)間:"+ w.getHour()+"點(diǎn) 上午工作,精神百倍");
}else {
w.setState(new NoonState());
w.WriteProgram();
}
}
}
中午工作狀態(tài)
public class NoonState extends WorkState {
@Override
public void WriteProgram(Work w) {
if (w.getHour()<13){
System.out.println("當(dāng)前時(shí)間:"+ w.getHour()+"點(diǎn) 餓了昂羡,犯困絮记,午休");
}else {
w.setState(new AfternoonState());
w.WriteProgram();
}
}
}
下午工作狀態(tài)
public class AfternoonState extends WorkState {
@Override
public void WriteProgram(Work w) {
if (w.getHour()<17){
System.out.println("當(dāng)前時(shí)間:"+ w.getHour()+"點(diǎn) 下午狀態(tài)還不錯(cuò),繼續(xù)努力");
}else {
w.setState(new EveningState());
w.WriteProgram();
}
}
}
晚上工作狀態(tài)
public class EveningState extends WorkState {
@Override
public void WriteProgram(Work w) {
if (w.isFinish()){
w.setState(new RestState());
w.WriteProgram();
}else {
if (w.getHour()<21){
System.out.println("當(dāng)前時(shí)間:"+ w.getHour()+"點(diǎn) 加班哦虐先,疲累至極");
}else {
w.setState(new SleepingState());
w.WriteProgram();
}
}
}
}
睡眠工作狀態(tài)
public class SleepingState extends WorkState {
@Override
public void WriteProgram(Work w) {
System.out.println("當(dāng)前時(shí)間:"+ w.getHour()+"點(diǎn) 不行了怨愤,睡著了");
}
}
下班狀態(tài)
public class RestState extends WorkState {
@Override
public void WriteProgram(Work w) {
System.out.println("當(dāng)前時(shí)間:"+ w.getHour()+"點(diǎn) 下班回家了");
}
}
工作類(lèi)
public class Work {
private WorkState current;
public Work(){
//工作初始化為上午工作狀態(tài),即上午9點(diǎn)開(kāi)始上班
current = new ForenoonState();
}
// 鐘點(diǎn)屬性蛹批,狀態(tài)轉(zhuǎn)換的依據(jù)
private double hour;
// 任務(wù)完成屬性
private boolean finish;
public double getHour() {
return hour;
}
public void setHour(double hour) {
this.hour = hour;
}
public boolean isFinish() {
return finish;
}
public void setFinish(boolean finish) {
this.finish = finish;
}
public void setState(WorkState state){
current = state;
}
public void WriteProgram(){
current.WriteProgram(this);
}
}
客戶(hù)端代碼
public class Main {
public static void main(String[] args){
Work emergencyProjects = new Work();
emergencyProjects.setHour(9);
emergencyProjects.WriteProgram();
emergencyProjects.setHour(10);
emergencyProjects.WriteProgram();
emergencyProjects.setHour(12);
emergencyProjects.WriteProgram();
emergencyProjects.setHour(13);
emergencyProjects.WriteProgram();
emergencyProjects.setHour(14);
emergencyProjects.WriteProgram();
emergencyProjects.setHour(17);
emergencyProjects.setFinish(false);
emergencyProjects.WriteProgram();
emergencyProjects.setHour(19);
emergencyProjects.WriteProgram();
emergencyProjects.setHour(22);
emergencyProjects.WriteProgram();
}
}
運(yùn)行結(jié)果
當(dāng)前時(shí)間:9.0點(diǎn) 上午工作撰洗,精神百倍
當(dāng)前時(shí)間:10.0點(diǎn) 上午工作膀息,精神百倍
當(dāng)前時(shí)間:12.0點(diǎn) 餓了,犯困了赵,午休
當(dāng)前時(shí)間:13.0點(diǎn) 下午狀態(tài)還不錯(cuò)潜支,繼續(xù)努力
當(dāng)前時(shí)間:14.0點(diǎn) 下午狀態(tài)還不錯(cuò),繼續(xù)努力
當(dāng)前時(shí)間:17.0點(diǎn) 加班哦柿汛,疲累至極
當(dāng)前時(shí)間:19.0點(diǎn) 加班哦冗酿,疲累至極
當(dāng)前時(shí)間:22.0點(diǎn) 不行了,睡著了