天天看點

【設計模式】簡單工廠模式

使用頻率:★★★★☆

一、什麼是簡單工廠模式

一個工廠方法,依據傳入的參數,生成對應的具體産品對象;

二、補充說明

不屬于23種GOF設計模式;

工廠方法一般設成靜态方法,傳回值一般是抽象類或接口,具體的産品類一般繼承或實作抽象類、接口;

優點:産品使用者不需要關心産品類的建立過程,與具體産品的實作類達到解耦的效果;

缺點:違背"開放--封閉"原則(OCP),因為新增一個産品類的時候,需要修改原先的工廠方法;

适用場合:當工廠類負責建立的對象比較少的時候;

三、角色

  • 抽象産品
  • 具體産品
  • 具體工廠
  • 産品使用者

具體産品繼承抽象産品;

單個具體工廠負責生産不同的具體産品;

産品使用者使用單個具體工廠生産的不同的具體産品;

四、例子

類關系圖:

【設計模式】簡單工廠模式

代碼:

【抽象産品類】首先,定義一個抽象父親類,擁有一個name屬性和列印name的方法,其次還有若幹doSomething方法:

package com.pichen.dp.creationalpattern.simplefactory;

public abstract class Father {
    private String name;

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }
    
    public abstract void printName();
    
    public abstract void doSomething01();
    public abstract void doSomething02();
    public abstract void doSomething03();
}      

【具體産品類】定義具體的中國父親類和美國父親類,繼承抽象父親類,并實作printName等方法:

package com.pichen.dp.creationalpattern.simplefactory;

public class ChineseFather extends Father{
    public ChineseFather(String name) {
        this.setName(name);
    }
    
    
    
    @Override
    public void printName() {
        System.out.println(this.getClass().getName() + ":" + this.getName());
    }
    @Override
    public void doSomething01() {
        // TODO Auto-generated method stub
        
    }
    @Override
    public void doSomething02() {
        // TODO Auto-generated method stub
        
    }
    @Override
    public void doSomething03() {
        // TODO Auto-generated method stub
        
    }

}      
package com.pichen.dp.creationalpattern.simplefactory;

public class AmericanFather extends Father{
    public AmericanFather(String name) {
        this.setName(name);
    }
    
    
    
    @Override
    public void printName() {
        System.out.println(this.getClass().getName() + ":" + this.getName());
    }
    @Override
    public void doSomething01() {
        // TODO Auto-generated method stub
        
    }
    @Override
    public void doSomething02() {
        // TODO Auto-generated method stub
        
    }
    @Override
    public void doSomething03() {
        // TODO Auto-generated method stub
        
    }

}      

【簡單工廠類】定義一個簡單共産類,并定義一個靜态生産方法,根據傳進來的type參數生成對應的具體父親類:

package com.pichen.dp.creationalpattern.simplefactory;

import com.pichen.dp.common.exception.DPException;

public class SimpleFactory {
    public static Father produce(String type, String name) throws DPException{
        if("cn".equals(type)){
            return new ChineseFather(name);
        }else if("us".equals(type)){
            return new AmericanFather(name);
        }else{
            throw new DPException("invalid param~");
        }
    }
}      

【産品使用類】開始測試使用産品,首選寫個makeMoney方法:由工廠類生産一個父親類,并開始賺錢,最後傳回由工廠生産的具體父親類執行個體;

main方法:調用makeMoney方法,并列印具體父親類的name;

具體看代碼:

package com.pichen.dp.creationalpattern.simplefactory;

import com.pichen.dp.common.exception.DPException;

public class Main {
    public static Father makeMoney(String type, String name) {
        Father father = null;
        /* 調用共産方法,建立具體産品類 */
        try {
            father = SimpleFactory.produce(type, name);
        } catch (DPException e) {
            System.out.println("exception happend when produce " + type + " obj~");
            e.printStackTrace();
        }
        
        /* 具體賺錢的步驟 */
        if(father != null){
            father.doSomething01();
            father.doSomething02();
            father.doSomething03();
        }
        
        return father;
    }
    
    public static void main(String[] args) {
        Father cnFather = Main.makeMoney("cn", "cn father");
        
        if (cnFather != null)
            cnFather.printName();
        
        Father usFather = Main.makeMoney("us", "us father ");
        if (usFather != null)
            usFather.printName();
        
        Father apple = Main.makeMoney("fruit","apple");
        if (apple != null)
            apple.printName();
    }
}      

【其它】異常類DPException:

package com.pichen.dp.common.exception;

public class DPException extends Exception{
    /**
     * 
     */
    private static final long serialVersionUID = 8110345414312602844L;

    public DPException(String msg){
        super(msg);
        System.out.println("DPException~");
    }
}      

@Author      風一樣的碼農

@HomePageUrl http://www.cnblogs.com/chenpi/

@Copyright      轉載請注明出處,謝謝~