1. 程式人生 > >設計模式學習之路(一) 記住所有設計模式

設計模式學習之路(一) 記住所有設計模式

雖然一直都想靜下心來好好學習設計模式,但總是抽不出一個系統的時間來好好看看與之相關的書籍,直到最近專案提交完畢,才可以有一些間來好好學習下它的精華。

首次看到設計模式這個東西,感覺非常的高大上,總是認為自己學不會,其主要原因是我之前的工作一直用的都是C程式設計,可以說與設計模式相關的並不多,而且已經習慣了這種面向過程的開發方式,所以一直一來我都覺得很難學會,而且之前有曾接觸過它,但是總是三天打魚兩天晒網,剛剛感覺略有心得,過一陣子不接觸又還給書本了。目前換了一份新工作,涉及到一些JAVA程式設計,這激發了我想重新撿起GoF的書籍來努力學習的激情。

多年的學習經驗告訴我,在學習一門新技術之前,必須要了解與之相關的Jargon。 翻開 Design Pattern 這本書,直接找到 1.4 The Catalog of Design Patterns 面對著這23種設計模式,咬咬牙,背誦之。

年近30的我,發覺記憶力大不如前,23種設計模式背完花了相當長的時間,過程很是辛苦,我每天上班第一件事就是開啟word 默寫一遍所有這23種設計模式,以增強記憶,但功夫不負有心人,經過我不斷的努力,終於可以將這些條目背下來了,儘管沒有辦法做到一字不落,但還是可以說到了相當熟練的地步了。

這是我所學習的第一步,那就是  背誦。

總結個人經驗就是,背英文相關的東西,最好的方就是多複習。

現在將它們寫到部落格裡,留著以後複習之用。

1.      Abstract Factory – define an interface forcreating families of related or dependent objects without specifying theirconcrete classes.

2.      Adapter – convert the interfaces of an object toanother the clients expect. Adapter makes classes work together that couldn’totherwise because of incompatible interfaces.

3.      Bridge --separate an abstraction from its representation so that the two can varyindependently.

4.      Builder – separate the construction of a complexobject from its representation, so that the same construction process cancreate different representations.

5.      Chain of responsibility – avoid coupling thesender of a request from its receiver by providing more than one object achance to handle it. Chain the receiving objects and pass the request along thechain until an object handles it.

6.      Composite – Compose object into tree-likestructure to represent part-whole hierarchies. Composite lets clients treatindividual objects and compositions of object uniformly..

7.      Command – encapsulate a request as an object, therebyletting you parameterize clients with different request, queue or log requestsand support undoable operations.

8.      Decorator – attaches different responsibility toan object dynamically. Decorator provide a flexible alternative to subclassingfor extending functionality.

9.      Facade – provide a unified interface to a set ofinterfaces in a subsystem. Facade defines a hither-level interface that makessubsystem easier to use.

10.  Factory Method – define an interface forcreating objects but let subclasses decide which class to instantiate. FactoryMethod lets a class defer its instantiation to subclasses.

11.  Flyweight – use sharing to support large numberof fine-grained objects efficiently.

12.  Iterator – provide a way to access the elementsof an aggregate object sequentially without exposing its underlyingrepresentation.

13.  Interpreter – given a language, define arepresentation for its grammar along with an interpreter that uses thisrepresentation to interpret sentences in this language.

14.  Mediator – define a object that encapsulate howa set of objects interact, Mediator promotes loose coupling by keeping objectsreferring to each other explicitly, and it lets you vary their interactionindependently.

15.  Memento – without violating encapsulation,capture and externalize the internal state of an object, so that it can berestored to this state later.

16.  Observer – define a one-to-many dependentbetween objects, so that when object changes state all of its dependents arenotified and updated automatically.

17.  Prototype – specify the kinds of objects tocreate using a prototypical instance, and create new objects by copying thisprototype.

18.  Proxy – provide a surrogate or placeholder to anobject to control access to it.

19.  Singleton – ensure a class only has one objectand provide a global point of access to it.

20.  State – allow object to alter its behavior whenits internal state changes. Objects will appear to change its class.

21.  Strategy – define a families of algorithmsencapsulate each one and make them interchangeable. Strategy lets the algorithmvary independently from clients that use it.

22.  Template Method – define the skeleton of anobject in an operation, deferring some steps to subclasses. Template Methodlets subclass redefine certain steps of an algorithm without changing thealgorithm’s structure.

23.  Visitor – represent the operation to beperformed on the elements of an object structure, visitor lets you define a newoperation without changing the classes of objects on which it operates.