Design Pattern學習筆記 --- 工廠模式(-)

工廠模式從目的準則來劃分:屬於建立型模式;設計模式

具體可分如下兩種設計模式:app

①:工廠方法(Factory Method)模式ide

②:抽象工廠(Abstact Factory)模式測試

一:工廠方法(Factory Method)模式ui

工廠方法模式又可細分爲兩種(簡單工廠模式[Simple Factory]和工廠方法模式)設計

簡單工廠模式接口

①:類圖產品

 

②場景描述:
    老張承包了一大塊果園,這個果園既種植蘋果,也種植香蕉.
    小王是一個水果經營商,從老張的果園採購水果.
    當小王向老張採購時,他確定要告訴老張要購買那種具體類型的水果.(apple or banana).it

③Mapping:
    果園 --- 簡單工廠模式中的工廠角色
    水果 --- 抽象產品角色
    蘋果,香蕉 --- 具體產品角色
    小王 --- Client;class

④代碼實現:

/**
 * 簡單工廠模式中的工廠角色
 */
package com.skywares.factorydesign.simplefactory;

/**
 * @author hubert
 *
 */
public class FruitVendorFactory {
   
    public Fruit supplyFruit(String fruitName)
    {
        if("apple".equalsIgnoreCase(fruitName))
        {
            return new Apple();
        }
       
        if("banana".equalsIgnoreCase(fruitName))
        {
            return new Banana();
        }
       
        System.out.println("cann't supply");
        return null;
    }
}

/**
 * 簡單工廠模式中的抽象產品角色
 */
package com.skywares.factorydesign.simplefactory;

/**
 * @author hubert
 *
 */
public interface Fruit {
    void supply();
}
 

/**
 * 簡單工廠模式中的具體產品角色
 */
package com.skywares.factorydesign.simplefactory;

/**
 *
 * @author hubert
 *
 */
public class Apple implements Fruit {

    static
    {
        System.out.println("I'm Apple!");
    }
   
    @Override
    public void supply() {
        System.out.println("supply apple");
    }

}

/**
 * 簡單工廠模式中的具體產品角色
 */
package com.skywares.factorydesign.simplefactory;

/**
 *
 * @author hubert
 *
 */
public class Banana implements Fruit {

    static
    {
        System.out.println("I'm banana!");
    }
   
    @Override
    public void supply() {
        System.out.println("supply banana");
    }

}

/**
 * 簡單工廠模式中的客戶調用類;
 */
package com.skywares.factorydesign.simplefactory;

/**
 * @author hubert
 *
 */
public class Client {

    /**
     * @param args
     */
    public static void main(String[] args) {
        FruitVendorFactory fruitFactory = new FruitVendorFactory();
        Fruit fruit = fruitFactory.supplyFruit("apple");

        if(fruit != null)
        {
            fruit.supply();
        }
       
    }

}

⑤:能夠改進的地方:

a:若是老張擴展了業務,又種植pear(梨), 那麼FruitVendorFactory須要作修改;不利於擴展;

b:全部的邏輯都集中在了FruitVendorFactory這個類,須要作大量的判斷;

因而就有了另外一種想法,老張把業務分開;分別創建ApplectFactory, BananaFactory,和PearFactory,那麼小王只須要向每一個特定的Factory下單就能夠了.就有了工廠方法模式.

二: 工廠方法模式:

①:UML圖:

 ②場景描述:
    老張承包了更多的果園(三塊),分別用來種Apple,Banana,Pear;並讓三個小弟經營.
    小王是一個水果經營商,他不帶直接找老張訂購水果,而是向老張的三個小弟來訂單水果.
    當小王去向不一樣的果園,他再也不須要指定要那種水果了,只能採購既定的水果.

③Mapping:
    果園 --- 工廠方法模式中的抽象工廠角色

     AppleFactory, BananaFactory, PearFactory -- 工廠方法模式中的具體工廠角色.
    水果 --- 抽象產品角色
    蘋果,香蕉,Pear --- 具體產品角色
    小王 --- Client;

④:代碼實現:

  /**
 *  抽象工廠接口
 */
package com.skywares.factorydesign.factory;

import com.skywares.factorydesign.product.Fruit;

/**
 * @author hubert
 *
 */
public interface Factory {
    Fruit supplyFruit();
}

package com.skywares.factorydesign.factory.imp;

import com.skywares.factorydesign.factory.Factory;
import com.skywares.factorydesign.product.Fruit;
import com.skywares.factorydesign.product.imp.Apple;

/**
 * @author hubert
 *
 */
public class AppleFactory implements Factory {
    @Override
    public Fruit supplyFruit() {
        return new Apple();
    }
}

/**
 * 具體產品工廠
 */
package com.skywares.factorydesign.factory.imp;
import com.skywares.factorydesign.factory.Factory;
import com.skywares.factorydesign.product.Fruit;
import com.skywares.factorydesign.product.imp.Banana;

