designs\project\designPatterns

本文除了状态机的数学定义是从课本上摘录的以外,其他的数学定义均为博主原创。所以,额,如有谬误,敬请留言批评指正。^%^。

感谢Head First Design Patterns 的作者们 Eric Freeman, Elisabeth Freeman, Kathy Sierra, and Bert Bates;在阅读完成本书后,结合discrete mathematics为本博文提供了创作灵感。

I prefer to describe design patterns without any graphical umls.

原则1:Identify the aspects of your application that vary and separate them from what stays the same.

原则2:Program to an interface, not an implementation.

原则3:Favor composition over inheritance.

 

The Strategy Pattern defines a family of algorithms, encapsulates each one, and makes them interchangeable. Strategy lets the algorithm vary independently from clients that use it.

策略 S=(f, Ο) 其中f代表Formulating后的算法封装;Ο 代表分治的a family of algorithms也就是所有被封装的算法的实现implementing

Formulating包括分析环境或情况,作出诊断,制定指导方针。它包括战略规划和战略思维等活动

 

扫描二维码关注公众号,回复: 9118097 查看本文章

 

原则4:Strive for loosely coupled designs between objects that interact

 

The Observer Pattern defines a one-to-many dependency between objects so that when one object changes state, all of its dependents are notified and updated automatically.

发布-订阅 O = (S, E, R), ES, R={(pub, sub) | sub OBSERVERS pub} S表示所有内部状态; E表示state变化事件eventR代表Observers观察Subject关系;

 

 

原则5:Classes should be open for extension, but closed for modifi cation.

 

The Decorator Pattern attaches additional responsibilities to an object dynamically. Decorators provide a fl exible alternative to subclassing for extending functionality.

前缀/后缀:If x,y∈∑* and w=xy, then the string x is called a prefix of w, and yλ, then x is said to be a proper prefix. Similarly, the string y is called a suffix of w; it is a proper suffix when xλ.

装饰:R={(w, x, y) | w=xy, x,y∈∑*}

 

 

原则6:Depend upon abstractions. Do not depend upon concrete classes.

 

The Factory Method Pattern defi nes an interface for creating an object, but lets subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses.

The Abstract Factory Pattern provides an interface for creating families of related or dependent objects without specifying their concrete classes.

分组:A = UiIAi????

 

The Singleton Pattern ensures a class has only one instance, and provides a global point of access to it.

单例:I=(f, R) R={(x,c) | x属于类型c, |{x}| = 1} f表示{} -> x获得类型c的实例x的方法;R代表x是某个类的实例,且这个实例是可数的,通常是1个;

 

 

The Command Pattern encapsulates a request as an object, thereby letting you parameterize other objects with different requests, queue or log requests, and support undoable operations.

命令:Command= Request.excute(x)=Receiver.action(x)

 

 

原则7:Principle of Least Knowledge talk only to your immediate friends.

 

The Adapter Pattern converts the interface of a class into another interface the clients expect. Adapter lets classes work together that couldn’t otherwise because of incompatible interfaces.

The Facade Pattern provides a unifi ed interface to a set of interfaces in a subsytem. Facade defi nes a higherlevel interface that makes the subsystem easier to use.

适配/门面:Adapter=(interface_f, R) R={A,B| AB的适配 or AB的门面} interface_f  R->R表示关系R的访问接口;R表示AB的适配关系,或者门面关系;

 

 

原则8:Don’t call us, we’ll call you.

 

The Template Method Pattern defi nes the skeleton of an algorithm in a method, deferring some steps to subclasses. Template Method lets subclasses redefi ne certain steps of an algorithm without changing the algorithm’s structure.

模板方法:F={(o1, o2,…) | oiO}  O={(A,B,C,…) | A->B,…}F封装算法流程;O表示封装算法操作;

 

 

原则9:A class should have only one reason to change.

 

The Iterator Pattern provides a way to access the elements of an aggregate object sequentially without exposing its underlying representation.

The Composite Pattern allows you to compose objects into tree structures to represent part-whole hierarchies. Composite lets clients treat individual objects and compositions of objects uniformly.

迭代/组合:I = {R1, R2} R1= {a relation on Data A} R2= {another relation on Data A like iterator or composite} R1表示数据 A上的原有业务逻辑;R2表示数据A的迭代/组合方法。

举个栗子:public string Property property { get; set;},大家看这个Property,他既然是Public的。还写个get,set方法,是不是很多余呢?这个问题困惑了我好多年。但是,自从学习了原则9:A class should have only one reason to change.之后。I={R1,R2},其中R1表示Property是某个类Class的属性是Has a的关系,R2表示Property和Property的实例property之间又可以有is a关系,栗如:以下的设计 public 大傻 小傻 {get小傻; set小傻}我家有个大傻,大傻生了一个小傻!0!

 

 

The State Pattern allows an object to alter its behavior when its internal state changes. The object will appear to change its class.

状态机M=(S, φ, Ο, v, w) 其中S 表示M的内部状态集; φ 表示M的输入字符集 ; Ο 表示 M的输出字符集; v 表示S X φ -> S是下一个状态函数,状态变迁函数,接受上一个状态S和本次输入φ 作为参数;w 表示S X φ –> Ο,是输出函数,接受上一个状态S和本次输入φ 作为参数。

 

 

The Proxy Pattern provides a surrogate or placeholder for another object to control access to it.

代理:R={{A, B}, {B, C}} B表示AC中间的代理。

发布了26 篇原创文章 · 获赞 4 · 访问量 2549

猜你喜欢

转载自blog.csdn.net/u012296499/article/details/82832923