天天看點

工廠模式(簡單工廠,工廠方法,抽象工廠)1.簡單工廠模式:2.工廠方法模式:

1.簡單工廠模式:

     簡單工廠模式又稱靜态工廠方法模式。從命名上就可以看出這個模式一定很簡單。它存

在的目的很簡單:定義一個用于建立對象的接口。

先來看看它的組成:

工廠模式(簡單工廠,工廠方法,抽象工廠)1.簡單工廠模式:2.工廠方法模式:
//抽象産品角色
public interface Fruit
{
    public void get();
}
//具體産品角色
public class Apple implements Fruit
{
    public void get()
    {
        System.out.println("采摘蘋果");
    }
}
public class Banana implements Fruit
{
    public  void get()
    {
        System.out.println("采摘香蕉");
    }
}      
//工廠類角色
public class FruitFactory
{
    public static Fruit getFruit(String type) throws InstantiationException, IllegalAccessException
    {//工廠方法.注意傳回類型為抽象産品角色
//      if(type.equalsIgnoreCase("apple"))
//      {
//              return Apple.class.newInstance();   
//      }
//      else if(type.equalsIgnoreCase("banana"))
//      {
//              return Banana.class.newInstance();
//      }
        try
        {
            Class fruit=Class.forName(type);//type需要類名的全路徑
            return (Fruit) fruit.newInstance();
        } catch (ClassNotFoundException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
 
        System.out.println("找不到相應的執行個體化類");
        return null;
    }
}      
public class MainClass
{
 
    /**
     * @param args
     * @throws IllegalAccessException 
     * @throws InstantiationException 
     */
    public static void main(String[] args) throws InstantiationException, IllegalAccessException
    {
        // TODO Auto-generated method stub
        //簡單工廠模式
//      Fruit apple=FruitFactory.getFruit("apple");
//      apple.get();
//      Fruit banana=FruitFactory.getFruit("banana");
//      banana.get();
        Fruit apple=FruitFactory.getFruit("design.pattern.simplefactory.Apple");//注意是全路徑
        apple.get();
        Fruit banana=FruitFactory.getFruit("design.pattern.simplefactory.Banana");
        banana.get();
    }
 
}      

2.工廠方法模式:

 工廠方法模式去掉了簡單工廠模式中工廠方法的靜态屬性,使得它可以被子類繼承。這

樣在簡單工廠模式裡集中在工廠方法上的壓力可以由工廠方法模式裡不同的工廠子類來分

擔。

工廠模式(簡單工廠,工廠方法,抽象工廠)1.簡單工廠模式:2.工廠方法模式:
//抽象産品角色,具體産品角色與簡單工廠模式類似,隻是變得複雜了些,這裡略。
//抽象工廠角色
public interface FruitFactoryInterface
{
    public Fruit getFruit();
}
public class OrangeFactory implements FruitFactoryInterface
{
 
    @Override
    public Fruit getFruit()
    {
        // TODO Auto-generated method stub
        return new Orange();
    }
 
}
public class PearFactory implements FruitFactoryInterface
{
 
    @Override
    public Fruit getFruit() 
    {
        // TODO Auto-generated method stub
        return new Pear();
    }
 
}
//應該和具體産品形成對應關系...
public class MainClass
{
 
