重构没有固定的形式,多年来我使用过不同的版本,并且我敢打赌不同的人也会有不同的版本。 该重构适用于这样的场景:switch语句块很大,并且会随时引入新的判断条件。这时,最好使用策略模式将每个条件封装到单独的类中。实现策略模式的方式是很多的。我在这里介绍的策略重构使用的是字典策略,这么做的好处是调用者不必修改原来的代码。

public class ClientCode {
public double CalculateShipping() {
ShippingInfo shippingInfo = new ShippingInfo();
return shippingInfo.CalculateShippingAmount(State.Alaska);
}
}
public enum State {
Alaska, NewYork, Florida
}
public class ShippingInfo {
public double CalculateShippingAmount(State shipToState) {
switch (shipToState) {
case State.Alaska:
return GetAlaskaShippingAmount();
case State.NewYork:
return GetNewYorkShippingAmount();
case State.Florida:
return GetFloridaShippingAmount();
default:
return 0d;
}
} private double GetAlaskaShippingAmount() {
return 15d;
}
private double GetNewYorkShippingAmount() {
return 10d;
}
private double GetFloridaShippingAmount() {
return 3d;
}
}
要应用该重构,需将每个测试条件至于单独的类中,这些类实现了一个共同的接口。然后将枚举作为字典的键,这样就可以获取正确的实现,并执行其代码了。以后如果希望添加新的条件,只需添加新的实现类,并将其添加至ShippingCalculations字典中。正如前面说过的,这不是实现策略模式的唯一方式。我在这里将字体加粗显示,是因为肯定会有人在评论里指出这点:)用你觉得好用的方法。我用这种方式实现重构的好处是,不用修改客户端代码。所有的修改都在ShippingInfo类内部。 
Jayme Davis指出这种重构由于仍然需要在构造函数中进行绑定,所以只不过是增加了一些类而已,但如果绑定IShippingCalculation的策略可以置于IoC中,带来的好处还是很多的,它可以使你更灵活地捆绑策略
public class ClientTest {
public Double CalculateShipping() {
ShippingInfo shippingInfo = new ShippingInfo();
return shippingInfo.CalculateShippingAmount(State.Alaska);
}
}
public enum State {
Alaska, NewYork, Florida
}
public class ShippingInfo {
private Dictionary<State, IShippingCalculation> ShippingCalculations; public ShippingInfo() {
ShippingCalculations =new Hashtable<State, IShippingCalculation>();
ShippingCalculations.put(State.Alaska, new AlaskShippingCalculation());
ShippingCalculations.put(State.NewYork, new NewYorkShippingCalculation());
ShippingCalculations.put(State.Florida, new FloridaShippingCalculation());
} public Double CalculateShippingAmount(State shipToState) {
return ShippingCalculations.get(shipToState).Calculate();
}
}
public interface IShippingCalculation {
Double Calculate();
}
public class AlaskShippingCalculation implements IShippingCalculation {
public Double Calculate () {
return 15d;
}
}
public class NewYorkShippingCalculation implements IShippingCalculation {
public Double Calculate () {
return 10d;
}
}
public class FloridaShippingCalculation implements IShippingCalculation {
public Double Calculate () {
return 3d;
}
}
为了使这个示例圆满,我们来看看在ShippingInfo构造函数中使用Ninject为IoC容器时如何进行绑定。需要更改的地方很多,主要是将state的枚举放在策略内部,以及Ninject向构造函数传递一个IShippingInfo的IEnumerable泛型。接下来我们使用策略类中的state属性创建字典,其余部分保持不变。(感谢Nate Kohari和Jayme Davis)
public interface IShippingInfo {
Double CalculateShippingAmount(State state);
} public class ClientCode {
public IShippingInfo ShippingInfo;
public Double CalculateShipping() {
return ShippingInfo.CalculateShippingAmount(State.Alaska);
}
}
public enum State{
Alaska, NewYork, Florida;
} public class ShippingInfo implements IShippingInfo{
private Dictionary<State, IShippingCalculation> ShippingCalculations=new Hashtable<State, IShippingCalculation>();
public ShippingInfo(IShippingCalculation<State> shippingCalculations) {
for(State state:State.values()) {
ShippingCalculations.put(state, shippingCalculations);
}
}
public Double CalculateShippingAmount(State shipToState) {
return ShippingCalculations.get(shipToState).Calculate();
}
} public interface IShippingCalculation<State> {
State getState();
Double Calculate();
} public class AlaskShippingCalculation implements IShippingCalculation {
@Override
public State getState() {
return State.Alaska;
}
@Override
public Double Calculate() {
return 15d;
}
} public class NewYorkShippingCalculation implements IShippingCalculation {
@Override
public State getState() {
return State.NewYork;
}
@Override
public Double Calculate() {
return 10d;
}
} public class FloridaShippingCalculation implements IShippingCalculation {
@Override
public State getState() {
return State.Florida;
}
@Override
public Double Calculate() {
return 3d;
}
}
 
 
 

