Showing posts with label Designed Pattern. Show all posts
Showing posts with label Designed Pattern. Show all posts

4/16/2009

豐富的裝飾者 - Decorator Pattern

咖啡太苦?恩..加糖..
沒有奶味?..那加奶精吧..

我要加甚麼就加甚麼...
Decorator模式使您個體多樣化...

Decorator提供多重物件的裝飾..
事實上,他是非常有用的Pattern..
應當好好學起來...!!
假定一個生活化的案例:
一家工廠製造一台車(Car)...
在製造的過程中...
其實是不斷的加工...
例如加上引擎..
加上輪框..
加上車胎等等...
而引擎也是一個不斷加工後所產生的一件元件....

Decorator Pattern就是像上述這樣的例子...
根據Decorator Pattern的正式定義如下:
Decorator Pattern可以動態將責任加諸在物件上,若要擴充模組,裝飾者提供了比繼承更有彈性的選擇

所謂從責任加諸在物件上..
意味著,責任就是傳遞需要使用的物件,裝飾在裝飾!
....待續一下..拖個稿..=.="

4/13/2009

我對委由(delegate)的基本看法..!!

基本上就是透過delegate...
可以達到彈性化的行為呼叫....
而不必被綁得死死的...
我們可以擁一種簡單的部分代碼表示..
public abstract class Cpu{
protected CpuInstruction cpuInstruction;
protected String type;
//skip...
public void executeInstruction(){
cpuInstruction.execute();
}//end executeInstruction
//skip...
}//end Cpu
//-------------------------------------
public class AmdCpu extends Cpu{
public AmdCpu(string type){
cpuInstrcution=new X86Instruction();
this.type=type;
}//end constructor

public void printType(){
System.out.println("CPU Type: "+this.type);
}//end printType

}//end AmdCpu
由上可以看到executeInstruction()是一個委由(delegate)的方法..
而他的行為決定則是由次類別(AmdCpu)來決定..
因此並不會一開始就被綁死..
這樣做是極具彈性了..
不過上述程式碼的缺點就是"行為實踐"不夠彈性罷了!!

Adapter Pattern的轉接結構

Adapter Pattern對於不同介面之間可以提供相容的轉換..
這是一種非常有用的設計..
例如:
舊版的列舉器轉換成新的迭代裝置(Iterator)...
生活化一點..
例如美規接口透過一個轉接器(Adapter)轉換成歐規接口..
這樣的一種概念其實不難懂...
相對的,他的實作也是很容易...!!

來談談範例:
我們以建立一個匯流排結構PCI介面..
假設並沒有實際設計PCI的具象實踐(Instance)...
僅有一個PCIe的具象實踐..
客乎要求我們必須以PCIe架構建立在早期的PCI架構上..

1.首先我們建立一個介面以作為超型別(Super-Type)之應用..
public interface IPciInterface {
public void link();
public void read();
public void write();
}//end IPciInterface
2.我們得知有一個Pcie具象實踐可以使用,並且他是實作於IPcieInterface介面..
public class Pcie implements IPcieInterface{
public void link(){
System.out.println("Linking!");
}//end link
public void read(){
System.out.println("Reading!");
}//end read

public void write(){
System.out.println("Writing!");
}//end write

}//end Pcie
3.為了符合客戶的需求,我們建立一個PcieToPci的轉接器(Adapter)..
public class PcieToPci implements IPciInterface{

private Pcie pcie;
public PcieToPci(Pcie pcie){
this.pcie=pcie;
}//end constructor

public void link(){
pcie.link();
}//end link

public void read(){
pcie.read();
}//end read

public void write(){
pcie.write();
}//end write

}//end PcieToPci
4.建立好PciToPci轉接器後,再將剩下的Client部分處理..
public class Main{
public static void main(String args[]){
Pci pcie=new Pcie();//Pcie的具象實踐
PcieToPci pcieToPci=new PcieToPci(pcie);

//operations
pcieToPci.link();
pcieToPci.read();
pcieToPci.write();

}//end main

}//end Main

以上是一個非常簡單的Adapter Pattern應用..
他並不難..
學習起來也很快!!...
大概就這樣..

4/10/2009

Singleton Pattern概要..!!

Singleton Pattern目的用來創造獨一無二的資源...
這是非常有用的結構..
例如:
針對撰寫系統..
假設我們透過Factory Pattern來撰寫獨一無二的DataBaseFactory..
來進行對DataBase的統一存取...
這時透過Singleton Pattern便可以建立獨一無二的Factory..
可以看看簡單的代碼範例:
class Singleton{
private Singleton(){

}//end constructor

public static Singleton getInstance(){
return new Singleton();
}//end getInstance

}//end Singleton
由上可以看出幾個要點:
1.constructor必須private防止外部建立多個instance
2.一定要搭配static關鍵字
3.本身是可以返回自有的constructor

但是上述依然不夠完美..
我們把它改寫成如下:
class Singleton{
private static Singleton uniqueInstance;
private Singleton(){

}//end constructor

public static Singleton getInstance(){
if(uniqueInstance==null)
uniqueInstance=new Singleton();
return uniqueInstance;

}//end getInstance

}//end Singleton
可以看出這樣的方式可以做到對Singleton物件的唯一性..
但是如果想到Multi-Thread情況..
他依然還是有問題:
1.容易造成資源取得先後順序問題
2.容易造成資訊錯誤的災難

我們可以透過synchrnized關鍵字來建立critical section..
如下這樣:
class Singleton{
private static Singleton uniqueInstance;
private Singleton(){

}//end constructor

public static synchronized Singleton getInstance(){
if(uniqueInstance==null)
uniqueInstance=new Singleton();
return uniqueInstance;
}//end getInstance
}//end Singleton
但是要注意一點:
synchronized對於Mutli-Thread效能是有衝擊的..
因此必須要考量..!!

我們可以採用雙重鎖來盡量減少多次的同步化
如下做法:
class Singleton{
private volatile static Singleton uniqueInstance;//volatile關鍵字可以表是變數的同一存取
private Singleton(){

}//end constructor

public static Singleton getInstance(){
if(uniqueInstance==null){

synchronized(your_class_name.class){
if(uniqueInstance==null) uniqueInstance=new Singleton();
}//end synchronized

}//end if
return uniqueInstance;
}//end getInstance

}//end Singleton

以上便是很簡單的Singleton應用..!!