public class BananaFactory implements Factory {
    @Override
    public Fruit supplyFruit() {
        return new Banana();
    }
}

/**
 * 具體產品工廠
 */
package com.skywares.factorydesign.factory.imp;

import com.skywares.factorydesign.factory.Factory;
import com.skywares.factorydesign.product.Fruit;
import com.skywares.factorydesign.product.imp.Pear;

public class PearFactory implements Factory {
    @Override
    public Fruit supplyFruit() {
        return new Pear();
    }
}

/**
 * 測試工廠方法模式
 */
package com.skywares.factorydesign.factorymethod;

import com.skywares.factorydesign.factory.Factory;
import com.skywares.factorydesign.factory.imp.AppleFactory;
import com.skywares.factorydesign.product.Fruit;

/**
 * @author hubert
 *
 */
public class FactoryMethodClient {
    public static void main(String[] args) {
        Factory fruitFactory = new AppleFactory();
        Fruit fruit = fruitFactory.supplyFruit();
        fruit.supply();
    }
}

⑤:假如,每一個果園都擴大了本身的產品範圍,再也不只是單獨地銷售水果,還銷售果汁,若是是按照工廠方法模式的作法,要新定義一個生產果汁的抽象工廠,再定義具體的工廠,這樣會形成類的爆炸,因而抽象工廠模式出爐了.

 

三:抽象工廠模式

①:UML圖

②:場景介紹:

較之工廠方法模式,每一個果園都新增了生產果汁的功能(AppleBeverage,BananaBeverage,PearBeverage);

③:mapping:

Fruint,Beverage分別表明抽象產品角色

AbstactFactory表明抽象工廠角色;

ConcreteFactoryA,ConcreteFactoryB,ConcreteFactoryC分別表明具體的工廠;

Apple,Banana,Pear,AppleBeverage,BananaBeverage,PearBeverage表明具體的產品.

④:代碼實現:

/**  * 抽象工廠模式中的抽象工廠類  */ package com.skywares.factorydesign.factory; import com.skywares.factorydesign.product.Beverage; import com.skywares.factorydesign.product.Fruit; /**  * @author hubert  *  */ public interface AbstactFactory {     Fruit supplyFruit();     Beverage supplyBeverage(); } /**  * 抽象工廠模式中的具體工廠角色  */ package com.skywares.factorydesign.factory.imp; import com.skywares.factorydesign.factory.AbstactFactory; import com.skywares.factorydesign.product.Beverage; import com.skywares.factorydesign.product.Fruit; import com.skywares.factorydesign.product.imp.Apple; import com.skywares.factorydesign.product.imp.AppleBeverage; /**  * @author hubert  *  */ public class ConcreteAppleFactory implements AbstactFactory {     @Override     public Fruit supplyFruit() {         return new Apple();     }     @Override     public Beverage supplyBeverage() {         return new AppleBeverage();     } } /**  * 抽象工廠模式中的具體工廠角色  */ package com.skywares.factorydesign.factory.imp; import com.skywares.factorydesign.factory.AbstactFactory; import com.skywares.factorydesign.product.Beverage; import com.skywares.factorydesign.product.Fruit; import com.skywares.factorydesign.product.imp.Banana; import com.skywares.factorydesign.product.imp.BananaBeverage; /**  * @author hubert  *  */ public class ConcreteBananaFactory implements AbstactFactory {     @Override     public Fruit supplyFruit() {         return new Banana();     }     @Override     public Beverage supplyBeverage() {         return new BananaBeverage();     } } /**  * 抽象工廠模式中的具體工廠角色  */ package com.skywares.factorydesign.factory.imp; import com.skywares.factorydesign.factory.AbstactFactory; import com.skywares.factorydesign.product.Beverage; import com.skywares.factorydesign.product.Fruit; import com.skywares.factorydesign.product.imp.Pear; import com.skywares.factorydesign.product.imp.PearBeverage; /**  * @author hubert  *  */ public class ConcretePearFactory implements AbstactFactory {     @Override     public Fruit supplyFruit() {         return new Pear();     }     @Override     public Beverage supplyBeverage() {         return new PearBeverage();     } } /**  * 測試抽象工廠模式;  */ package com.skywares.factorydesign.abstactfactory; import com.skywares.factorydesign.factory.AbstactFactory; import com.skywares.factorydesign.factory.imp.ConcreteAppleFactory; import com.skywares.factorydesign.product.Beverage; import com.skywares.factorydesign.product.Fruit; /**  * @author hubert  *  */ public class TestAbstractFactory {     /**      * @param args      */     public static void main(String[] args) {         AbstactFactory abstactFactory = new ConcreteAppleFactory();         Fruit fruit = abstactFactory.supplyFruit();         Beverage beverage = abstactFactory.supplyBeverage();         fruit.supply();         beverage.drink();     } }

相關文章
相關標籤/搜索