Refactoring - replace Switch statement with state/strategy
来源:互联网 发布:阿里云 宕机 编辑:程序博客网 时间:2024/06/07 01:16
/*
Author: Jiangong SUN
*/
switch statements is procedural structure, not object programming, it can be replaced by strategy pattern with implementation of interface/abstract strategy, with concrete strategies.
public class SwitchToReplace { public static void Main() { Console.WriteLine(Get(Instruments.Telephone)); Console.WriteLine(Get(Instruments.Mobile)); Console.WriteLine(Get(Instruments.Television)); Console.WriteLine(Get(Instruments.Guitar)); Console.ReadLine(); } public enum Instruments { Telephone, Mobile, Television, Guitar } public static string Get(Instruments instruments) { string value; switch (instruments) { case Instruments.Telephone: Console.WriteLine("Case 1"); value = "Telephone"; break; case Instruments.Mobile: Console.WriteLine("Case 2"); value = "Mobile"; break; case Instruments.Television: Console.WriteLine("Case 3"); value = "Television"; break; default: Console.WriteLine("Default case"); value = "Default case"; break; } return value; } }
Replace Switch statement with State/Strategy pattern
public class SwitchReplaced { public enum InstrumentEnum { Telephone, Mobile, Television, Guitar } //define an abstract class and its method signature public abstract class Instrument { public abstract string GetInstrumentName(); } //inherit abstract class and override it's method public class InstrumentTelephone : Instrument { public override string GetInstrumentName() { Console.WriteLine("Case 1"); return "Telephone"; } } public class InstrumentMobile : Instrument { public override string GetInstrumentName() { Console.WriteLine("Case 2"); return "Mobile"; } } public class InstrumentTelevision : Instrument { public override string GetInstrumentName() { Console.WriteLine("Case 3"); return "Television"; } } public class InstrumentGuitar : Instrument { public override string GetInstrumentName() { Console.WriteLine("Case 4"); return "Guitar"; } } public class InstrumentContext { private readonly Instrument _instrumentInstance; //put abstract class Instrument's instance in constructor, assign it to local variable public InstrumentContext(Instrument instrument) { _instrumentInstance = instrument; } //Invoke method of concrete strategy public string InvokeGetInstrumentName() { return _instrumentInstance.GetInstrumentName(); } } //Extract same calling code in a single method public string InstrumentInfo(Instrument instrument) { InstrumentContext context = new InstrumentContext(instrument); return context.InvokeGetInstrumentName(); } public static void Main() { SwitchReplaced sr = new SwitchReplaced(); //call Console.WriteLine(sr.InstrumentInfo(new InstrumentGuitar())); Console.WriteLine(sr.InstrumentInfo(new InstrumentMobile())); Console.ReadLine(); } }
I hope this post can do help to you! enjoy coding!
- Refactoring - replace Switch statement with state/strategy
- Refactoring a Switch Statement
- Replace Type Code with State/Strategy(以State/Strategy取代型别码)
- 8.15 replace type code with state/strategy (以state/strategy取代类型码)
- 重构之4.Replace Type Code with State/Strategy(以State/Strategy取代类型码)
- 重新组织数据之十五 :Replace Type Code with State/Strategy(以State/strategy 取代型别码)
- Refactoring Day 8 : Replace Inheritance with Delegation
- Refactoring Day 31 : Replace conditional with Polymorphism
- refactoring笔记——代码重构的方法:Replace Temp with Query
- Refactoring with Solr
- Refactoring Mongoose with Q
- state 和 statement
- MySQL replace statement
- with statement
- Using the Switch Statement
- Refactoring Game Entities with Components
- The switch statement of JavaScript
- State模式与Strategy模式的区别
- 3种get请求方式
- delphi 更改DBGrid 颜色技巧
- 每日一C-- 信号量结构体 利用信号实现进程间通信 初始化调用杀死
- CountDownTimer in Android
- 黑马程序员--java高新技术 25--枚举,反射
- Refactoring - replace Switch statement with state/strategy
- what a fucking day
- 3367Pseudoforest 伪森林 最小生成树
- 基于QCGP(QuadraticGW&PR)Combining 算法的白平衡处理程序
- ObjC学习补充
- linux调节亮度
- 30 Things I did After Installing Ubuntu 13.04 Raring Ringtail
- 25 Effective Website Navigation Designs
- 2013年6月5日凌晨