资源预览内容
第1页 / 共12页
第2页 / 共12页
第3页 / 共12页
第4页 / 共12页
第5页 / 共12页
第6页 / 共12页
第7页 / 共12页
第8页 / 共12页
第9页 / 共12页
第10页 / 共12页
亲,该文档总共12页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述
1、初识状态模式按钮来控制一个电梯的状态,一个电梯开们,关门,停,运行。每i种状态改变,都有可能要根据其他状态来 更新处理。例如,开门状体,你不能在运行的时候开门,而是在电梯定下后才能开门。我们给一部手机打电话,就可能出现这几种情况:用户开机,用户关机,用户欠费停机,用户消户,对方正在 通话中,己经连接上对方等。所以当我们拨打这个号码的时候:系统就要判断,该用户是否在开机旦不忙状态, 又或者是关机,欠费等状态。但不管是那种状态我们都应给出对应的处理操作。2、什么是状态模式允许一个对象在其内部状态改变时改变它的行为。对象看起来似乎修改了它的类。3、模式结构图ContextstateORequest() 9KIstate-Handle()4、模式代码事例4、1涉及到的类,以及类图MobileContext s Abstract States CallEndState、CallStartState ConnectState、WaitState4、2具体的代码实现非状态模式实现/*电话* author LuXiaoFeng* date 2012-12-23*/public class Mobile implements IStateConstants (private int currentstate;public int getCurrentState() (return currentstate; public void setCurrentState(int currentstate) ( this.currentstate = currentstate;public boolean callStart() (boolean result = false;LogUtiLprintWithSystemOutC, 开始准备打电话,拨号中);switch (currentstate) (case callend state:break;case callstart_state:result = isBusy();break;case connect_state:break;case wait_state:break;default:break;return result;public void connecting(int connectState) (switch (connectState) (case callend state:break;case callstart_state:break;case connectstate:Log Ut il. print With Syst em Out (对方电话已经接通,正在聊天ing );LogUtiLprintWithSystemOutC, 聊天结束);break;case wait_state:break;default:break;public void callEnd(int callendState) (switch (callendState) (case callend state:LogUtil.printWithSystemErr(“通话结束);break;case callstart_state:break;case connect_state:break;case wait_state:break;default:break;public void waitting(int waitState) (switch (waitState) (case callend state:break;case callstart_state:break;case connect_state:break;case waitstate:LogUtil.printWithSystemErr对方正在通话中,请稍后再拨);break;default:break;boolean isBusyO (return true;NoPatternRunpublic class NoPatternRun implements IPatternRunOverride public void run() Mobile mobile = new Mobile(); if(mobile.callStart()mobile.waittingtestate);mobile.callEnd(callend_state);else mobile.connecting(connect_state);mobile.callEnd(callend_state); _状态模式实现MobileContext* author LuXiaoFeng* date 2012-12-23*/public class MobileContext implements IStateConstants private static Map stateMap; private Abstractstate currentstate;void initStateMap() stateMap = new HashMap();stateMap.put(callstart_statef new CallStartState(th stateMap.put(ca!2end_state new CallEndStace(this) stateMap.put (vai.state, new WaitState (this);stateMap.put(connect_stanew ConnectState(this) _public MobileContext() initStateMap();public void setState(int stateType) currentstate = stateWap.get(stateType);public void callStart () currentstate.callStart ();public void connecting() currentstate.connecting();)pnblic void callEnd() currentstatecallEnd();public void waitting() currentstate.waitting();)电话状态* author LuXiaoFeng date 2012-12-23*/pnblic abstract class AbstractState implements IStateConstants protected MobileContext context;public MobileContext getContext() return context;pnblic void setContext(MobileContext context) this.context = context;pnblicabstract voidcallStart();publicabstractvoidconnecting();pnblicabstractvoidcallEnd();publicabstractvoidwaitting();CallEndStateimport .designpattern.utilLogUtil;电话结束状态author LuXiaoFeng date 2012-12-23 /pnblic class CallEndState extends AbstractStatepublic CallEndState(MobileContextthiscontext = context;context) Overridepnblic void/ TODOcallStart () Auto-generated methodstubOverridepublic void/ TODOconnecting() Auto-generated methodstubOverride public voidLogUtil.printlfithSystemErr(;callEnd() Override public void waitting() / TODO Auto-generated method stub电话开始状态* author LuXiaoFeng* date 2012-12-23 */public class CallStartState extends Abstractstate public CallStartState(MobileContext context) this.context = context;Overridepublic void callStart() LogUtil .printJfithSystemOut备打电话,拨号中);if (isBusy () ) context.setState(vait_state); context.waitting(); else context.setState(connect_state); contextconnecting(); )|Overridepublic void connecting() Overridepublic void callEnd() Overridepublic void waitting() boolean isBusy() return true;import .designpattern.util.LogUtil;/*电话连接状态* author LuXiaoFeng* date 2012-12-23*/public class ConnectState extends Abstractstate public ConnectState(MobileContext context) this.context = context;)Overridepublic void callStart() r Override、public void connecting() LogUtil.printJFithSystemOut (”对方电话已经
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号