    /**
     * @param args
     * @throws IllegalAccessException 
     * @throws InstantiationException 
     */
    public static void main(String[] args) throws InstantiationException, IllegalAccessException
    {
        // TODO Auto-generated method stub
        FruitFactoryInterface ffi_0=new PearFactory();//工廠方法模式
        Fruit pear=ffi_0.getFruit();
        pear.get();
        
        FruitFactoryInterface ffi_1=new OrangeFactory();
        Fruit orange=ffi_1.getFruit();
        orange.get();
    }
 
}      

缺點

可以看出工廠方法的加入,使得對象的數量成倍增長。當産品種類非常多時,會出現大量的與之對應的工廠對象,這不是我們所希望的。

解決辦法

為了避免這種情況,可以考慮使用簡單工廠模式與工廠方法模式相結合的方式來減少工廠類:即對于産品樹上類似的種類(一般是樹的葉子中互為兄弟的)使用簡單工廠模式來實作。

簡單工廠模式與工廠方法模式真正的避免了代碼的改動了?

沒有。在簡單工廠模式中,新産品的加入要修改工廠角色中的判斷語句;而在工廠方法模式中,要麼将判斷邏輯留在抽象工廠角色中,要麼在客戶程式中将具體工廠角色寫死(就象上面的例子一樣)。而且産品對象建立條件的改變必然會引起工廠角色的修改。

面對這種情況,Java 的反射機制與配置檔案的巧妙結合突破了限制——這在Spring 中完美的展現了出來。

小結

工廠方法模式仿佛已經很完美的對對象的建立進行了包裝,使得客戶程式中僅僅處理抽

象産品角色提供的接口。那我們是否一定要在代碼中遍布工廠呢?大可不必。也許在下面情

況下你可以考慮使用工廠方法模式:

1) 當客戶程式不需要知道要使用對象的建立過程。

2) 客戶程式使用的對象存在變動的可能,或者根本就不知道使用哪一個具體的對象。

3.抽象工廠模式

 先來認識下什麼是産品族: 位于不同産品等級結構中,功能相關聯的産品組成的家族。

還是讓我們用一個例子來形象地說明一下吧。

例子中的NorthFruit 和SouthFruit 就是兩個産品樹(産品層次結構);而如圖所示的

NorthGrape 和NorthPine 就是一個産品族。他們都可以放到水果家族中,是以功能

有所關聯。同理SouthGrape 和SouthPine 也是一個産品族。

可以說,抽象工廠模式和工廠方法模式的差別就在于需要建立對象的複雜程度上。而且

抽象工廠模式是三個裡面最為抽象、最具一般性的。

抽象工廠模式的用意為:給用戶端提供一個接口,可以建立多個産品族中的産品對象

而且使用抽象工廠模式還要滿足一下條件:

工廠模式(簡單工廠,工廠方法,抽象工廠)1.簡單工廠模式:2.工廠方法模式:

來看看抽象工廠模式的各個角色(和工廠方法的如出一轍):

工廠模式(簡單工廠,工廠方法,抽象工廠)1.簡單工廠模式:2.工廠方法模式:
//抽象産品角色,具體産品角色與簡單工廠模式類似,隻是變得複雜了些,這裡略。
//抽象工廠角色
public interface AbFruitFactory
{
    public Fruit getGrape();
    public Fruit getPine();
}      
public class NorthFruitFactory implements AbFruitFactory
{//具體工廠角色
 
    @Override
    public Fruit getGrape()
    {
        // TODO Auto-generated method stub
        return new NorthGrape();
    }
 
    @Override
    public Fruit getPine()
    {
        // TODO Auto-generated method stub
        return new NorthPine();
    }
 
}      
public class SouthFruitFactory implements AbFruitFactory
{
 
    @Override
    public Fruit getGrape()
    {
        // TODO Auto-generated method stub
        return new SouthGrape();
    }
 
    @Override
    public Fruit getPine()
    {
        // TODO Auto-generated method stub
        return new SouthPine();
    }
 
}      
public class MainClass
{
 
    /**
     * @param args
     * @throws IllegalAccessException 
     * @throws InstantiationException 
     */
    public static void main(String[] args) throws InstantiationException, IllegalAccessException
    {
        // TODO Auto-generated method stub
        AbFruitFactory Abff_0=new NorthFruitFactory();//抽象工廠方法
        Fruit grape_N=Abff_0.getGrape();
        grape_N.get();
        
        AbFruitFactory Abff_1=new SouthFruitFactory();
        Fruit grape_S=Abff_1.getGrape();
        grape_S.get();
        
        Fruit pine_N=Abff_0.getPine();
        pine_N.get();
        
        Fruit pine_S=Abff_1.getPine();
        pine_S.get();
    }
 
}