导航
您当前的位置:首页 > 计算机 > 软件水平
问题:

[问答题] 某大型商场内安装了多个简易的纸巾售卖机,自动出售2元钱一包的纸巾,且每次仅售出一包纸巾。纸巾售卖机的状态图如图6-1所示。中级软件设计师,历年真题,2011年下半年(下午)《软件设计师》真题采用状态(State)模式来实现该纸巾售卖机,得到如图6-2所示的类图。其中类State为抽象类,定义了投币、退币、出纸巾等方法接口。类SoldState、SoldOutState、NoQuarterState和HasQuarterState分别对应图6-1中纸巾售卖机的4种状态:售出纸巾、纸巾售完、没有投币、有2元钱。中级软件设计师,历年真题,2011年下半年(下午)《软件设计师》真题【Java代码】import java.util.*;interface State{public void insertQuarter(  );//投币public void ejectQuarter(  );//退币public void turnCrank(  );//按下“出纸巾”按钮public void dispense(  );//出纸巾}class TissueMachine{(1)soldOutState,noQuarterState,hasQuarterState,soldState,state;state=soldOutState;int count=0;//纸巾数public TissueMachine(int numbers){/*实现代码省略*/}public State getHasQuarterState(  ){return hasQuarterState;}public State getNoQuarterState(  ){return noQuarterState;}public State getSoldState(  ){return soldState;}public State getSoldOutState(  ){return soldOutState;}public int getCount(  ){return count;}//其余代码省略}class NoQuarterState implements State{TissueMachine tissueMachine;public void insertQuarter(  ){tissueMachine.setState((2));}//构造方法以及其余代码省略}class HasQuarterState implements State{TissueMachine tissueMachine;public void ejectQuarter(  ){tissueMachine.setState((3));}//构造方法以及其余代码省略}class SoldState implements State{TissueMachine tissueMachine;public void dispense(  ){if(tissueMachine.getCount(  )>0){tissueMachine.setState((4));}else{tissueMachine.setState((5));}}}
答案解析:

相关问题
关于我们 | 用户指南 | 版权声明 | 给我留言 | 联系我们 | 积分商城 | 答案求助 | 网站地图
Copyright © 2024 www.daanwo.com All Rights Reserved