# POSD Final Notes
## ★ __++**Singleton**++__:(P109~116)
>Ensure a class only has one instance, and provide a global point of access to it.
>(保證一個class只有一個instance, 提供一個global point給其他使用者存取。)
![](https://i.imgur.com/CwDbDZf.png)
![](https://i.imgur.com/qRbkHhH.png)
![](https://i.imgur.com/lTUKto0.png)
:::info
+ 步驟:
+ 把constructor放到protected,不要讓user直接用
+ 在public放一個static Singleton指標
+ 呼叫instance()時,新增一個Singleton物件,此時_instance不再是0
+ 以後再呼叫instacne()時,就不會再產生物件
:::
:::success
+ 優點:
+ Controlled access to sole instance.
(singleton會封裝他唯一的instance,嚴格控管client端存取)
+ Reduced name space.
+ Permits refinement of operations and representation.
+ Permits a variable number of instances.
+ More flexible than class operations.
:::
## ★ __++**Observer**++__:(P249~257)
>Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.
>(定義一個物件們之間1對多的依存關係,當一個物件(subject)修改狀態時,其他依賴它的物件(concrete observer),也會自動修改和更新。)
## ★ __++**Strategy**++__:(P266~273)
>Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it.
>(對同一家族的每個方法進行封裝,使他們可以彼此交換。Strategy讓使用它的人,在client端能獨立地修改方法,e.g.文字編輯的排版策略)
## ★ __++**Decorator**++__:(P149~156)
>Attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality.
>(不改變物件本質而是動態地增加行為,為subclass提供有彈性的替代方案來延伸功能)
## ★ __++**Factory Method**++__:(P93~100)
>Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses.
>(定義創造物件的介面,將創造的過程遮蔽起來,達到提高靈活性的目的。)
+ 例如將 ScrollBar* sb = new MotiScrollBar; 改成 ScrollBar* sb= guifactory ->CreatScrollBar();
+ 你看不出來是windows的ScrollBar還是Mac的ScrollBar,因為不知道所以有彈性。
## ★ __++**Bridge**++__:(P130~138)
>Decouple an abstraction from its implementation so that the two can vary independently.
>(分離抽象部分(interface)和實作部分讓兩邊獨立來加以改變。)
## ★ __++**Command**++__:(P198~206)
>Encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations.
>(將請求封裝成一物件,使我們可以對於不同的請求、請求佇列或請求紀錄、支援可回復操作,將客戶進行參數化)
+ 當我們需要向某些物件發送請求,但是並不知道請求的接收者是誰,也不知道被請求的操作是哪個,我們只需在程式運行時指定具體的請求接收者即可,此時,可以使用command pattern來進行設計,使得請求發送者與請求接收者消除彼此之間的耦合,讓物件之間的調用關係更加靈活。
## ★ __++**Iterator**++__:(P219~231)
>Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation.
>(循序的存取一群物件而不接觸到物件底層的表達方式。)
## ★ __++**Proxy**++__:(P176~185)
>Provide a surrogate or placeholder for another object to control access to it.
>(提供物件的代理人來控制是否要存取物件。例如:網頁圖片先知道大小和位置,等到滑鼠瀏覽到那才載入;也就是需要的當下才create出來。)
## ★ __++**Adapter**++__:(P120~129)
>Convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn't otherwise because of incompatible interfaces.
>(改變目前類別的介面,變成client所期望的另一種介面。Adpater能讓不相容的兩種類別同時工作。)
+ Class adapter (multiple inheritance)
![](https://i.imgur.com/12uWTzJ.png)
+ Object adapter (object composition)
![](https://i.imgur.com/5hwG4z8.png)
> Favor object composition over class inheritance.
## ★ __++**Mediator**++__:(P232~240)
>Define an object that encapsulates how a set of objects interact. Mediator promotes loose coupling by keeping objects from referring to each other explicitly, and it lets you vary their interaction independently.
>(用一個物件來封裝一系列物件的互動,仲介者使各個物件不需要有明顯互動關係,使它們的耦合關係鬆散,而且可以獨立地改變它們之間的互動關係。)
+ A(賣房子的人)和B(想要買房子的人)B溝通透過C(仲介)
+ 仲介收集所有要賣房子的人,和想要買房子的人,兩邊交易的成功率就能增加。
## ★ __LEXI的8個pattern功能__
>composite, strategy, decorator, abstract factory, bridge, command, iterator, visitor
+ **Composite**:to represent the document's physical structure
+ **Strategy**:to allow different formatting algorithms
+ **Decorator**:for embellishing the user interface
+ **Abstract Factory**:for supporting multiple look-and-feel standards
+ **Bridge**:to allow multiple windowing platforms
+ **Command**:for undoable user operations
+ **Iterator**:for accessing and traversing object structures
+ **Visitor**:for allowing an open-ended number of analytical capabilities without complicating the document structure's implementation.
## ★ __++**SiteMap and relationship**++__
![](http://www.gofpatterns.com/images/gofPatterns.jpg)
![](https://i.imgur.com/hn44fie.png)
## ★ __++**UncleBob**++__
|Abbreviation|Full Name|Definition|
|---|---|---|
|SRP|The Single Responsibility Principle|A class should have one, and only one, reason to change.|
|OCP|The Open Closed Principle|You should be able to extend a classes behavior, without modifying it.|
|LSP|The Liskov Substitution Principle|Derived classes must be substitutable for their base classes.|
|ISP|The Interface Segregation Principle|Make fine grained interfaces that are client specific.|
|DIP|The Dependency Inversion Principle|Depend on abstractions, not on concretions.|
:earth_asia: [鮑伯叔叔達康](http://butunclebob.com/ArticleS.UncleBob.PrinciplesOfOod)
## ★ __++**SRP = Single-Responsibility Principle**++__
:arrow_right: 藉由將介面中不相關的責任(responsibility)移到另外的介面中,也就是說不要讓一個介面『參雜(耦合)』一個以上的責任,基本概念就是 responsibility assignment(責任分配)的問題。
:arrow_right: 英文解釋:
>If a class has more than one responsibility, then the responsibilities become coupled. Changes to one responsibility may impair or inhibit the ability of the class to meet the others. This kind of coupling leads to fragile designs that break in unexpected ways when changed.
:earth_asia: http://teddy-chen-tw.blogspot.tw/2011/12/3.html
## ★ __++**據說老師會考的,據說**++__
1. visitor和factory很像,多新增一個method就要大改
2. abstract factory: 除了產生物件,還要知道每個物件之間的關係;factory method: 只產生物件
3. visitor深入結構,但沒動到結構;strategy深入結構,且可改變內部結構(內層);decorator未深入結構,且不改變結構(外層)
4. Mediator和observer的structure圖一樣,但Mediator必須和所有widget實作,變得無法reusable
5. SRP PDF,第112頁,modem只有兩個責任,send+recv和dial+handup
6. DIP PDF,3個bad design,很困難改變(僵硬(Rigidity))、易碎(fargility)、不好重複使用(immobility)
7. adapter必考,要看implementation第二點和圖
## ★ __++**Download**++__
:earth_asia: [2015 POSD_FINAL 考古題](https://drive.google.com/open?id=0B1bjhJN52iPcanF1UC1SUGFVZ1E)
:earth_asia: [Design Pattern CD (GoF)](https://drive.google.com/open?id=0B1bjhJN52iPcZW1UYkUxbU90Tms)
###### tags: `NTUT` `POSD`