时间:2021-05-20
这里列举行为型模式·到此23种就列完了···这里是看着菜鸟教程来实现··,他里边列了25种,其中过滤器模式和空对象模式应该不属于所谓的23种模式
责任链模式:为请求创建一个接收者对象的链,对请求的发送者和接收者进行解耦,大部分用于web中吧。。
Task中的continuewith和微软的tpl数据流应该是类似这种模式的实现吧
命令模式(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", name, 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):定义一系列算法,封装成类,可以相互替换,通过构造不同的类,执行不同的操作。这样做方便调用,添加新的算法也方便,
最后加了自己之前对这个模式的奇葩写法
模板模式(Template Pattern):.net的泛型就是这个模式的实现吧··照着模子写就行了
访问者模式(Visitor Pattern):在被访问的类里边加一个对外提供接待访问的接口
把数据结构和对应的操作分开·添加操作很容易,但是如果结构变化多的化,改起来就麻烦了··
没研究没用过····
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
设计模式分为三种类型,共23种。 其中创建型模式有单例模式、抽象工厂模式、建造者模式、工厂模式、原型模式。 结构型模式有适配器模式、桥接模式、装饰模式、组合
java设计模式之State(状态模式)在状态模式中,一个类的行为基于它的状态的改变而改变。状态模式归属于行为型模式。在下面的实例中,我们创建了一个接口Stat
一、概况总体来说设计模式分为三大类:(1)创建型模式,共五种:工厂方法模式、抽象工厂模式、单例模式、建造者模式、原型模式。(2)结构型模式,共七种:适配器模式、
前几篇介绍了设计模式的特性并且详细讲解了4种创建型模式,创建型模式是负责如何产生对象实例的,接下来讲讲结构型模式。一、什么是结构型模式?结构型模式是解析类和对象
MySQL设计和命令行模式下建立详解系列文章:MySQL设计和命令行模式下建立详解C++利用MySQLAPI连接和操作数据库实例详解1.数据表的设计MySQL数