随笔分类 -  DesignPatterns

设计模式
摘要:简单解锁例子 enum State { 锁定, 失败, 已解锁 } class Program { static void Main(string[] args) { string code = "132"; var state = State.锁定; var strb = new StringBu 阅读全文
posted @ 2022-07-05 15:19 后跳 阅读(37) 评论(0) 推荐(0)
摘要:电话状态机 enum State { 脱机, 连接中, 已连接, 通话保持 } enum Trigger { 拨打电话, 挂断电话, 接收来电, 通话保持, 取消等待, 语音留言 } class Program { public static Dictionary<State, List<(Trig 阅读全文
posted @ 2022-07-05 15:03 后跳 阅读(46) 评论(0) 推荐(0)
摘要:状态之开关控制 public class Switch { public State State = new OffState(); public void On() { State.On(this); } public void Off() { State.Off(this); } } publi 阅读全文
posted @ 2022-07-05 14:24 后跳 阅读(28) 评论(0) 推荐(0)
摘要:构造注入时接收空对象导致执行错误的处理方式: 基本情况: interface ILog { void Info(string msg); void Wran(string msg); } class BankAccount { private readonly ILog log; private i 阅读全文
posted @ 2022-06-13 11:07 后跳 阅读(25) 评论(0) 推荐(0)
摘要:解释器模式(Interpreter Pattern) 提供了评估语言的语法或表达式的方式,它属于行为型模式。这种模式实现了一个表达式接口,该接口解释一个特定的上下文。这种模式被用在 SQL 解析、符号处理引擎等。 .情景:将 string input = "(12+5)-(12+1)";进行解析,并 阅读全文
posted @ 2022-05-27 16:07 后跳 阅读(56) 评论(0) 推荐(0)
摘要:两个银行之间进行汇款: public class BankAccount { private int balance; private int overdraft = -500; /// <summary> /// 存款 /// </summary> /// <param name="amount" 阅读全文
posted @ 2022-05-26 13:44 后跳 阅读(50) 评论(0) 推荐(0)
摘要:public class BankAccount { private int balance; private int overdraft = -500; /// <summary> /// 存款 /// </summary> /// <param name="amount"></param> pu 阅读全文
posted @ 2022-05-26 10:54 后跳 阅读(50) 评论(0) 推荐(0)
摘要:命令模式(Command Pattern)是一种数据驱动的设计模式,它属于行为型模式。请求以命令的形式包裹在对象中,并传给调用对象。调用对象寻找可以处理该命令的合适的对象,并把该命令传给相应的对象,该对象执行命令。 银行取款存款例子: 把各中操作扁平化,然后自定义编辑指令进行组合。最后按顺序执行! 阅读全文
posted @ 2022-05-26 09:13 后跳 阅读(36) 评论(0) 推荐(0)
摘要:延续责任链模式: https://www.cnblogs.com/Zingu/p/16309483.html 以上例子是增加永久防御和攻击,假设增加增益属性只在一个回合内或者一段时间内有效该如何处理? 增加一个游戏管理者:用来查询各项属性。 public class Game { public ev 阅读全文
posted @ 2022-05-25 17:00 后跳 阅读(51) 评论(0) 推荐(0)
摘要:责任链模式(Chain of Responsibility Pattern) 情景:假设一个卡牌类游戏,怪物卡和属性卡 可以给怪物增加攻击属性,防御属性 /// <summary> /// 生物基础 /// </summary> public class Creature { public stri 阅读全文
posted @ 2022-05-25 15:33 后跳 阅读(52) 评论(0) 推荐(0)
摘要:Dynamic Proxy 情景: 定义银行接口,包含两个方法 存钱和取钱 需要在原有接口基础上记录存钱和取钱的次数相关信息; Nuget:ImpromptuInterface 实现: public interface IBankAccount { void Deposit(int amount); 阅读全文
posted @ 2022-05-20 18:35 后跳 阅读(30) 评论(0) 推荐(0)
摘要:Compositite_Proxy_Array_Backed_Properties 情景: 有四个点选框 主项是All 子项是Pillars, Walls, Floors; 若子项全选中则主项选中,子项有一项未选中,则主项也未选中。 图例如下(红色为选中): 实现1: public class Ma 阅读全文
posted @ 2022-05-20 17:38 后跳 阅读(45) 评论(0) 推荐(0)
摘要:Compositite Proxy_SoA_AoS 情景1: 一个游戏 有100个对象的X坐标需要全部移动; class Creature { public byte Age; public int X, Y; } class Program { static void Main(string[] 阅读全文
posted @ 2022-05-20 16:28 后跳 阅读(15) 评论(0) 推荐(0)
摘要:Value Proxy [DebuggerDisplay("{value*100.0d}%")] public struct Perceentage { private readonly double value; public Perceentage(double value) { this.va 阅读全文
posted @ 2022-05-20 15:08 后跳 阅读(37) 评论(0) 推荐(0)
摘要:Property Proxy public class Property<T>:IEquatable<Property<T>> where T:new() { private T value; public T Value { get => value; set { if (this.value.E 阅读全文
posted @ 2022-05-20 14:08 后跳 阅读(28) 评论(0) 推荐(0)
摘要:行车不规范,情人两行泪! 代理模式: public interface ICar { void Driver(); } public class Car : ICar { public void Driver() { Console.WriteLine("Car is running"); } } 阅读全文
posted @ 2022-05-20 12:10 后跳 阅读(66) 评论(0) 推荐(0)
摘要:GoF 设计模式 享元例子: 示例1:将一段文字的某一段内容转换为大写: class TextFormatted { private string text; private bool[] isFormatted; public TextFormatted(string text) { this.t 阅读全文
posted @ 2022-05-20 11:38 后跳 阅读(34) 评论(0) 推荐(0)
摘要:用途:节约空间,共享单元 示例1:重复用户 public class User { string fullName; public User(string fullName) { this.fullName = fullName; } public override string ToString( 阅读全文
posted @ 2022-05-20 10:48 后跳 阅读(23) 评论(0) 推荐(0)
摘要:Decorator in Dependency Injection public interface IReportServer { void PrintReport(); } public class ReportService : IReportServer { public void Prin 阅读全文
posted @ 2022-05-19 16:44 后跳 阅读(43) 评论(0) 推荐(0)
摘要:装饰者模式 检查装饰器循环 过度设计 较复杂。 public abstract class Shape { public virtual string AsString() => string.Empty; } class Circel : Shape { float ridus; public C 阅读全文
posted @ 2022-05-19 15:18 后跳 阅读(35) 评论(0) 推荐(0)