重构11-Switch to Strategy(Switch到策略模式)的更多相关文章

  1. 1.Strategy Pattern(策略模式)

    策略模式(Strategy Pattern): 我的理解,将代码中每个变化之处抽出,提炼成一个一个的接口或者抽象类,让这些变化实现接口或继承抽象类成为具体的变化类.再利用多态的功能,可将变化之处用接口 ...

  2. Strategy Pattern(策略模式)

    Head First定义: 策略模式定义了算法族,分别封装起来,让他们之间可以互相替换,此模式让算法的变化独立于使用算法的客户. 策略模式的设计原则主要有三个: 找出应用中可能需要变化的部分,把它们独 ...

  3. 使用策略模式重构switch case 代码

    目录 1.背景 2.案例 3.switch…case…方式实现 4.switch…case…带来的问题 5.使用策略模式重构switch…case…代码 6.总结 1.背景 之前在看<重构    ...

  4. 使用反射+策略模式代替项目中大量的switch case判断

    我这里的业务场景是根据消息类型将离线消息存入mongoDB不同的collection中.其中就涉及到大量的分支判断,为了增强代码的可读性和可维护性,对之前的代码进行了重构. 先对比一下使用反射+策略模 ...

  5. C#设计模式系列:策略模式(Strategy)

    1.策略模式简介 1.1>.定义 策略是为达到某一目的而采取的手段或方法,策略模式的本质是目标与手段的分离,手段不同而最终达成的目标一致.客户只关心目标而不在意具体的实现方法,实现方法要根据具体 ...

  6. 设计模式笔记:策略模式(Strategy)

    1. 策略模式简介 1.1 定义 策略是为达到某一目的而采取的手段或方法,策略模式的本质是目标与手段的分离,手段不同而最终达成的目标一致.客户只关心目标而不在意具体的实现方法,实现方法要根据具体的环境 ...

  7. 第21章 策略模式(Strategy Pattern)

    原文 第21章 策略模式(Strategy Pattern) 策略模式 导读:策略模式看完之后,大多数人都会感觉有点混了,包括我,感觉策略模式是一种OO思想的体现(纯属个人拙见). 概述:       ...

  8. 反馈法学习设计模式(一)——策略模式Strategy Pattern

    简介(Introduction) 之前学习Java8实战时,遇到一个很好的策略模式示例.便想着借着这个示例结合反馈式的方法来,学习策略设计模式,也以便后面反复琢磨学习. 首先我们通过练习,逐步写出符合 ...

  9. 策略模式(Strategy)简介

    一.策略模式(Strategy)简介 策略模式是行为模式. 行为模式:规定了各个对象应该具备的职责以及对象间的通信模式,它很好的规范了对象间调用和数据传递方式 策略模式适合于算法经常变化的情况 算法的 ...

  10. Spring中常见的设计模式——策略模式

    策略模式(Strategy Pattern) 一.策略模式的应用场景 策略模式的应用场景如下: 系统中有很多类,而他们的区别仅仅在于行为不同. 一个系统需要动态的在集中算法中选择一种 二.用策略模式实 ...

随机推荐

  1. C++拓扑排序

    安利一篇比较写的比较好的的博客... 拓扑排序的原理及其实现 我本来以为我看懂了原理就会打了,没想到因为没有手动实践过...原理实际上也没记清楚.... 一题HDU的拓扑裸题HDU 3342 我的拓扑 ...

  2. web.xml 详解

    http://xmlns.jcp.org/xml/ns/javaee 重定向为 http://www.oracle.com/webfolder/technetwork/jsc/xml/ns/javae ...

  3. Oracle数据库编程:使用PL/SQL编写触发器

    8.使用PL/SQL编写触发器: 触发器存放在数据缓冲区中.        触发器加序列能够实现自动增长.        在触发器中不能使用connit和rollback.        DML触发器 ...

  4. PB学习笔记(一)

    前言:我绝对很痛恨PB.1.没人带2.自己摸索3.头发掉了4.老大不停的给任务5.这语言老的不行了6,代码可读性不是一般的差 我绝对很喜欢PB.1.自我学习成功后那种成就感2.老大也会帮给我看看,指点 ...

  5. magento目录结构精编版

    1 /app – 程序根目录 2 3 /app/etc – 全局配置文件目录 4 5 /app/code – 所有模块安装其模型和控制器的目录 6 7 /app/code/core – 核心代码或经过 ...

  6. C#和JavaScript的区别

    Strong and Loose Typing: 强弱比较 // C# var customer = new Customer(); //var is compiler inferred //Java ...

  7. 转载:CSS3 圆角(border-radius)

    前缀 例1 例2:无边框 书写顺序 其它 支持性 值:半径的长度 前缀 -moz(例如 -moz-border-radius)用于Firefox -webkit(例如:-webkit-border-r ...

  8. Fom同时控制每一行不同的状态

    代码:app_item_property.set_property('HEADER.MATTER_CODE', alterable,property_off);   实现效果:             ...

  9. webstorm 主题设置 皮肤设置

    推荐个编辑器主题下载的一个网站. Color Themes    网址:http://color-themes.com [点这里直接跳转] 但是,只支持几个编辑器. 各种颜色搭配的主题,随你选择!我个 ...

  10. AVR JTAG MKii 引脚布局 ( JTAG 和 ISP )

    1,JTAG和ISP引脚复用,JTAG的TCK,TDO,TDI分别与ISP的SCK,MISO,MOSI复用:2,VT引脚必须接目标板电源,不然仿真器接口电路不工作: JTAG接口引脚定义如下 标配的J ...