工厂模式从目的准则来划分:属于创建型模式;
具体可分以下两种设计模式:
①:工厂方法(Factory Method)模式
②:抽象工厂(Abstact Factory)模式
一:工厂方法(Factory Method)模式
工厂方法模式又可细分为两种(简单工厂模式[Simple Factory]和工厂方法模式)
简单工厂模式
①:类图
②场景描述:
老张承包了一大块果园,这个果园既种植苹果,也种植香蕉. 小王是一个水果经营商,从老张的果园采购水果. 当小王向老张采购时,他肯定要告诉老张要购买那种具体类型的水果.(apple or banana).③Mapping:
果园 --- 简单工厂模式中的工厂角色 水果 --- 抽象产品角色 苹果,香蕉 --- 具体产品角色 小王 --- Client;④代码实现:
/**
* 简单工厂模式中的工厂角色 */ 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(); } }