重构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) 一.策略模式的应用场景 策略模式的应用场景如下: 系统中有很多类,而他们的区别仅仅在于行为不同. 一个系统需要动态的在集中算法中选择一种 二.用策略模式实 ...
随机推荐
- POJ3080Blue Jeans(暴力)
开始做字符串专题,地址 第一题水题,暴力就可以做 #include <map> #include <set> #include <stack> #include & ...
- URAL 2070 Interesting Numbers (找规律)
题意:在[L, R]之间求:x是个素数,因子个数是素数,同时满足两个条件,或者同时不满足两个条件的数的个数. 析:很明显所有的素数,因数都是2,是素数,所以我们只要算不是素数但因子是素数的数目就好,然 ...
- Castle 开发系列文章
转自:http://terrylee.cnblogs.com/archive/2006/04/28/castl_ioc_article.html Castle是针对.NET平台的一个开源项目,从数据访 ...
- ps:探索按钮按起落下的技巧
(从死了一次又一次终于挂掉的百度空间中抢救出来的,发表日期 2014-07-10) 先上图: 那个看上去想按下去的,那个看上去像自然地呢? 显而易见: 第一像按下去的,第二个像自然地. 原因: 渐变: ...
- Django官方文档学习2——数据库及模板
网址:https://docs.djangoproject.com/en/1.10/intro/tutorial02/ 1.扫描installed_apps,创建需要的数据库table python ...
- 关于c#中的Timer控件的简单用法
Timer控件主要会用到2个属性一个是Enabled和IntervalEnabled主要是控制当前Timer控件是否可用timer1.Enabled=false;不可用timer1.Enabled=t ...
- Codeforces Round #306 (Div. 2) B. Preparing Olympiad dfs
B. Preparing Olympiad Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/550 ...
- DP总结 ——QPH
常见优化 单调队列 形式 dp[i]=min{f(k)} dp[i]=max{f(k)} 要求 f(k)是关于k的函数 k的范围和i有关 转移方法 维护一个单调递增(减)的队列,可以在两头弹出元素,一 ...
- [Angular-Scaled Web] 7. Refactor code into Models
In the previous code, both categories and bookmarks are binded to $rootscope, or let says the same s ...
- Linux - wc统计文件行数、单词数或字节数
一 wc简单介绍 wc命令用来打印文件的文本行数.单词数.字节数等(print the number of newlines, words, and bytes in files).在Windows的 ...