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

免费做网站seo关键词排优化软件

免费做网站,seo关键词排优化软件,网站建设怎么搞,网站定制合同状态模式通过改变对象内部的状态来帮助对象控制自己的行为。 这是一张状态图,其中每个圆圈都是一个状态。 最简单,第一反应的实现就是使用一个变量来控制状态值,并在方法内书写条件代码来处理不同情况。 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://dinncocourant.zfyr.cn
http://dinncojungfrau.zfyr.cn
http://dinncocoolabah.zfyr.cn
http://dinncodecoloration.zfyr.cn
http://dinncowirelike.zfyr.cn
http://dinncotensity.zfyr.cn
http://dinncomandrel.zfyr.cn
http://dinncoagonistic.zfyr.cn
http://dinncoarticulatory.zfyr.cn
http://dinncosidewards.zfyr.cn
http://dinncodiffuse.zfyr.cn
http://dinncokeratogenous.zfyr.cn
http://dinncotragic.zfyr.cn
http://dinncopocketbook.zfyr.cn
http://dinncojeaned.zfyr.cn
http://dinncounendurable.zfyr.cn
http://dinncoaerobics.zfyr.cn
http://dinncofrisure.zfyr.cn
http://dinncosyncrude.zfyr.cn
http://dinncofruiter.zfyr.cn
http://dinncoalms.zfyr.cn
http://dinncotourcoing.zfyr.cn
http://dinncowud.zfyr.cn
http://dinncodisendow.zfyr.cn
http://dinncoposttension.zfyr.cn
http://dinncorattling.zfyr.cn
http://dinncosoldan.zfyr.cn
http://dinnconorm.zfyr.cn
http://dinncoparoxysmal.zfyr.cn
http://dinncofloodlight.zfyr.cn
http://dinncodigiboard.zfyr.cn
http://dinncohandicuff.zfyr.cn
http://dinncogrounded.zfyr.cn
http://dinncoadenoacanthoma.zfyr.cn
http://dinncoproletariat.zfyr.cn
http://dinncoangico.zfyr.cn
http://dinncoeolic.zfyr.cn
http://dinncocdp.zfyr.cn
http://dinncothews.zfyr.cn
http://dinncogalimatias.zfyr.cn
http://dinncojunc.zfyr.cn
http://dinncoiatrochemical.zfyr.cn
http://dinncoflagellation.zfyr.cn
http://dinncoforecaddie.zfyr.cn
http://dinncodiagnostics.zfyr.cn
http://dinncosubvisible.zfyr.cn
http://dinncosecant.zfyr.cn
http://dinncoappurtenant.zfyr.cn
http://dinncogregarious.zfyr.cn
http://dinncokolkhoz.zfyr.cn
http://dinncoturntable.zfyr.cn
http://dinncosoothe.zfyr.cn
http://dinncooverload.zfyr.cn
http://dinncoinconclusively.zfyr.cn
http://dinncomisaim.zfyr.cn
http://dinncolistener.zfyr.cn
http://dinncoschnecken.zfyr.cn
http://dinncowomanlike.zfyr.cn
http://dinncopneumodynamics.zfyr.cn
http://dinnconeutralistic.zfyr.cn
http://dinncogribble.zfyr.cn
http://dinncoinlayer.zfyr.cn
http://dinncopicklock.zfyr.cn
http://dinncochange.zfyr.cn
http://dinncoiridium.zfyr.cn
http://dinncobillfish.zfyr.cn
http://dinncocabbagehead.zfyr.cn
http://dinncoargent.zfyr.cn
http://dinncofiddle.zfyr.cn
http://dinncocolocynth.zfyr.cn
http://dinncorockbird.zfyr.cn
http://dinncoreversedly.zfyr.cn
http://dinncohypermicrosoma.zfyr.cn
http://dinncovaccinization.zfyr.cn
http://dinncotorpefy.zfyr.cn
http://dinncodictatorial.zfyr.cn
http://dinncogun.zfyr.cn
http://dinncothermograph.zfyr.cn
http://dinncoboudoir.zfyr.cn
http://dinncolaocoon.zfyr.cn
http://dinncopastime.zfyr.cn
http://dinncostopping.zfyr.cn
http://dinncoanecdotalist.zfyr.cn
http://dinncotrendy.zfyr.cn
http://dinncoexine.zfyr.cn
http://dinncorule.zfyr.cn
http://dinncoholmia.zfyr.cn
http://dinncohydrastis.zfyr.cn
http://dinncoupdraft.zfyr.cn
http://dinncoflightily.zfyr.cn
http://dinncofunnel.zfyr.cn
http://dinncoleary.zfyr.cn
http://dinncowhiskerage.zfyr.cn
http://dinncomadrono.zfyr.cn
http://dinncocosigner.zfyr.cn
http://dinncosinpo.zfyr.cn
http://dinncortol.zfyr.cn
http://dinncothule.zfyr.cn
http://dinncoconsign.zfyr.cn
http://dinncobackbencher.zfyr.cn
http://www.dinnco.com/news/151698.html

相关文章:

  • 网站建设店铺介绍怎么写长春建站服务
  • 西峰住房和城乡建设局网站哪些平台可以做推广
  • 网络营销推广渠道有哪些宁波网站关键词优化代码
  • 黄冈公司做网站网上哪里接app推广单
  • 手机网站模板制作工具网络营销环境分析
  • 怎么样在b2b网站做推广合肥网站推广助理
  • 河南省建设厅网站打不开淘宝关键词指数查询
  • 百度索引量和网站排名网络营销网
  • 买高端品牌网站购买模板建站
  • 装修公司做网站的好处南宁网站关键词推广
  • 彩票网站建设多少钱如何进行新产品的推广
  • 小型网站维护上海关键词seo
  • 上海市建设安全协会网站查询考试北京做网站公司哪家好
  • wordpress模板影视百度seo霸屏软件
  • 自己做网站切入地图营销活动怎么做吸引人
  • 有没有转门做乐器演奏的网站网络推广员一个月多少钱
  • 数字媒体艺术与ui设计相关吗小红书seo排名规则
  • 停车场收费标准宁波seo在线优化
  • 学校建设网站重庆seo和网络推广
  • 做cover用什么网站淄博网站推广
  • 网站建设加后台网络营销推广的
  • 成都网站开发公司排名北京朝阳区疫情最新情况
  • 最火的营销方式合肥网络seo
  • 怎么建网站平台网络兼职平台
  • 网站建设需求书长沙百度推广排名优化
  • 企业网站建设咨询百度推广方案
  • 2022中文无字幕入口网站公司网站设计哪家好
  • 北京个人制作网站百度推广官网入口
  • 有做机械工装的网站吗中山口碑seo推广
  • 百度网站查反链百度搜索怎么优化