重构11-Switch to Strategy(Switch到策略模式)
重构没有固定的形式,多年来我使用过不同的版本,并且我敢打赌不同的人也会有不同的版本。 该重构适用于这样的场景: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;
}
}
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;
}
}
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.Strategy Pattern(策略模式)
		
策略模式(Strategy Pattern): 我的理解,将代码中每个变化之处抽出,提炼成一个一个的接口或者抽象类,让这些变化实现接口或继承抽象类成为具体的变化类.再利用多态的功能,可将变化之处用接口 ...
 - Strategy Pattern(策略模式)
		
Head First定义: 策略模式定义了算法族,分别封装起来,让他们之间可以互相替换,此模式让算法的变化独立于使用算法的客户. 策略模式的设计原则主要有三个: 找出应用中可能需要变化的部分,把它们独 ...
 - 使用策略模式重构switch case 代码
		
目录 1.背景 2.案例 3.switch…case…方式实现 4.switch…case…带来的问题 5.使用策略模式重构switch…case…代码 6.总结 1.背景 之前在看<重构 ...
 - 使用反射+策略模式代替项目中大量的switch case判断
		
我这里的业务场景是根据消息类型将离线消息存入mongoDB不同的collection中.其中就涉及到大量的分支判断,为了增强代码的可读性和可维护性,对之前的代码进行了重构. 先对比一下使用反射+策略模 ...
 - C#设计模式系列:策略模式(Strategy)
		
1.策略模式简介 1.1>.定义 策略是为达到某一目的而采取的手段或方法,策略模式的本质是目标与手段的分离,手段不同而最终达成的目标一致.客户只关心目标而不在意具体的实现方法,实现方法要根据具体 ...
 - 设计模式笔记:策略模式(Strategy)
		
1. 策略模式简介 1.1 定义 策略是为达到某一目的而采取的手段或方法,策略模式的本质是目标与手段的分离,手段不同而最终达成的目标一致.客户只关心目标而不在意具体的实现方法,实现方法要根据具体的环境 ...
 - 第21章 策略模式(Strategy Pattern)
		
原文 第21章 策略模式(Strategy Pattern) 策略模式 导读:策略模式看完之后,大多数人都会感觉有点混了,包括我,感觉策略模式是一种OO思想的体现(纯属个人拙见). 概述: ...
 - 反馈法学习设计模式(一)——策略模式Strategy Pattern
		
简介(Introduction) 之前学习Java8实战时,遇到一个很好的策略模式示例.便想着借着这个示例结合反馈式的方法来,学习策略设计模式,也以便后面反复琢磨学习. 首先我们通过练习,逐步写出符合 ...
 - 策略模式(Strategy)简介
		
一.策略模式(Strategy)简介 策略模式是行为模式. 行为模式:规定了各个对象应该具备的职责以及对象间的通信模式,它很好的规范了对象间调用和数据传递方式 策略模式适合于算法经常变化的情况 算法的 ...
 - Spring中常见的设计模式——策略模式
		
策略模式(Strategy Pattern) 一.策略模式的应用场景 策略模式的应用场景如下: 系统中有很多类,而他们的区别仅仅在于行为不同. 一个系统需要动态的在集中算法中选择一种 二.用策略模式实 ...
 
随机推荐
- HDU 4284Travel(状压DP)
			
HDU 4284 Travel 有N个城市,M条边和H个这个人(PP)必须要去的城市,在每个城市里他都必须要“打工”,打工需要花费Di,可以挣到Ci,每条边有一个花费,现在求PP可不可以从起点1 ...
 - js ajax post提交 ie和火狐、谷歌提交的编码不一致,导致中文乱码
			
今天遇到一个问题找了很久发现: 使用js ajax post提交 ie和火狐.谷歌提交的编码不一致,导致中文乱码 //http://www.cnblogs.com/QGC88 $.ajax({ url ...
 - UI:字典的两种取值的区别
			
字典的两种取值的区别 (objectForKey: 和 valueForKey )参考 一般来说 key 可以是任意字符串组合,如果 key 不是以 @ 符号开头,这时候 valueForKey: 等 ...
 - C++的优秀特性1:引用
			
(转载请注明原创于潘多拉盒子) 一本典型的C语言教科书的厚度大约是200页左右,而一本典型的C++教科书的厚度至少要500页.比如K&R的<The C Programming Langu ...
 - pic/at89c2051 programmer
			
http://dangerousprototypes.com/forum/viewtopic.php?t=170 It looks like the PICKIT2 uses a small boos ...
 - centos7.2安装配置
			
VMware12上安装Centos7,如果电脑是64位,这必须选择64位的centos系统,不然安装完会找不到网卡.安装过程中应当开启网卡选项. 安装完想用ifconfig命令查找IP地址会提示错误: ...
 - BZOJ 1083: [SCOI2005]繁忙的都市 kruskal
			
1083: [SCOI2005]繁忙的都市 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=1083 Description 城市C是一个非 ...
 - CodeForces 176C Playing with Superglue 博弈论
			
Playing with Superglue 题目连接: http://codeforces.com/problemset/problem/176/C Description Two players ...
 - hdu 1257 小希的迷宫 并查集
			
小希的迷宫 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1272 D ...
 - hdu 4739 Zhuge Liang's Mines 随机化
			
Zhuge Liang's Mines Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.p ...