【转】Java重构-策略模式、状态模式、卫语句

前言

当代码中出现多重if-else语句或者switch语句时。弊端之一:如果这样的代码出现在多处,那么一旦出现需求变更,就需要把所有地方的if-else或者switch代码进行更改,要是遗漏了某一处,那么程序就会出错。弊端之二:代码逻辑难以理解。

卫语句

卫语句的使用,并不能改变前言说的弊端:一旦需求更改,需要修改所有使用更改需求的if-else代码块。不过使用卫语句可以让自己或者代码维护人员很容易的了解代码的含义。

代替异常分支

所谓的异常分支就是普通情况之外的分支,这种情况的下的if-else可以用

if (condition)
    return obj;

代替。

代替多重if-else分支

这个情况的代替方式是可以为

if (isSunshine()) {
    // 晴天时处理逻辑
}
if (isRain()) {
    // 下雨时处理逻辑
}
if (isOvercast()) {
    // 阴天时处理逻辑
}
...

策略模式

使用策略模式可以代替多重if-elseswitch语句,让代码维护变得更加简单。 
策略模式UML: 
这里写图片描述

  • 环境(Context)角色:持有一个Strategy的引用
  • 抽象策略(Strategy)角色:这是一个抽象角色,通常由一个接口或抽象类实现
  • 具体策略(ConcreteStrategy)角色:包装了相关的算法或行为

策略模式代码模板:

package xyz.zeling.test.strategy.template;

import xyz.zeling.test.strategy.template.base.Strategy;

/**
 * @description 环境角色
 * @author zeling * @date 2018年1月14日 下午8:43:58 */ public class Context { /** * 策略对象 */ private Strategy strategy; /** * @param strategy 具体策略对象 */ public Context(Strategy strategy) { this.strategy = strategy; } /** * @description 执行策略方法 * @date 2018年1月14日 下午8:43:31 */ public void contextInterface() { strategy.strategyInterface(); } }
package xyz.zeling.test.strategy.template.base;

/**
 * @description 抽象策略角色
 * @author zeling
 * @date 2018年1月14日 下午8:41:14 */ public interface Strategy { /** * @description 策略方法 * @date 2018年1月14日 下午8:41:00 */ void strategyInterface(); }
package xyz.zeling.test.strategy.template;

import xyz.zeling.test.strategy.template.base.Strategy;

