2010年3月10日 星期三

Design Pattern : 06 Prototype


通常我們產生一個instance 通常都會是利用new Class方式
來產生想要的instnace,但如果要產生的物件是一個比較麻煩的東西
譬如說,在畫布上隨手畫出的物件,想要產生多個instance,
這時候,就需要用clone的方式,針對既存的物件原形複製出來

把一個物件class當作原型,丟到manager內當作一個參考
當使用者需要一個instnace時,manager就依照這個原型
複製出一個相同物件出來,傳給需求單位

Prototype Patterng的結構是包含
1.一個Manager Class
2.一個Product 抽象類別:
定義了要在Manager內被呼叫的method
Product內包括了一個createClone()的method
可以對自身複製出一個instance傳出去,至於實作部分就留給其繼承者實作出來即可
3.多個繼承自Product的class(在這範例是StringBox)

其運作流程為
1.利用Manager註冊Product
2.利用Manager產生Product instance
3.操作Product instance
參與者
1.Manager
本身為標準class,非abstract
但裡面method呼叫的是以抽象類別(Product)所訂的method來執行
在這裡面包含了產生product instance的運算邏輯


public class Manager {
    private Hashtable table=new Hashtable();
    public void register(String name,Product p){
        table.put(name,p); 
    }
    public Product create(String protoname){
    Product p=(Product)table.get(protoname);
        return p.createClone();
    }
}


先使用register把Product與設定要產生這product的字串註冊到這manager內
以後就可以利用註冊的字串用create來取得該product 的instance

2.Product(framework)
規範了在Managaer內會被呼叫的method名稱
public abstract class Product implements Cloneable {
public abstract Product createClone();
public abstract void show(String s);
}
在Java內,Product一定要先implements Cloneable


createClone()就是clone自己產生instance的地方,這部分留給繼承者實作
3.StringBox
public class StringBox extends Product {
    public Product createClone(){
        Product p=null;
        try{
            p=(Product)clone();
        }catch(CloneNotSupportedException e){
            e.printStackTrace();
        }
        return p;
    }
}


4.Main(外部應用程式)


Manager manager=new Manager();//產生manager
manager.register("my-",(Product)new StringBox('-'));//註冊product到manager上
Product p=manager.create("my-");//利用manager產生product instance
p.show("hello world");//操作product

2010年3月9日 星期二

Design Pattern : 05 Singleton


當在整個程式裡,我們對於某個clas
只需要存在一個instance時,例如整個程式的設定檔
整個通用的css class,這時可使用Singleton Pattern達到這個目的
參與者
1.Singleton
只能透過 class的 static method getInstance來取得instance
不能在外部使用new 來產生instance,所以將建構子設定為private


public class Singleton{
    private static Singleton _singleton=new Singleton('myid')
    private String myid;//這是instance的var
    private static String myVer="Sample";//這是static var
    private Singleton(String myid){
        this.myid=myid;
        showID();
    }
    private void showID(){
        System.out.println(this.myid)
    }
    public static Singleton getInstance(){
        System.out.println(myVer);
        return _singleton();
    }
}

2.Main 外部主程式


Singleton.getInstance();


PS:在static method內,只能夠呼叫使用staic variable或method
建構子是屬於instance method,可以呼叫使用static或instance method
在使用Vector來儲存物件,記得要宣告內存的類型
Vector myv=new Vector();

2010年3月8日 星期一

Design Pattern : 04 Factory

要建立一個可以用來產生product 的factory class
想要做到,不管什麼product都可以套用相同的產生流程
也就是說一個factory可以用來產生不同的product
這時候就可以用Factory Pattern

定義好抽象的Factory類別與Product類別當框架Framework
在Factory定義實體method產生Product物件
在外部程式要利用Factory產生Product

都是利用上層Framework設定好抽象的factory與product
所提供的public method

但真正內部運作的是分別繼承自factory與product的realFactory
與realProduct

