05 2022 档案
摘要:解释器模式(Interpreter Pattern) 提供了评估语言的语法或表达式的方式,它属于行为型模式。这种模式实现了一个表达式接口,该接口解释一个特定的上下文。这种模式被用在 SQL 解析、符号处理引擎等。 .情景:将 string input = "(12+5)-(12+1)";进行解析,并
阅读全文
摘要:两个银行之间进行汇款: public class BankAccount { private int balance; private int overdraft = -500; /// <summary> /// 存款 /// </summary> /// <param name="amount"
阅读全文
摘要:public class BankAccount { private int balance; private int overdraft = -500; /// <summary> /// 存款 /// </summary> /// <param name="amount"></param> pu
阅读全文
摘要:命令模式(Command Pattern)是一种数据驱动的设计模式,它属于行为型模式。请求以命令的形式包裹在对象中,并传给调用对象。调用对象寻找可以处理该命令的合适的对象,并把该命令传给相应的对象,该对象执行命令。 银行取款存款例子: 把各中操作扁平化,然后自定义编辑指令进行组合。最后按顺序执行!
阅读全文
摘要:延续责任链模式: https://www.cnblogs.com/Zingu/p/16309483.html 以上例子是增加永久防御和攻击,假设增加增益属性只在一个回合内或者一段时间内有效该如何处理? 增加一个游戏管理者:用来查询各项属性。 public class Game { public ev
阅读全文
摘要:责任链模式(Chain of Responsibility Pattern) 情景:假设一个卡牌类游戏,怪物卡和属性卡 可以给怪物增加攻击属性,防御属性 /// <summary> /// 生物基础 /// </summary> public class Creature { public stri
阅读全文
摘要:添加类 public class AutofacBuilder { private static IContainer _container; public static void init() { ContainerBuilder builder = new ContainerBuilder();
阅读全文
摘要:Dynamic Proxy 情景: 定义银行接口,包含两个方法 存钱和取钱 需要在原有接口基础上记录存钱和取钱的次数相关信息; Nuget:ImpromptuInterface 实现: public interface IBankAccount { void Deposit(int amount);
阅读全文
摘要:Compositite_Proxy_Array_Backed_Properties 情景: 有四个点选框 主项是All 子项是Pillars, Walls, Floors; 若子项全选中则主项选中,子项有一项未选中,则主项也未选中。 图例如下(红色为选中): 实现1: public class Ma
阅读全文
摘要:Compositite Proxy_SoA_AoS 情景1: 一个游戏 有100个对象的X坐标需要全部移动; class Creature { public byte Age; public int X, Y; } class Program { static void Main(string[]
阅读全文
摘要:Value Proxy [DebuggerDisplay("{value*100.0d}%")] public struct Perceentage { private readonly double value; public Perceentage(double value) { this.va
阅读全文
摘要: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
阅读全文
摘要:行车不规范,情人两行泪! 代理模式: public interface ICar { void Driver(); } public class Car : ICar { public void Driver() { Console.WriteLine("Car is running"); } }
阅读全文
摘要:GoF 设计模式 享元例子: 示例1:将一段文字的某一段内容转换为大写: class TextFormatted { private string text; private bool[] isFormatted; public TextFormatted(string text) { this.t
阅读全文
摘要:用途:节约空间,共享单元 示例1:重复用户 public class User { string fullName; public User(string fullName) { this.fullName = fullName; } public override string ToString(
阅读全文
摘要:Decorator in Dependency Injection public interface IReportServer { void PrintReport(); } public class ReportService : IReportServer { public void Prin
阅读全文
摘要:静态装饰组合Static Decorator Compsition public abstract class Shape { public abstract string AsString(); } public class Circel : Shape { float ridus; public
阅读全文
摘要:装饰者模式 检查装饰器循环 过度设计 较复杂。 public abstract class Shape { public virtual string AsString() => string.Empty; } class Circel : Shape { float ridus; public C
阅读全文
摘要:DesignPartten_Decoration_Dynamic_Composition 先定义个一个形状的接口,可以转换为字符串 interface IShape { string AsString(); } 添加各种形状 class Circel:IShape { float ridus; pu
阅读全文
摘要:装饰器模式 Multiple Inheritance多重继承 鸟 会飞 蜥蜴 会爬行 龙 能飞也能爬行 C# 不支持多继承,单可以多实现、所以我们这样做 public interface IBrid { void Fly(); } public class Brid: IBrid { public
阅读全文
摘要:装饰者模式: 例子:对StringBuilder进行装饰 public class MyString { private StringBuilder sb = new StringBuilder(); public static implicit operator MyString(string s
阅读全文
摘要:例子:对应 https://www.cnblogs.com/Zingu/p/16256988.html enum Color { Yellow, Red, Green } enum Size { Small, Medium, Large, Yuge } class Product { public
阅读全文
摘要:复合模式 神经元和神经元组 public static class ExtensitionNeural { //public static void ShowNeuralsIn(this Neural l, int dent) //{ // Console.WriteLine($"{new stri
阅读全文
摘要:class GraphicObject { public virtual string GroupName { get; set; }= "Group"; public string Color; private Lazy<List<GraphicObject>> graphicObjects =
阅读全文
摘要:桥: interface IRenderer { void RenderCircle(float radius); } class VectorRenderder : IRenderer { public void RenderCircle(float radius) { Console.Write
阅读全文
摘要:适配器模式+AutoFac interface ICommand { void Excute(); } class OpenCommand : ICommand { public void Excute() { Console.Write("this is Opened"); } } class S
阅读全文
摘要:适配器模式: 我国规定民用交流电压是220V(+10% -15%),就是说交流220V电压正常范围在187V--242V 但是每种电器的接收的电压都不一致: 1.电脑 2手机 3.空调 虽然输入电压是相同的220V 但是电器内部少有能直接接收220V电压的零件。 再举个例子。或者说你的手机出国也能充
阅读全文
摘要:单例模式 线程检查: public sealed class PreThreadSingleton { private static ThreadLocal<PreThreadSingleton> local = new ThreadLocal<PreThreadSingleton>(() => n
阅读全文
摘要:单例模式 LAZY<T>: interface IDatabase { int GetConnection(string name); } class FileDatabase : IDatabase { private Dictionary<string, int> dic = new Dicti
阅读全文
摘要:单例模式 MonoState public class Boss { private static string name; public string Name { get => name; set => name = value; } public override string ToStrin
阅读全文
摘要:单例模式:上下文语境 using System; using System.Collections.Generic; namespace DesignParttern_Singleton_Context { public class BuildContext:IDisposable { public
阅读全文
摘要:MoreLinq是在Linq的基础上增加了很多的扩充。 Git: https://github.com/morelinq/MoreLINQ Nuget: pm=> Install-Package morelinq -Version 3.3.2 具体使用: ForEach var list1 = En
阅读全文
摘要:Dependence Inversion Principle 依赖倒置原则 Dependency Inversion:依赖倒置。即面向抽象/接口编程。可参考依赖倒置原则。面向抽象编程,可大大提高灵活度,因为抽象可继承实现,而所有实现均可替代抽象。 设计模式05-依赖倒置(依赖高层抽象,不应该依赖细节
阅读全文
摘要:Liskov Substitution Principle 里式替换原则 Liskov Substitution: 里氏替换。在任何基类类出现的地方,子类能直接替代基类,也就是说,基类有任何修改,都不会对子类功能产生影响。 以下例子:正方形是矩形吗? class Rectange { //publi
阅读全文
摘要:Interface Segregation: 接口隔离 单一职能和接口隔离感觉上十分相似,都使用了最小化原则。(接口保持最小单位,然后可以使用继承进行拓展) 以下例子是打印机功能,新的打印机可以有多个功能,单是老的打印只能打印,此时我们就应该把打印接口单独隔离 class Document { }
阅读全文
摘要:Open Closed Principle:开闭原则 OpenClosed: 开闭原则。对拓展开放,对修改关闭,增加新需求时可以轻松拓展而不用修改已有代码。 以下例子是对于属于的过滤(零件名称,零件颜色,零件尺寸分别过滤筛选) enum Color { Yellow,Red,Green } enum
阅读全文
摘要:Single Responsibility Principle:单一职责原则 Single: 单一功能,各个部分保持独立,避免一个程序负责大于等于两个以上的功能 public class Log { private readonly List<string> entries = new List<s
阅读全文
浙公网安备 33010602011771号