一、浩言
人静而后安,安而能后定,定而能后慧,慧而能后悟,悟而能后得。
二、背景
开年,看了看最近网上传的《阿里巴巴Java开发规范》,然后看到了如下一部分。
这里说如果if else if else超过了三层就需要使用状态设计模式。碰巧年前我写了一个订单状态转换的代码,我没有使用if else if else的形式,我使用了case,如果改为if等 就是if else if else形式了。
所以我就在思考,这段代码是否可以改为所说的状态设计模式了?
三、状态设计模式
3.1状态模式定义:
状态模式是属于行为模式的范畴,即是对象在内部状态改变时改变它的行为,对象看起来好像修改了它的类。
下图是截取《Head First 设计模式》中的图片
所以我上图code.png图片中,是没有对象状态变化或者行为的操作的。所以多层的if else if else也是要根据实际的代码问题来做处理。下面上《Head First 设计模式》中的代码如下
3.2 code代码案例
State状态类
package com.mouse.design.state;
/**
* Created by wuhao on 2017/2/7.
*/
public interface State {
public void insertQuarter();
public void ejectQuarter();
public void turnCrank();
public void dispense();
}
package com.mouse.design.state;
/**
* Created by wuhao on 2017/2/7.
*/
public class GumballMachine {
State hasQuarterState;
State noQuarterState;
State soldOutState;
State soldState;
State winnerState;
State state = soldOutState;
int count = 0;
public GumballMachine(int numberCount) {
this.count = numberCount;
hasQuarterState = new HasQuarterState(this);
noQuarterState = new NoQuarterState(this);
soldOutState = new SoldOutState(this);
soldState = new SoldState(this);
winnerState = new WinnerState(this);
if (numberCount > 0) {
state = noQuarterState;
}
}
/**
* 投币
*/
public void insertQuarter() {
state.insertQuarter();
}
/**
* 退还硬币
*/
public void ejectQuarter() {
state.ejectQuarter();
}
/**
* 转动转轴
*/
public void turnCrank() {
state.turnCrank();
state.dispense();
}
void setState(State state) {
this.state = state;
}
/**
* 释放糖果并且糖果数减1;
*/
void releaseBall() {
System.out.println("a gumball comes rolling out the slot");
if (count != 0) {
count = count - 1;
}
}
public State getHasQuarterState() {
return hasQuarterState;
}
public void setHasQuarterState(State hasQuarterState) {
this.hasQuarterState = hasQuarterState;
}
public State getNoQuarterState() {
return noQuarterState;
}
public void setNoQuarterState(State noQuarterState) {
this.noQuarterState = noQuarterState;
}
public State getSoldOutState() {
return soldOutState;
}
public void setSoldOutState(State soldOutState) {
this.soldOutState = soldOutState;
}
public State getSoldState() {
return soldState;
}
public void setSoldState(State soldState) {
this.soldState = soldState;
}
public State getWinnerState() {
return winnerState;
}
public void setWinnerState(State winnerState) {
this.winnerState = winnerState;
}
public State getState() {
return state;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
}
HasQuarterState类
package com.mouse.design.state;
import java.util.Random;
/**
* Created by wuhao on 2017/2/7.
*/
public class HasQuarterState implements State {
GumballMachine gumballMachine;
public HasQuarterState(GumballMachine gumballMachine) {
this.gumballMachine = gumballMachine;
}
@Override
public void insertQuarter() {
System.out.println("你不能重复投币");
}
@Override
public void ejectQuarter() {
System.out.println("退还投入的硬币!!!");
gumballMachine.setState(gumballMachine.getNoQuarterState());
}
@Override
public void turnCrank() {
System.out.println("you turn...");
int win = new Random().nextInt(10);
System.out.println("win--->"+win);
if( (0 == win) && gumballMachine.getCount() > 0 ){
gumballMachine.setState(gumballMachine.getWinnerState());
}else{
gumballMachine.setState(gumballMachine.getSoldState());
}
}
@Override
public void dispense() {
System.out.println("No gumball dispensed...");
}
}
client.java类
package com.mouse.design.state;
/**
* Created by wuhao on 2017/2/7.
*/
public class Client {
public static void main(String[] args) {
GumballMachine gumballMachine = new GumballMachine(5);
System.out.println("---------1--------");
gumballMachine.insertQuarter();
gumballMachine.turnCrank();
System.out.println("--------2-----");
gumballMachine.insertQuarter();
gumballMachine.turnCrank();
System.out.println("------3-------");
gumballMachine.insertQuarter();
gumballMachine.turnCrank();
System.out.println("------4-------");
gumballMachine.insertQuarter();
gumballMachine.turnCrank();
System.out.println("------5-------");
gumballMachine.insertQuarter();
gumballMachine.turnCrank();
System.out.println("------6-------");
gumballMachine.insertQuarter();
gumballMachine.turnCrank();
}
}
3.3 代码
代码已经上传到github上了,之前一直用bitbucket,不过现在把一些代码弄到github上。
https://github.com/MahoneWu/mouse-java-git.git
四:思考
最近看那个规范,觉得对于我来说还是有很大很大帮助的,其中的一些东西注意项,有说到需要注意的设计模式,比如"模板设计模式",这篇写的"状态设计模式",其实设计模式在思维上是很特别的,写法也是。所以状态设计模式一般用于"状态的变化并且业务复杂的"。
近期了把去年没写完的几篇文章陆续发了,还有很早写的两篇比较简单的设计模式的文章。都是简单的记载下了。并且所有的测试代码都会放到github上面了。
五:浩语
__
__ _ ____ __| |__ _____ ___
\ \/ \/ / | \ | \\__ \ / _ \
\ /| | / Y \/ __ \( <_> )
\/\_/ |____/|___| (____ /\____/
\/ \/
任何事情都是要靠努力和用心。