設計模式匯總,包含生動的實例

文章結構是:英文原文+個人的理解
學習方法:
How to Use a Design Pattern Once you’ve picked a design pattern, how do you use it?Here’s a step-by-step approach to applying a design pattern effectively: 1. Read the pattern once through for an overview. Pay particular attention to the Applicability and Consequences sections to ensure the pattern isright for your problem. 2. Go back and study the Structure, Participants, and Collaborations sections. Make sure you understand the classes and objects in the pattern and how they relate to one another. 3. Look at the Sample Code section to seea concrete example of the pattern in code. Studying the code helps you learn how to implement the pattern. 4. Choose names for pattern participants that are meaningful in the application context. The names for participants in design patterns are usually too abstract to appear directly in an application. Nevertheless, it’s useful to incorporate the participant name into the name that appears in the application. That helps make the pattern more explicit in the implementation. For example, if you use the Strategy pattern for a text compositing algorithm, then you might have classesSimpleLayoutStrategy or TeXLayoutStrategy. 5. Define theclasses. Declare their interfaces, establish their inheritance relationships, and define the instance variables that represent data and object references.Identify existing classes in your application that the pattern will affect, and modify them accordingly.
學習pattern的時候的關注點是:結構,組成,連接的部分;
選擇design pattern的關注點;適用性和模式的結果;
實現的時候的關注點:範例代碼;
下面介紹每個模式我的理解(使用日常用到的工具或者自己寫代碼的習慣進行理解):
factory pattern 是常用的sdk給出的api的格式,比如我需要創建一個sdk内的物件時,只需要提供各種參數,sdk就可以創建好這個物件;除此之外,直接使用已經編譯好的lib,這樣可以加速程序的編譯,不用每次都編譯,也就是說lib的好處,除了封裝之外,還有加速編譯的效果;
builder pattern:
• Builder often builds a Composite.
• Often, designs start out using Factory Method (less complicated, more customizable, subclasses proliferate) and evolve toward Abstract Factory, Prototype, or Builder (more flexible, more complex) as the designer discovers where more flexibility is needed.
建設模式是一步一步的創建一個物件,這樣的物件更加靈活和複雜;
Prototype:Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype.從原型中不斷用複製的方式創建物件;
Singleton:
Ensure a class only has one instance, and provide a global point of access to it.

如果一個類是singleton模式的話,命名的時候,需要把singleton放入名字;

Structural patterns are concerned with how classes and objects are composed to form larger structures. Structural class patterns use inheritance to compose interfaces or implementations.結構型的模式是把多個類和物件進行結構設計的;

Adapter:
Convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn’t otherwise because of incompatible interfaces.
由於接口不一致而導致的情況,可以用adapter處理;比如string to int,int to string 的function就屬於這種模式;所以使用方式是 class A,class B, Adapter{ transform(class A){return class B}}

Bridge:
Decouple an abstraction from its implementation so that the two can vary independently.
繼承中的virtual函數,可以讓子類自己定義自己接口,但是對外的接口是一致的;
Composite:
Compose objects into tree structures to represent part-whole hierarchies. Composite lets clients treat individual objects and compositions of objects uniformly.

平時定義結構體的時候,就會包含各種結構體,這就是結構體常用的設計模式,包含,再包含;
優點:新增元素的時候,很容易,不用改動其他的部分;如果某個方法需要更新了,需要新增元素,不會影響到其他的方法(其他的方法不會調用這個元素);
Decorator
Attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality.
Sometimes we want to add responsibilities to individual objects, not to an entire class.Agraphicaluser interface toolkit,for example,should let you add properties like borders or behaviors like scrolling to any user interface component.
一個類的功能很多,但是不是所有物件都需要這些功能,那麽就需要默認是屏蔽的或者沒有,只有儅需要用到的時候,才附加上去;這個和創建模式中的builder pattern是相關的,因爲需要新增裝飾上去;
實例:命名空間,可以不斷新增各種類,並調用這些類的方法;
Facade
Provide a unified interface to a set of interfaces in a subsystem. Facade defines a higher-level interface that makes the subsystem easier to use.
爲了讓子系統更容易使用,而設計一個更上層的接口,這個接口使用一系列的系統接口;這個很好理解,就和Init()實現時,往往都要調用和處理多個接口一樣;
Flyweight
Use sharing to support large numbers of fine-grained objects efficiently;
Proxy
Provide a surrogate or placeholder for another object to control access to it.
One reason for controlling accessto an object is to defer the full cost of its creation and initialization until we actually need to use it. Consider a document editor that can embed graphical objectsin a document. Some graphical objects, like large raster images, can be expensive to create.But opening a document should be fast, sowe should avoid creating all the expensive objects at once when the document is opened. This isn’t necessary anyway, because not all of these objects will be visible in the document at the same time。
也是先隱藏或者暫停部分功能,等需要用的時候,再使用,只不過代理模式的處理方式,是可以處理部分功能而那些耗時間的功能(需要正在更改原object的功能)就需要本尊處理了;
實例:處理數據庫的時候,會預讀出數據庫的内容,儅查找的時候就只需要使用預讀出的數據就好,而要修改内容的時候,就需要直接修改數據庫了;

The Adapter (139) and Bridge (151) patterns have some common attributes. Both promote flexibility by providing a level of indirection to another object. Both involve forwarding requests to this object from an interface other than its own. The key difference between these patterns lies in their intents. Adapter focuses on resolving incompatibilities between two existing interfaces. It doesn’t focus on how those interfaces are implemented, nor does it consider how they might evolve independently.
Behavioral Patterns:
Behavioral patterns are concerned with algorithms and the assignment of responsibilities between objects.處理物件之間的運算和算法的模型,operator的方法設計的時候需要參考的模型;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值