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

Abstract Factory and Factory method

Abstract Factory



Def: 

  • Provide an interface for creating families of related or dependent objects without specifying their concrete classes. 
  • A hierarchy that encapsulates: many possible "platforms", and the construction of a suite of "products". 
  • The new operator considered harmful.
定義一個interface利用工廠來產生這些物件並符合這個interface 為標準。
當你是在作一個platform ,而不是一個product  時,
就必須考慮這個pattern
它讓你的程式可以和底層分開

example:
Example of Abstract Factory

Factory Method


Def: 

  • Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses. 
  • Defining a "virtual" constructor. 
  • The new operator considered harmful.
當工廠越來越多
就必須為所有的工廠作一個interface
讓所有的工廠都有一個規範
可以共用同一個interface
減少讓client 使用的複雜度

可以多看一些dependency injection
考慮用object pool
讓程式少作initialization
多作clone , 及多用reuse 的class


2015年11月28日 星期六

Creational Design Patterns

creational design pattern

顧名思義
即產生instance 的pattern
為何要用factory 其實跟DIP(dependency inversion principle)有關系
為了要讓高階的程式邏輯不直接和低階的邏輯產生相依性
而讓new 這個動作交由外部的程式來產生



跟create object 有相關的design pattern 如下:

Abstract Factory and Factory method

Builder

Prototype

Singleton

結論:
1. 可以把這些 creational design pattern 當成互補的pattern
Factory pattern 可以當作產生object 的首選方法
Builder 可以利用factory 作每一個step 的實作,進而完成複雜的 component
Prototype 是利用現成的prototype object 來copy 成另一個新的object
Singleton 可以用在每一個pattern 中。

2. 在開發過程中,
一開始常會用factory pattern
等到factory 的靈活性不足時
才會考慮使用prototype, builder 的pattern
雖然更複雜
但也更有彈性。

Reference: 
1. book Learning PHP Design Patterns 
2. https://sourcemaking.com/design_patterns/builder

2015年11月27日 星期五

Object Oriented Programming basic concept



講到OOP

就一定要講到SOLID

S => SRP(Single Responsibility Principle) 單一職責原則O => OCP(Open Close Principle) 開放封閉原則L => LSP(Liskov Substitution Principle) 替換原則I => IAP(Interface Agreegation Principle) 介面分割原則D => DIP(Dependency Inversion Principle) 相依性反轉原則








SRP(Single Responsibility Principle): 

當一把瑞士刀太多功能時,你也不會想要用它

指每一個class 都應該只有一個職責
若一個class 作了太多事情
會導致這個class 的複雜度增加
也會造成日後的maintain cost 增加




OCP(Open Close Principle)

藉由增加新的code 來擴充系統的功能,非修改現有的程式碼來擴充系統的功能,所以新的code加進去時不會對現有的功能 造成影響。
對擴充具有開放性,對修改具有封閉性(單一理由修改)



LSP(Liskov Substitution Principle) 替換原則

子型別必需可替換父型別
玩具鴨需要電池
但野鴨不需要電池
兩者的行為也不同,
就無法用繼承的方式作實作

I => IAP(Interface Agreegation Principle) 介面分割原則

用戶不應被迫相依於他們用不到的函式,
介面屬於client site
不屬於server site
儘可能該介面單純化


想一想
如果介面為了要server 眾多的client 的需求
把介面作的越來越複雜
這樣增加的複雜度是所有的使用者共同承擔的
會造成所有使用者互相影響

D => DIP(Dependency Inversion Principle) 相依性反轉原則

高階邏輯應相依於抽像概念
而非相依於細節
也就是說所有的 new   instance
都應該由factory 來產生
讓所有的物件只相依於interface
而相依於實際上的object

寫程式如何看有沒有功力
就看他寫的code 是否有分階層
讓看的人可以比較清楚的了解程式的邏輯

插頭相依於插座
只有二頭插座侑三頭插座之分
並不會管後面到底是怎麼供電、
電從那裡來的問題


Design pattern overview

在學習design pattern 的同時
需要對它有一個整體性的了解

1. 需了解OOP 的basic concept
2. 對UML 需要有一些認知
3. 需要在寫程式上面多一些經驗

接下來再來談design pattern


  1. Creational Design Patterns
  2. Structural Design Patterns
  3. Behavioral Design Patterns

2015年10月3日 星期六

Why Design Pattern ?

身為一個軟體工程師
為什麼要學Design Pattern ?
它有什麼好處?
學生時期老師都沒有教
它可能不太重要吧

後來進了業界工作
只有在比較嚴格的code review
才會考慮到這個東西一點點

但大多數的同事寫code 還是帶有自已的風格
即你寫一套style , 我寫一套style
你怎麼說同事寫的code 是錯呢??

之後看了一些書
才了解

為了要讓相同的程式可以reuse

為了架構好擴充、好maintain

為了可以一眼看出別人的架構設計

學 OOP and Design Pattern 是不可或缺的


它有什麼好處呢??
對軟體最重要的部分是:

  1. 寫的程式架構簡單 => 複雜的程式,就算寫的再快,也沒人看的懂
  2. 看 code 可以比較快 => 因為function 寫的短,比較好懂
  3. 加新的feature 不會影響到舊的功能 => 利用open close principle (OCP)
  4. 修改容易變動的功能可以改的更快 => 減少時程開發,就是減少成本
  5. 增加產品的生命,降低Redesign 的頻率 => 利用 adaptor and decorator pattern 去達成