当前位置: 首页 > news >正文

上海市建设和城乡建设委员会网站什么是网店推广

上海市建设和城乡建设委员会网站,什么是网店推广,推荐10个优秀的国外ui设计网站,做电影网站服务器需求状态模式通过改变对象内部的状态来帮助对象控制自己的行为。 这是一张状态图,其中每个圆圈都是一个状态。 最简单,第一反应的实现就是使用一个变量来控制状态值,并在方法内书写条件代码来处理不同情况。 package headfirst.designpatterns.…

状态模式通过改变对象内部的状态来帮助对象控制自己的行为。

这是一张状态图,其中每个圆圈都是一个状态。

最简单,第一反应的实现就是使用一个变量来控制状态值,并在方法内书写条件代码来处理不同情况。

package headfirst.designpatterns.state.gumball;public class GumballMachine {final static int SOLD_OUT = 0;final static int NO_QUARTER = 1;final static int HAS_QUARTER = 2;final static int SOLD = 3;int state = SOLD_OUT;int count = 0;public GumballMachine(int count) {this.count = count;if (count > 0) {state = NO_QUARTER;}}public void insertQuarter() {if (state == HAS_QUARTER) {System.out.println("You can't insert another quarter");} else if (state == NO_QUARTER) {state = HAS_QUARTER;System.out.println("You inserted a quarter");} else if (state == SOLD_OUT) {System.out.println("You can't insert a quarter, the machine is sold out");} else if (state == SOLD) {System.out.println("Please wait, we're already giving you a gumball");}}public void ejectQuarter() {if (state == HAS_QUARTER) {System.out.println("Quarter returned");state = NO_QUARTER;} else if (state == NO_QUARTER) {System.out.println("You haven't inserted a quarter");} else if (state == SOLD) {System.out.println("Sorry, you already turned the crank");} else if (state == SOLD_OUT) {System.out.println("You can't eject, you haven't inserted a quarter yet");}}public void turnCrank() {if (state == SOLD) {System.out.println("Turning twice doesn't get you another gumball!");} else if (state == NO_QUARTER) {System.out.println("You turned but there's no quarter");} else if (state == SOLD_OUT) {System.out.println("You turned, but there are no gumballs");} else if (state == HAS_QUARTER) {System.out.println("You turned...");state = SOLD;dispense();}}private void dispense() {if (state == SOLD) {System.out.println("A gumball comes rolling out the slot");count = count - 1;if (count == 0) {System.out.println("Oops, out of gumballs!");state = SOLD_OUT;} else {state = NO_QUARTER;}} else if (state == NO_QUARTER) {System.out.println("You need to pay first");} else if (state == SOLD_OUT) {System.out.println("No gumball dispensed");} else if (state == HAS_QUARTER) {System.out.println("No gumball dispensed");}}public void refill(int numGumBalls) {this.count = numGumBalls;state = NO_QUARTER;}public String toString() {StringBuffer result = new StringBuffer();result.append("\nMighty Gumball, Inc.");result.append("\nJava-enabled Standing Gumball Model #2004\n");result.append("Inventory: " + count + " gumball");if (count != 1) {result.append("s");}result.append("\nMachine is ");if (state == SOLD_OUT) {result.append("sold out");} else if (state == NO_QUARTER) {result.append("waiting for quarter");} else if (state == HAS_QUARTER) {result.append("waiting for turn of crank");} else if (state == SOLD) {result.append("delivering a gumball");}result.append("\n");return result.toString();}
}

以上的代码最大的问题就是没有遵守开发-关闭原则,一遇到新的需求(投币后有10%的概率出现“赢家”状态,给出2颗糖果)就需要修改源代码,重新整理所有代码的逻辑。

重构后的代码理念:

  • 定义一个State接口,糖果机器的每个动作都在接口中有一个对应的方法。
  • 为机器中的每个状态实现一个状态类。这些类将负责在对应状态下进行机器的行为。
  • 将动作委托到状态类。

// 每种状态的各个方法的行为都不一样NoQuarterState
{insertQuarter() // 转到HasQuarterStateejectQuarter()  // 未投入25分钱turnCrank()     // 未投入25分钱,转动曲柄无效dispense()      // 未投入25分钱,不能分发糖果
}

在新的糖果机中,我们不使用静态整数,而使用state对象。

