##大话设计模式笔记
- 24种设计模式,4大原则
- 简单工厂模式和策略模式很像
- 工厂模式是简单工厂的改进版
- 开源项目二次开发时,可以利用装饰模式添加新功能
- 浏览器中用代理模式来优化下载
- 原型模式就是Java里面的clone()方法
###1.简单工厂模式
public class OperationFactory { public static Operation createOperate(string operate) { Operation oper = null; switch(operate) { case "+": oper = new OperationAdd(); break; case "-": oper = new OperationSub(); break; case "*": oper = new OperationMul(); break; case "/" oper = new OperationDiv(); break; } return oper; } }
Operation oper; oper = OperationFactory.createOperate("+"); oper.NumberA = 1; oper.NumberB = 2; double result = oper.GetResult();
|
###2.策略模式
class CashContext { CashSuper cs = null; public CashSuper(String type) { case "正常收费": CashNormal cs0 = new CashNormal(); cs = cs0; break; case "满300烦100": CashReturn cr1 = new CashReturn("300", "100"); cs = cs1; break; case "打8折": CashRebate cr2 = new CashRebate("0.8"); break; } public double GetResult(double money) { return cs.acceptCash(money); } }
CashContext csuper = new CashContext("正常收费"); double price = csuper.GetResult(100d);
|
###3.装饰模式
动态的给一个对象添加一些额外的职责,就增加功能来说,装饰模式比生成子类更为灵活。
abstract class Component { public abstract void Operation(); }
class ConcreteComponent : Component { public override void Operation() { } }
abstract class Decorator : Component { protected Component component; public void SetComponent(Component component) { this.component = component; } public override void Operation() { if(null != component) { component.Operation(); } } }
ConcreteComponent c = new ConcreteComponent(); ConcreteDecoratorA d1 = new ConcreteDecoratorA(); ConcreteDecoratorB d2 = new ConcreteDecoratorB();
d1.SetComponent(c); d2.SetComponent(d1);
d2.Operation();
|
###4.代理模式
abstract class Subject { public abstract void Request(); }
class RealSubject : Subject { public override void Request() { } }
class Proxy : Subject { RealSubject realSubject = new RealSubject(); public override void Request() { realSubject.Requst(); } }
Proxy proxy = new Proxy(); proxy.Request();
|
###5.工厂方法模式
克服了简单工厂违背开放-封闭原则的缺点,又保持了封装对象创建过程的优点。
interface IFactory { LeiFeng CreateLeiFeng(); }
class UndergraduateFactory : IFactory { public LeiFeng CreateLeiFeng() { } }
class VolunteerFactory : IFactory { public LeiFeng CreanteLeiFeng() { } }
IFactory factory = new UndergraduateFactory(); LeiFeng student = factory.CreateLeiFeng();
|
###6.原型模式
abstract class Prototype { private String id; public Prototype(string id) { this.id = id; } public string id { get { return id; } } public abstract Prototype Clone() { } }
class ContretePrototype : Prototype { public ConcretePrototype(string id) : base(id) { } public override Prototype Clone() { } }
ConcretePrototype p1 = new ConcretePrototype("I"); ConcretePrototype p2 = (ConcretePrototype)p1.Clone();
|
###7.模板方法模式
###8.外观模式
###9.建造者模式
###10.观察者模式
###11.抽象工厂模式
###12.状态模式
###13.适配器模式
###14.备忘录模式
###15.组合模式
###16.迭代器模式
###17.单例模式
###18.桥接模式
###19.命令模式
###20.职责链模式
###21.中介者模式
###22.享元模式
###23.解释器模式
###24.访问者模式
###单一职责原则
就一个类而言,应该仅有一个引起它变化的原因
- 发现职责并把哪些职责相互分离
- 如果你能想到多于一个动机去改变一个类,那么这个类就具有多于一个的职责
###开放-封闭原则
是说软件实体(类、模块、函数等等)应该可以扩展,但是不可以修改。对于扩展是开放的,对于更改是封闭的
- 面对需求,对程序的改动是通过增加新的代码进行的,而不是更改现有的代码
- 猜测出最有可能发生变化的种类,然后构造出抽象来隔离那些变化
###依赖倒转原则
A. 高层模块不应该依赖底层模块。两个都应该依赖抽象 B. 抽象不应该依赖细节。细节应该依赖抽象
- 针对接口编程,不要针对实现编程
- 里氏代换原则:子类型必须能够替换掉他们的父类型。
###迪米特法则
###模式总结