2015年12月4日 星期五

Bridge


Intent

  • Decouple an abstraction from its implementation so that the two can vary independently.
  • Publish interface in an inheritance hierarchy, and bury implementation in its own inheritance hierarchy.
  • Beyond encapsulation, to insulation

Problem

"Hardening of the software arteries" has occurred by using subclassing of an abstract base class to provide alternative implementations. This locks in compile-time binding between interface and implementation. The abstraction and implementation cannot be independently extended or composed.

有時因不同原因
導致abstract class 和implementation 相依性太重
無法讓各自獨自發展。

Motivation:






Structure:



Adapter Pattern



Intent(變壓器、HDMI 轉接頭)

Convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn't otherwise because of incompatible interfaces.
Wrap an existing class with a new interface.
Impedance match an old component to a new system

Problem

An "off the shelf" component offers compelling functionality that you would like to reuse, but its "view of the world" is not compatible with the philosophy and architecture of the system currently being developed.














2015年12月1日 星期二

Structural Design Pattern



在寫程式中
常常需要reuse 相同的component
但因為interface 的不同,
常常需要前置及後置處理
讓程式可以運作



Adapter
Match interfaces of different classes
讓不同的interface 的class 可以互相運作

Bridge
Separates an object’s interface from its implementation
讓implementation 及abstract class 可以互相分離
各自獨立變化
但同時也造成系統的複雜度

Composite
A tree structure of simple and composite objects
Decorator
Add responsibilities to objects dynamically
Facade
A single class that represents an entire subsystem
Flyweight
A fine-grained instance used for efficient sharing
Private Class Data
Restricts accessor/mutator access
Proxy
An object representing another object

Discussion:
1. Adapter 是在舊系統和新的介面串接時,很常會用到的pattern
bridge是在設計階段,為了簡化架構,可以讓abstract class 和implementation 可以獨立變化的pattern

2. 

Object Pool


Object Pool:


Def:

Object pooling can offer a significant performance boost; it is most effective in situations where the cost of initializing a class instance is high, the rate of instantiation of a class is high, and the number of instantiations in use at any one time is low.



Singleton

Singleton:




Def: 

  • Ensure a class has only one instance, and provide a global point of access to it. 
  • Encapsulated "just-in-time initialization" or "initialization on first use".
當建立db connection 耗資源時
我們就會考慮用singleton pattern
使用persistent connect

Prototype

Prototype:( copy)


Def: 

  • Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype. 
  • Co-opt one instance of a class for use as a breeder of all future instances. 
  • The new operator considered harmful.
當需要產生的物系眾多時
可以考慮用這個pattern
會比用new  來的快

在factory pattern 及object pool 中
可以考慮這個pattern
來增加程式執行速度

Builder

Builder


Def: 

  • Separate the construction of a complex object from its representation so that the same construction process can create different representations. 
  • Parse a complex representation, create one of several targets.
想像當產生出來的object 越來越多選項
越來越複雜的的同時
就需要利用builder 這個design pattern