这里列举行为型模式・到此23种就列完了・・・这里是看着菜鸟教程来实现・・,他里边列了25种,其中过滤器模式和空对象模式应该不属于所谓的23种模式
责任链模式:为请求创建一个接收者对象的链,对请求的发送者和接收者进行解耦,大部分用于web中吧。。
Task中的continuewith和微软的tpl数据流应该是类似这种模式的实现吧
using System;using System.Collections.Generic;using System.linq;using System.Text;using System.Threading.Tasks;//责任链模式namespace ExercisePrj.Dsignmode{ public abstract class AbstractLogger { public static int INFO = 1; public static int DEBUG = 2; public static int ERROR = 3; protected int level; //责任链中的下一个对象 protected AbstractLogger nextLogger; public voID SetNextLogger(AbstractLogger next) { nextLogger = next; } public voID LogMessage(int level,string message) { if(this.level<=level) { Write(message); } if(nextLogger!=null) { nextLogger.LogMessage(level,message); } } protected abstract voID Write(string message); } public class ConsoleLogger : AbstractLogger { public ConsoleLogger(int level) { this.level = level; } protected overrIDe voID Write(string message) { Console.Writeline("Standard Console::Logger: " + message); } } public class ErrorLogger : AbstractLogger { public ErrorLogger(int level) { this.level = level; } protected overrIDe voID Write(String message) { Console.Writeline("Error Console::Logger: " + message); } } public class fileLogger : AbstractLogger { public fileLogger(int level) { this.level = level; } protected overrIDe voID Write(String message) { Console.Writeline("file::Logger: " + message); } }}
命令模式(Command Pattern):请求以命令的形式执行,CAD的的命令应该就是以这种方式执行的・二次开发的时候通过特性标识和继承他的接口来添加命令,非常方便
using System;using System.Collections.Generic;using System.linq;using System.Text;using System.Threading.Tasks;//命令模式namespace ExercisePrj.Dsignmode{ public interface IOrder { voID Execute(); } public class Stock { private string name = "ABC"; private int quantity = 10; public voID Buy() { Console.Writeline("Stock name:{0},quantity:{1},bought",name,quantity); } public voID Sell() { Console.Writeline("Stock name:{0},quantity:{1}sold",quantity); } } //请求类 public class BuyStock : IOrder { private Stock abcStock; public BuyStock(Stock abcStock) { this.abcStock = abcStock; } public voID Execute() { abcStock.Buy(); } } //继承接口的实体 public class SellStock : IOrder { private Stock abcStock; public SellStock(Stock abcStock) { this.abcStock = abcStock; } public voID Execute() { abcStock.Sell(); } } //命令调用类 public class broker { private List<IOrder> orderList = new List<IOrder>(); public voID takeOrder(IOrder order) { orderList.Add(order); } public voID placeOrders() { foreach (IOrder order in orderList) { order.Execute(); } orderList.Clear(); } }}
解释器模式:就是实现一种表达式接口,C#的各种表达式就是这种实现吧・・这玩意跟富文本编辑器一样是个大坑吧・・,做好了确实很好使,一不小心就得跪
using System;using System.Collections.Generic;using System.linq;using System.Text;using System.Threading.Tasks;//解释器模式namespace ExercisePrj.Dsignmode{ public interface Expression { bool Interpret(string context); } public class TerminalExpression : Expression { private string data; public TerminalExpression(string data) { this.data = data; } public bool Interpret(string context) { if (context.Contains(data)) { return true; } return false; } } public class OrExpression : Expression { private Expression expr1 = null; private Expression expr2 = null; public OrExpression(Expression expr1,Expression expr2) { this.expr1 = expr1; this.expr2 = expr2; } public bool Interpret(String context) { return expr1.Interpret(context) || expr2.Interpret(context); } } public class AndExpression : Expression { private Expression expr1 = null; private Expression expr2 = null; public AndExpression(Expression expr1,Expression expr2) { this.expr1 = expr1; this.expr2 = expr2; } public bool Interpret(String context) { return expr1.Interpret(context) && expr2.Interpret(context); } }}
迭代器模式(Iterator Pattern):.NET自带接口・・・,直接实现就行了・・注意又泛型接口和非泛型接口・・非泛型接口迭代对象返回的是object,泛型接口返回的直接就是对象了,还有通过yIEld的简化写法不用额外去实现IEnumerator接口
using System;using System.Collections;using System.Collections.Generic;using System.linq;using System.Text;using System.Threading.Tasks;namespace ExercisePrj.Dsignmode{ public class IteratorEx : IEnumerable //<IteratorEx> { public string name; private List<IteratorEx> List = new List<IteratorEx>(); //public IEnumerator<IteratorEx> GetEnumerator() //{ // foreach (var l in List) // { // yIEld return l; // } //} public voID SetList(List<IteratorEx> data) { List = data; } IEnumerator IEnumerable.GetEnumerator() { foreach (var l in List) { yIEld return l; } //return new IteratorExEnum(List.ToArray()); } } public class IteratorExEnum : IEnumerator { private IteratorEx[] List; private int position = -1; public IteratorExEnum(IteratorEx[] data) { List = data; } public object Current { get { try { return List[position]; } catch (IndexOutOfRangeException) { throw new InvalIDOperationException(); } } } public bool MoveNext() { position++; return position < List.Length; } public voID reset() { position = -1; } }}
中介者模式(Mediator Pattern):用一个中介对象封装一些对象的交互,中介者使对象不用显式的互相引用,MVC和mvp 的c和p都是类似这玩意的实现吧
using System;using System.Collections.Generic;using System.linq;using System.Text;using System.Threading.Tasks;namespace ExercisePrj.Dsignmode{ //中介类 public class ChatRoom { public static voID ShowMessage(User user,string msg) { Console.Writeline(new DateTime().ToString()+"["+ user.name + "] : " + msg); } } public class User { public string name { get; set; } public User(string name) { name = name; } public voID SendMessage(String message) { ChatRoom.ShowMessage(this,message); } }}
备忘录模式(Memento Pattern):在不破坏封装的前提下,捕获一个对象的内部状态,并在对象之外保存,
大部分支持回退的 *** 作场景下应该都是这种模式・・之前做的软件中有画图的 *** 作・・・支持后退,实现方式非常简单粗暴・・,直接吧图层的画图对象克隆一份保存・・只支持5还是10步,讲道理这么实现确实有点那啥了・・・
using System;using System.Collections.Generic;using System.linq;using System.Text;using System.Threading.Tasks;namespace ExercisePrj.Dsignmode{ public class Memento { public string State { get; } public Memento(string state) { State = state; } } public class Originator { public string State { get; set; } public Memento SaveStatetoMemento() { return new Memento(State); } public voID GetStateFromMemento(Memento Memento) { State = Memento.State; } } public class CareTaker { private List<Memento> mementoList = new List<Memento>(); public voID Add(Memento state) { mementoList.Add(state); } public Memento Get(int index) { return mementoList[index]; } }}
观察者模式(Observer Pattern):.net自带的有接口提供来实现观察者模式・・・这里照着msdn来实现一遍,自带的接口里边还实现了资源的释放・・,之前并发编程里边的rx也是这个模式的具体实现・
using System;using System.Collections.Generic;using System.linq;using System.Text;using System.Threading.Tasks;//观察者模式namespace ExercisePrj.Dsignmode{ public class Subject: IObservable<Subject> { public int State {get; set;} public Subject(int state) { State = state; } private List<IObserver<Subject>> observers = new List<IObserver<Subject>>(); public Idisposable Subscribe(IObserver<Subject> observer) { if (!observers.Contains(observer)) observers.Add(observer); return new Unsubscriber(observers,observer); } private class Unsubscriber : Idisposable { private List<IObserver<Subject>> _observers; private IObserver<Subject> _observer; public Unsubscriber(List<IObserver<Subject>> observers,IObserver<Subject> observer) { this._observers = observers; this._observer = observer; } public voID dispose() { if (_observer != null && _observers.Contains(_observer)) _observers.Remove(_observer); } } public voID TrackLocation(Subject ob) { Console.Writeline("start"); foreach (var observer in observers) { if (ob==null) observer.OnError(new Exception("unkNowExeption")); else observer.OnNext(ob); } } public voID EndTransmission() { foreach (var observer in observers.ToArray()) if (observers.Contains(observer)) observer.OnCompleted(); observers.Clear(); } } public class BinaryObserver : IObserver<Subject> { public voID OnCompleted() { Console.Writeline("complete"); } public voID OnError(Exception error) { Console.Writeline(error.Message); } public voID OnNext(Subject value) { Console.Writeline("Binary String: " + Convert.ToString(value.State,2)); } } public class OctalObserver : IObserver<Subject> { public voID OnCompleted() { Console.Writeline("complete"); } public voID OnError(Exception error) { Console.Writeline(error.Message); } public voID OnNext(Subject value) { Console.Writeline("Octal String: " + Convert.ToString(value.State,8)); } } public class HexaObserver : IObserver<Subject> { public voID OnCompleted() { Console.Writeline("complete"); } public voID OnError(Exception error) { Console.Writeline(error.Message); } public voID OnNext(Subject value) { Console.Writeline("Hex String: " + Convert.ToString(value.State,16)); } }}
状态模式(State Pattern):当对象内部状态发生改变时,行为也跟着改变
这个模式是为了解决类里边的大量if和swicth语句,讲道理例子写的有点怪・・・主体是context
using System;using System.Collections.Generic;using System.linq;using System.Text;using System.Threading.Tasks;namespace ExercisePrj.Dsignmode{ public class Context { public State State { get; set; } public Context() { State = null; } } public interface State { voID DoAction(Context context); } public class StartState : State { public voID DoAction(Context context) { Console.Writeline("Player is in start state"); context.State = this; } public overrIDe string ToString() { return "Start State"; } } public class StopState : State { public voID DoAction(Context context) { Console.Writeline("Player is in stop state"); context.State = this; } public overrIDe string ToString() { return "Stop State"; } }}
空对象模式(Null Object Pattern):就是吧对空值的判断定义一个啥也不做的实体对象出来・・C#的Nullable就是这个的实现・・・这玩意不在23种设计模式里边・・・
using System;using System.Collections.Generic;using System.linq;using System.Text;using System.Threading.Tasks;namespace ExercisePrj.Dsignmode{ public abstract class AbstractCustomer { public abstract bool IsNull(); public abstract string name { get; } } public class RealCustomer : AbstractCustomer { public overrIDe string name { get; } public RealCustomer(string name) { name = name; } public overrIDe bool IsNull() { return false; } } public class NullCustomer : AbstractCustomer { public overrIDe string name { get { return "Not Available in Customer Database"; } } public overrIDe bool IsNull() { return true; } } public class CustomerFactory { public static string[] names = {"Rob","Joe","JulIE"}; public static AbstractCustomer getCustomer(string name) { if(names.Contains(name)) { return new RealCustomer(name); } return new NullCustomer(); } }}
策略模式(Strategy Pattern):定义一系列算法,封装成类,可以相互替换,通过构造不同的类,执行不同的 *** 作。这样做方便调用,添加新的算法也方便,
最后加了自己之前对这个模式的奇葩写法
using System;using System.Collections.Generic;using System.linq;using System.Reflection;using System.Text;using System.Threading.Tasks;namespace ExercisePrj.Dsignmode{ public interface IStrategy { int DoOperation(int num1,int num2); } public class OperationAdd : IStrategy { public int DoOperation(int num1,int num2) { return num1 + num2; } } public class OperationSubstract : IStrategy { public int DoOperation(int num1,int num2) { return num1 - num2; } } public class OperationMultiply : IStrategy { public int DoOperation(int num1,int num2) { return num1 * num2; } } public class ContextEx { private IStrategy strategy; public ContextEx(IStrategy strategy) { this.strategy = strategy; } public int ExecuteStrategy(int num1,int num2) { return strategy.DoOperation(num1,num2); } //奇葩的写法简单粗暴 private Dictionary<string,Func<int,int,int>> funcs = new Dictionary<string,int>>(); public int ExecuteStrategy(string name,int num1,int num2) { if(funcs.Count==0) { //反射写法 var assembly = Assembly.GetExecutingAssembly(); var types = assembly.GetTypes(); foreach (var t in types) { if (t.GetInterface("IStrategy") != null) { var instance = assembly.CreateInstance(t.Fullname) as IStrategy; funcs.Add(t.name,instance.DoOperation); } } //直接添加 //funcs.Add("OperationAdd",new Func<int,int>((n1,n2) => { return n1 + n2; })); //funcs.Add("OperationSubstract",n2) => { return n1 - n2; })); //funcs.Add("OperationMultiply",n2) => { return n1 * n2; })); } return funcs[name](num1,num2); } }}
模板模式(Template Pattern):.net的泛型就是这个模式的实现吧・・照着模子写就行了
using System;using System.Collections.Generic;using System.linq;using System.Text;using System.Threading.Tasks;namespace ExercisePrj.Dsignmode{ public abstract class Game { public abstract voID Initialize(); public abstract voID StartPlay(); public abstract voID Endplay(); //模板 public voID play() { //初始化游戏 Initialize(); //开始游戏 StartPlay(); //结束游戏 Endplay(); } } public class Cricket : Game { public overrIDe voID Endplay() { Console.Writeline("Cricket Game Finished!"); } public overrIDe voID Initialize() { Console.Writeline("Cricket Game Initialized! Start playing."); } public overrIDe voID StartPlay() { Console.Writeline("Cricket Game Started. Enjoy the game!"); } }}
访问者模式(Visitor Pattern):在被访问的类里边加一个对外提供接待访问的接口
把数据结构和对应的 *** 作分开・添加 *** 作很容易,但是如果结构变化多的化,改起来就麻烦了・・
没研究没用过・・・・
using System;using System.Collections.Generic;using System.linq;using System.Text;using System.Threading.Tasks;namespace ExercisePrj.Dsignmode{ public interface IComputerPartVisitor { voID Visit(Computer computer); voID Visit(Mouse mouse); voID Visit(Keyboard keyboard); voID Visit(Monitor monitor); } public interface IComputerPart { voID Accept(IComputerPartVisitor computerPartVisitor); } public class Keyboard : IComputerPart { public voID Accept(IComputerPartVisitor computerPartVisitor) { computerPartVisitor.Visit(this); } } public class Monitor : IComputerPart { public voID Accept(IComputerPartVisitor computerPartVisitor) { computerPartVisitor.Visit(this); }} public class Mouse : IComputerPart { public voID Accept(IComputerPartVisitor computerPartVisitor) { computerPartVisitor.Visit(this); } } public class Computer : IComputerPart { IComputerPart [] parts; public Computer() { parts = new IComputerPart[] { new Mouse(),new Keyboard(),new Monitor() }; } public voID Accept(IComputerPartVisitor computerPartVisitor) { for (int i = 0; i < parts.Length; i++) { parts[i].Accept(computerPartVisitor); } computerPartVisitor.Visit(this); } } public class ComputerPartdisplayVisitor : IComputerPartVisitor { public voID Visit(Computer computer) { Console.Writeline("displaying Computer."); } public voID Visit(Mouse mouse) { Console.Writeline("displaying Mouse."); } public voID Visit(Keyboard keyboard) { Console.Writeline("displaying Keyboard."); } public voID Visit(Monitor monitor) { Console.Writeline("displaying Monitor."); } }}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。
总结以上是内存溢出为你收集整理的C#设计模式之行为型模式详解全部内容,希望文章能够帮你解决C#设计模式之行为型模式详解所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)