/**
 * @description 具体策略类A
 * @author zeling * @date 2018年1月14日 下午8:45:00 */ public class ConcreteStrategyA implements Strategy { @Override public void strategyInterface() { // TODO Auto-generated method stub } } 
package xyz.zeling.test.strategy.template;

import xyz.zeling.test.strategy.template.base.Strategy;

/**
 * @description 具体策略类B
 * @author zeling * @date 2018年1月14日 下午8:45:00 */ public class ConcreteStrategyB implements Strategy { @Override public void strategyInterface() { // TODO Auto-generated method stub } }
package xyz.zeling.test.strategy.template;

import xyz.zeling.test.strategy.template.base.Strategy;

/**
 * @description 具体策略类C
 * @author zeling * @date 2018年1月14日 下午8:45:00 */ public class ConcreteStrategyC implements Strategy { @Override public void strategyInterface() { // TODO Auto-generated method stub } } 

实例应用:水果有不同种类,每种水果对应不同价格,用策略模式实现。

package xyz.zeling.test.strategy.demo.base;

/**
 * @description 抽象策略,水果接口
 * @author zeling
 * @date 2018年1月14日 下午3:01:20 */ public interface Fruit { /** * @description 输出对应价格 * @date 2018年1月14日 下午3:03:07 */ void price(); } 
package xyz.zeling.test.strategy.demo;

import xyz.zeling.test.strategy.demo.base.Fruit;

/**
 * @description 具体策略,苹果类
 * @author zeling * @date 2018年1月14日 下午3:04:17 */ public class Apple implements Fruit { @Override public void price() { System.out.println("苹果的价格!"); } } 
package xyz.zeling.test.strategy.demo;

import xyz.zeling.test.strategy.demo.base.Fruit;

/**
 * @description 具体策略,香蕉
 * @author zeling * @date 2018年1月14日 下午3:06:52 */ public class Banana implements Fruit { @Override public void price() { System.out.println("香蕉的价格!"); } }
package xyz.zeling.test.strategy.demo;

import xyz.zeling.test.strategy.demo.base.Fruit;

/**
 * @description 具体策略,梨
 * @author zeling * @date 2018年1月14日 下午3:08:19 */ public class Pear implements Fruit { @Override public void price() { System.out.println("梨的价格!"); } } 
package xyz.zeling.test.strategy.demo;

import xyz.zeling.test.strategy.demo.base.Fruit;

/**
 * @description 策略环境,输出水果价格
 * @author zeling * @date 2018年1月14日 下午3:09:31 */ public class FruitPrice { /** * 策略对象 */ private Fruit fruit; /** * @param fruit 策略对象 */ public FruitPrice(Fruit fruit) { this.fruit = fruit; } /** * @description 输出水果价格 * @date 2018年1月14日 下午3:12:26 */ public void printFruitPrice() { fruit.price(); } }

客户端

    /**
     * @description 使用策略模式:针对一组算法,将每一个算法封装到具有共同接口的独立的类
     * @date 2018年1月14日 下午3:17:11
     */
    public static void useStrategy() { // 具体使用策略 Fruit apple = new Apple(); // 将策略放入环境中并执行策略 new FruitPrice(apple).printFruitPrice(); }

状态模式

状态模式类图: 
这里写图片描述
说明:

  • 环境(Context)角色,也成上下文:定义客户端所感兴趣的接口,并且保留一个具体状态类的实例。这个具体状态类的实例给出此环境对象的现有状态
  • 抽象状态(State)角色:定义一个接口,用以封装环境(Context)对象的一个特定的状态所对应的行为
  • 具体状态(ConcreteState)角色:每一个具体状态类都实现了环境(Context)的一个状态所对应的行为

策略模式代码模板:

package xyz.zeling.test.state.template.base;

/**
 * @description 抽象状态角色
 * @author zeling
 * @date 2018年1月14日 下午8:41:14 */ public interface State { /** * @description 处理方法 * @date 2018年1月14日 下午8:41:00 */ void handle(); }
package xyz.zeling.test.state.template;

import xyz.zeling.test.state.template.base.State;

/**
 * @description 具体状态类A
 * @author zeling * @date 2018年1月14日 下午8:45:00 */ public class ConcreteStateA implements State { @Override public void handle() { // TODO Auto-generated method stub } } 
package xyz.zeling.test.state.template;

import xyz.zeling.test.state.template.base.State;

/**
 * @description 具体状态类A
 * @author zeling * @date 2018年1月14日 下午8:45:00 */ public class ConcreteStateB implements State { @Override public void handle() { // TODO Auto-generated method stub } } 
package xyz.zeling.test.state.template;

import xyz.zeling.test.state.template.base.State;

/**
 * @description 具体状态类A
 * @author zeling * @date 2018年1月14日 下午8:45:00 */ public class ConcreteStateC implements State { @Override public void handle() { // TODO Auto-generated method stub } } 
package xyz.zeling.test.state.template;

import xyz.zeling.test.state.template.base.State;

/**
 * @description 状态模式,环境角色类
 * @author zeling * @date 2018年1月14日 下午8:43:58 */ public class Context { /** * 状态对象 */ private State state; /** * @description 设置状态 * @date 2018年1月14日 下午9:13:20 * @param state 具体状态 */ public void setState(State state) { this.state = state; } /** * @description 执行策略方法 * @date 2018年1月14日 下午8:43:31 */ public void request() { state.handle(); } }

实例应用:橘子有不同颜色,状态不同的橘子颜色不同,用状态模式实现。

package xyz.zeling.test.state.demo.base;

/**
 * @description 状态模式,状态接口
 * @author zeling
 * @date 2018年1月14日 下午4:13:21 */ public interface Orange { /** * @description 输出橘子的颜色 * @date 2018年1月14日 下午4:14:00 */ void printColor(); } 
package xyz.zeling.test.state.demo;

import xyz.zeling.test.state.demo.base.Orange;

/**
 * @description 状态2:青色的橘子
 * @author zeling * @date 2018年1月14日 下午4:17:52 */ public class CyanOrange implements Orange { @Override public void printColor() { System.out.println("My color is cyan!"); } } 
package xyz.zeling.test.state.demo;

import xyz.zeling.test.state.demo.base.Orange;

/**
 * @description 橘子状态管理器
 * @author zeling * @date 2018年1月14日 下午4:19:55 */ public class OrangeStateManage { /** * 橘子的状态 */ private Orange state; /** * @description 设置橘子的状态 * @date 2018年1月14日 下午4:21:36 * @param state */ public void setState(Orange state) { this.state = state; } /** * @description 输出当前状态的橘子颜色 * @date 2018年1月14日 下午4:22:08 */ public void print() { state.printColor(); } } 
package xyz.zeling.test.state.demo;

import xyz.zeling.test.state.demo.base.Orange;

/**
 * @description 状态1:红色外观的橘子
 * @author zeling * @date 2018年1月14日 下午4:15:29 */ public class RedOrange implements Orange { @Override public void printColor() { System.out.println("My color is red!"); } }
package xyz.zeling.test.state.demo;

import xyz.zeling.test.state.demo.base.Orange;

/**
 * @description 状态3:黄色的橘子
 * @author zeling * @date 2018年1月14日 下午4:18:45 */ public class YellowOrange implements Orange { @Override public void printColor() { System.out.println("My color is yellow!"); } }

客户端

    /**
     * @description 使用状态模式:状态模式,又称状态对象模式(Pattern of Objects for
     *              States),状态模式是对象的行为模式。状态模式允许一个对象在其内部状态改变的时候改变其行为。这个对象看上去就像是改变了它的类一样
     * @date 2018年1月14日 下午4:04:16
     */
    public static void useState() { // 具体使用状态 Orange cyanOrange = new CyanOrange(); // 创建环境 OrangeStateManage orangeStateManage = new OrangeStateManage(); // 设置状态并执行 orangeStateManage.setState(cyanOrange); orangeStateManage.print(); }

策略模式和状态模式的比较

讲真,我觉得它们都差不多啊,好难区别啊!!! 
不过,虽然讲不出来它们的区别是什么,但是有个例子可以很好的描述它们的区别

  • 状态模式:这个模式就好比员工申请离职单的流程,离职单到直接上级,这个状态就是直接上级批示,等直接上级审阅之后,通过了就到下一个状态。这一个个状态对应不同的处理,这是有顺序要求的。
  • 策略模式:这个模式好比于你假期要出国游玩,有日本、美国、新加坡等国家,你每到一个国家就执行不同的游玩策略,可以先去日本,也可以先去美国,没有顺序要求。

demo

github地址:https://github.com/zeling1005/state-and-strategy.git

猜你喜欢

转载自www.cnblogs.com/didiaoxiong/p/9125940.html