基于设计模式的学习之旅

上传人:ss****gk 文档编号:206248795 上传时间:2021-10-30 格式:DOC 页数:12 大小:120.50KB
返回 下载 相关 举报
基于设计模式的学习之旅_第1页
第1页 / 共12页
基于设计模式的学习之旅_第2页
第2页 / 共12页
基于设计模式的学习之旅_第3页
第3页 / 共12页
基于设计模式的学习之旅_第4页
第4页 / 共12页
基于设计模式的学习之旅_第5页
第5页 / 共12页
点击查看更多>>
资源描述

《基于设计模式的学习之旅》由会员分享,可在线阅读,更多相关《基于设计模式的学习之旅(12页珍藏版)》请在金锄头文库上搜索。

1、1、初识状态模式按钮来控制一个电梯的状态,一个电梯开们,关门,停,运行。每i种状态改变,都有可能要根据其他状态来 更新处理。例如,开门状体,你不能在运行的时候开门,而是在电梯定下后才能开门。我们给一部手机打电话,就可能出现这几种情况:用户开机,用户关机,用户欠费停机,用户消户,对方正在 通话中,己经连接上对方等。所以当我们拨打这个号码的时候:系统就要判断,该用户是否在开机旦不忙状态, 又或者是关机,欠费等状态。但不管是那种状态我们都应给出对应的处理操作。2、什么是状态模式允许一个对象在其内部状态改变时改变它的行为。对象看起来似乎修改了它的类。3、模式结构图ContextstateOReques

2、t() 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 current

3、state; 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;ca

4、se 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;d

5、efault: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:br

6、eak;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()m

7、obile.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;vo

8、id 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(in

9、t 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 abstr

10、act class AbstractState implements IStateConstants protected MobileContext context;public MobileContext getContext() return context;pnblic void setContext(MobileContext context) this.context = context;pnblicabstract voidcallStart();publicabstractvoidconnecting();pnblicabstractvoidcallEnd();publicabs

11、tractvoidwaitting();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

12、/ 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 c

13、ontext) 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() Overridep

14、ublic 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 (”对方电话已经

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 办公文档 > 其它办公文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号