如果要產生不同的product,就可以分別產生不同的realFactory與
realProduct
在不變動外部Main程式狀況下 即可直接使用
參與者
1.Factory (屬framwork)
在Factory class內是用到了Templete Pattern,
提供外部呼叫使用的public method(create),是一個實體的method
而在這public method內呼叫執行的是一些抽象method
這些method就必須要被繼承的子類別實作出來


public abstract classFactory{
    public Product create(){
        Product p =createProduct();
        registProduct(p);
        return p;
    }
    protected abstract Product createProduct();
    protected abstract void registProduct(Product p);
}

2.RealFactory (instance)
繼承自Factory後,把factory內定一的抽象method都實作出來

public class RealFactory extends Factory{
    protected Product createProduct(){
        return RealProduct();  
    };
    protected abstract void registProduct(Product p){
        .....
    };
}

3.Product (屬framework)
只定義public 抽象method,來規範外界要操作使用product的方法

public abstract class Product{
    public abstract void use();
}

4.RealProcuct(instance)
真正的產品class,繼承自Product,並把Product內定義的
抽象method 實作

public class RealProduct{
    public void use(){
        .....
    }
}

5.Main(外部主程式)
產生一個實體的Factory,雖然是new RealFactory()
但類別宣告,還是宣告在其上層的Factory
利用產生的factory來產生product
真正產生的是realProduct物件
但類別還是宣告在其上層的Product class


Factory factory=new RealFactory();
Product product=factory.creat()
product.use();



ps:在Product的子類別,不提供public的建構子,目的是在於
強制規範product只能經由factory去產生,而不能自行使用
new product()產生出來

2010年3月5日 星期五

Design Pattern : 03 Template


在父類別定義好大綱(也就是運算邏輯),
子類別只要實作method 就可以讓多個class共用相同的運算邏輯
參與者
1.Template 父類別
Template必須為abstract class,不能用interface宣告
因為在template內,負責主要邏輯運算的method 必須要是一個
final method



public abstract class Template{
    public abstract void open();
    public final void display(){
        open();
        open();
        open();
    }
}

sample裡的display method包含了運算邏輯,是一個實體的method,但
在裡面呼叫執行的是抽象 method,這部分的method一定要在子類別內被實做
2.MyDisplay 子類別
子類別繼承Template,並把abstract 部分實作出來



public class MyDisplay{
    public void open(){
        .....
    }
}  

3.Main主程式



Template a=new MyDisplay();
a.display();

主程式執行時,會用到的是包含運算邏輯的method

Abstract class與Interface的差別
1.只要class內有method宣告為abstract,則class就必須宣告為abstract
Interface內所有的method都必須是abstract
也就是說在abstract class內,還可包含有非abstract的method
如果是extend abstract method ,只要 實作abstract method即可
對於非abstract method可直接使用,若要改寫則要override


2.只要是abstract class就不能被直接new 出instance


3.Class一次只可以繼承(extends )一個父class
但可以實作(implement)多個class
亦可同時extends 與implement

2010年3月4日 星期四

Design Pattern : 02 Adapter


當在開發Class時,在功能上可以利用舊的Class功能
這時候這個新的Class,就可以用Adapter方式去包裝
也就是說
在Adapter內定義新的method,但method內實際是在執行舊Class的method
使用者只知道他呼叫的是Adapter的method,不需知道其內部是如何運作
Adapter的做法,分別可以用繼承或委讓方式來實做
參與者
1.Old Class
原有已經開發好的Class
譬如 原本已有一個print() method
2.Adapter1
使用繼承方式來包裝Old Clas所產生新的Class


class Adapter1 extend Old{
    function Adapter1(){}
    function show(){print()}
}

3.Adapter2
在Class內包含了一個Old Class的instnace,用委讓的方式
來執行真正要做的事


class Adapter2{
    var old:Old
    function Adapter2(){
        old=new Old();
    }
    function show(){
        old.print()
    }
}

4.Main
主要應用程式


//var adapter:Adapter1=new Adapter1('hello');
var adapter:Adapter2=new Adapter2('hello');
adapter.show();