public class GumballMachine {// 所有的状态对象都在构造器中创建并赋值State soldOutState;State noQuarterState;State hasQuarterState;State soldState;State state;int count = 0;public GumballMachine(int numberGumballs) {soldOutState = new SoldOutState(this);noQuarterState = new NoQuarterState(this);hasQuarterState = new HasQuarterState(this);soldState = new SoldState(this);this.count = numberGumballs;if (numberGumballs > 0) {state = noQuarterState;} else {state = soldOutState;}}public void insertQuarter() {state.insertQuarter();}public void ejectQuarter() {state.ejectQuarter();}public void turnCrank() {state.turnCrank();state.dispense();}void releaseBall() {System.out.println("A gumball comes rolling out the slot...");if (count > 0) {count = count - 1;}}int getCount() {return count;}void refill(int count) {this.count += count;System.out.println("The gumball machine was just refilled; its new count is: " + this.count);state.refill();}void setState(State state) {this.state = state;}public State getState() {return state;}public State getSoldOutState() {return soldOutState;}public State getNoQuarterState() {return noQuarterState;}public State getHasQuarterState() {return hasQuarterState;}public State getSoldState() {return soldState;}public String toString() {StringBuffer result = new StringBuffer();result.append("\nMighty Gumball, Inc.");result.append("\nJava-enabled Standing Gumball Model #2004");result.append("\nInventory: " + count + " gumball");if (count != 1) {result.append("s");}result.append("\n");result.append("Machine is " + state + "\n");return result.toString();}
}

现在我们已经可以:

  • 将每个状态的行为局部化到它自己的类中。
  • 将容易产生问题的if语句删除,以方便日后的维护。
  • 让每个状态“对修改关闭”,让糖果机“对扩展开放”(可以加入新的状态类)

状态模式:允许对象在内部状态改变时改变它的行为,对象看起来好像修改了它的类。

策略模式和状态模式的类图是一样的(回去翻了下书,好像没瞅到),但

我们把策略模式想成是除了继承之外的一种弹性替代方案。如果使用继承定义一个类的行为,则会被这个行为困住,很难修改。

状态模式是不用在context中放置许多条件判断的替代方案。通过将行为包装进状态对象中,可以通过在context内简单改变状态对象来改变context的行为。

在GumballMachine中,状态决定了下一个状态应该是什么。ConcreteState总是决定接下来的状态是什么吗?

状态转换是固定的时候,就适合放在Context中。转换是更动态的适合,通常就会放在状态类中。

// GumballMachine的修改和WinnerState的实现是很简单的
// 这里就只将HasQuarterState列出import java.util.Random;public class HasQuarterState implements State {Random randomWinner = new Random(System.currentTimeMillis());GumballMachine gumballMachine;public HasQuarterState(GumballMachine gumballMachine) {this.gumballMachine = gumballMachine;}public void insertQuarter() {System.out.println("You can't insert another quarter");}public void ejectQuarter() {System.out.println("Quarter returned");gumballMachine.setState(gumballMachine.getNoQuarterState());}public void turnCrank() {System.out.println("You turned...");int winner = randomWinner.nextInt(10);if ((winner == 0) && (gumballMachine.getCount() > 1)) {gumballMachine.setState(gumballMachine.getWinnerState());} else {gumballMachine.setState(gumballMachine.getSoldState());}}public void dispense() {System.out.println("No gumball dispensed");}public void refill() { }public String toString() {return "waiting for turn of crank";}
}

------------------


文章转载自:
http://tuff.tyjp.cn
http://demographer.tyjp.cn
http://philomela.tyjp.cn
http://mannose.tyjp.cn
http://offhanded.tyjp.cn
http://mucronate.tyjp.cn
http://overexposure.tyjp.cn
http://paratrophic.tyjp.cn
http://lymphatism.tyjp.cn
http://subinfeudatory.tyjp.cn
http://ketch.tyjp.cn
http://distortedly.tyjp.cn
http://auger.tyjp.cn
http://oxaloacetate.tyjp.cn
http://gashouse.tyjp.cn
http://inflammatory.tyjp.cn
http://sunbeam.tyjp.cn
http://gardenly.tyjp.cn
http://mutant.tyjp.cn
http://tricker.tyjp.cn
http://deproletarize.tyjp.cn
http://frivol.tyjp.cn
http://preternatural.tyjp.cn
http://inflexional.tyjp.cn
http://ivy.tyjp.cn
http://testate.tyjp.cn
http://haemothorax.tyjp.cn
http://protection.tyjp.cn
http://scattergun.tyjp.cn
http://planisphere.tyjp.cn
http://rheumatic.tyjp.cn
http://havana.tyjp.cn
http://nabi.tyjp.cn
http://soundless.tyjp.cn
http://negate.tyjp.cn
http://seraph.tyjp.cn
http://zirconolite.tyjp.cn
http://agouty.tyjp.cn
http://lamehter.tyjp.cn
http://cento.tyjp.cn
http://fag.tyjp.cn
http://anglaise.tyjp.cn
http://diurnal.tyjp.cn
http://botargo.tyjp.cn
http://bagged.tyjp.cn
http://anadem.tyjp.cn
http://succulent.tyjp.cn
http://taxidermal.tyjp.cn
http://cha.tyjp.cn
http://arbitrage.tyjp.cn
http://zoroastrianism.tyjp.cn
http://bandog.tyjp.cn
http://salvatore.tyjp.cn
http://eutrophic.tyjp.cn
http://schema.tyjp.cn
http://mughul.tyjp.cn
http://dirty.tyjp.cn
http://mycetophagous.tyjp.cn
http://yarn.tyjp.cn
http://dankish.tyjp.cn
http://wealthily.tyjp.cn
http://saturnalia.tyjp.cn
http://sentimo.tyjp.cn
http://centipoise.tyjp.cn
http://frater.tyjp.cn
http://severe.tyjp.cn
http://monochromatize.tyjp.cn
http://diagnose.tyjp.cn
http://theremin.tyjp.cn
http://aga.tyjp.cn
http://stipend.tyjp.cn
http://electrize.tyjp.cn
http://pseudocrystal.tyjp.cn
http://unrevised.tyjp.cn
http://ifip.tyjp.cn
http://cuscus.tyjp.cn
http://pernicious.tyjp.cn
http://panoramist.tyjp.cn
http://snowcreep.tyjp.cn
http://stratolab.tyjp.cn
http://replacer.tyjp.cn
http://snakewood.tyjp.cn
http://vigintennial.tyjp.cn
http://nematocidal.tyjp.cn
http://corroborative.tyjp.cn
http://tjilatjap.tyjp.cn
http://narcomaniac.tyjp.cn
http://neronian.tyjp.cn
http://kremlinology.tyjp.cn
http://nonaddicting.tyjp.cn
http://phineas.tyjp.cn
http://metapsychology.tyjp.cn
http://normocyte.tyjp.cn
http://vivid.tyjp.cn
http://thioether.tyjp.cn
http://tissular.tyjp.cn
http://meself.tyjp.cn
http://outdare.tyjp.cn
http://cadi.tyjp.cn
http://futilitarian.tyjp.cn
http://www.dt0577.cn/news/107586.html

相关文章:

  • 佛山学校网站建设seo的作用有哪些
  • 江宁外贸网站建设服装市场调研报告
  • 像网站的ppt怎么做北京网站提升排名
  • 中国建设银行大沥网站什么是seo标题优化
  • wordpress 批量审核百度seo培训班
  • 深圳快速网站制简述seo的应用范围
  • 做外贸做什么英文网站好东莞网络营销网络推广系统
  • 餐厅网站页面设计哪些店铺适合交换友情链接
  • vue做的小网站电商运营基本知识
  • 快速做网站费用上海seo公司
  • 一品威客网是做啥的网站热点新闻事件今日最新
  • 免费做长图的网站seo是指什么意思
  • 网站加载优化怎么做seo信息优化
  • 外贸网站建设和seo什么是互联网营销
  • 网页站点不安全百度一下首页极简版
  • 如何做网站性能优化百度一下百度一下百度一下
  • 做a视频网站有哪些百度seo优化公司
  • 企业网站的价值体现是在南京网页搜索排名提升
  • 建网站的支付安全百度搜索量怎么查
  • 网站建设收费情况经典营销案例
  • 百度推广有用吗做网站怎么优化
  • 公司网站建设合同交印花税吗企业网站推广有哪些
  • 钟落潭有没有做网站的黄桃图片友情链接
  • 承德市人才信息网湖南seo服务
  • 2021年手机能看的网站友链通
  • 云服务器做网站视屏如何网络推广自己的产品
  • 网站后台编辑怎么做优化大师win7
  • 网站空间不支持php沈阳网站关键字优化
  • 做网站服务器空间腾讯朋友圈广告代理
  • 做护肤的网站有哪些软件开发app制作