创建型模式5之2-AbstractFactory抽象工厂模式例子理解

来源:互联网 发布:什么虚拟机可以装mac 编辑:程序博客网 时间:2024/05/17 06:47

例子一

package com.AbstractFactoryModel;/** *  抽象工厂:多个抽象产品类,派生出多个具体产品类;一个抽象工厂类,派生出多个具体工厂类;每个具体工厂类可创建多个具体产品类的实例。    即提供一个创建一系列相关或相互依赖对象的接口,而无需指定他们的具体的类。“一对多”的关系。    抽象工厂:(1) 一个系统不应当依赖于产品类实例如何被创建、组合和表达的细节,这对于所有形态的工厂模式都是重要的。(2) 这个系统有多于一个的产品族,而系统只消费其中某一产品族。(3) 同属于同一个产品族的产品是在一起使用的,这一约束必须在系统的设计中体现出来。(4) 系统提供一个产品类的库,所有的产品以同样的接口出现,从而使客户端不依赖于实现。 * @author Administrator * *///定义不同的产品之间的一定具备的标准,用interface实现 //其中的method()方法可看作提取出不同产品的共性,如手机都有类似的功能 interface IProductA {//设想:苹果手机    public void method();//设想:手机生产方法}interface IProductB {//设想:安卓手机    public void method();//假设:手机生产方法}// 实现了产品标准实现的一系列具体产品// 由于已经设计好A1由厂商1生产,故以下输出代码有“厂商x”class ProductA1 implements IProductA {    public void method() {        System.out.println("厂商1    生产ProductA1 ...");//设想:苹果公司设计了6s    }}class ProductA2 implements IProductA {    public void method() {        System.out.println("厂商2    生产ProductA2 ...");//设想:苹果公司设计了6s plus    }}class ProductB1 implements IProductB {    public void method() {        System.out.println("厂商1    生产ProductB1 ...");//设想:华为公司设计了荣耀6    }}class ProductB2 implements IProductB {    public void method() {        System.out.println("厂商2    生产ProductB2 ...");//设想:华为公司设计了荣耀6 plus    }}// 每一种牌子的产品生产工厂,即不同的厂商负责自己牌子产品的生产abstract class Factory1 {    abstract IProductA getProductA1();//设想:富士康公司代工生产了苹果6s    abstract IProductB getProductB1();//设想:富士康公司代工生产了荣耀6}abstract class Factory2 {    abstract IProductA getProductA2();//设想:台电公司代工生产了苹果6s plus    abstract IProductB getProductB2();//设想:台电公司代工生产了荣耀6 plus}// 具体的工厂用来生产相关的产品class ConcreteFactory1 extends Factory1 {    public IProductA getProductA1() {        return new ProductA1();//设想:富士康公司使用苹果的设计来生产了苹果6s    }    public IProductB getProductB1() {        return new ProductB1();//设想:富士康公司使用华为的设计来生产了荣耀6    }}class ConcreteFactory2 extends Factory2 {    public IProductA getProductA2() {        return new ProductA2();//设想:台电公司使用华为的设计来生产了苹果6s plus    }    public IProductB getProductB2() {        return new ProductB2();//设想:台电公司使用华为的设计来生产了华为6s plus    }}// 测试类public class AbstractFactoryModel {    public static void test() {    //设想:富士康公司使用苹果的设计来生产了苹果6s    //设想:富士康公司使用华为的设计来生产了荣耀6        // 厂商1负责生产产品A1、B1        Factory1 factory1 = new ConcreteFactory1();        IProductA productA1 = factory1.getProductA1();        IProductB productB1 = factory1.getProductB1();        productA1.method();        productB1.method();    //设想:富士康公司使用苹果的设计来生产了苹果6s plus    //设想:富士康公司使用华为的设计来生产了荣耀6  plus        // 厂商2负责生产产品A2、B2        Factory2 factory2 = new ConcreteFactory2();        IProductA productA2 = factory2.getProductA2();        IProductB productB2 = factory2.getProductB2();        productA2.method();        productB2.method();    }}

例子二

package com.test.factorymodel;import com.test.factorymodel.AbstractFactory.BigFactory;import com.test.factorymodel.AbstractFactory.SmallFactory;public class AbstractFactory {    public interface IScreen {//设想:屏幕技术    }    public interface IBody {//设想:机身技术    }    public static class BigScreen implements IScreen {//设想:大型屏幕生产        public BigScreen() {            // TODO Auto-generated constructor stub            System.out.println("BigScreen");        }    }    public static class SmallScreen implements IScreen {//设想:小型屏幕生产        public SmallScreen() {            // TODO Auto-generated constructor stub            System.out.println("SmallScreen");        }    }    public static class BigBody implements IBody {//设想:大型机身生产        public BigBody() {            // TODO Auto-generated constructor stub            System.out.println("BigBody");        }    }    public static class SmallBody implements IBody {//设想:小型机身生产        public SmallBody() {            // TODO Auto-generated constructor stub            System.out.println("SmallBody");        }    }    public interface IFactory {//设想:工厂机身和屏幕生产        public IScreen createScreen();        public IBody createBody();    }    public static class BigFactory implements IFactory {//设想:大工厂的大机身和大屏幕生产        @Override        public IScreen createScreen() {            // TODO Auto-generated method stub            return new BigScreen();        }        @Override        public IBody createBody() {            // TODO Auto-generated method stub            return new BigBody();        }    }    public static class SmallFactory implements IFactory {//设想:小工厂的小机身和小屏幕生产        @Override        public IScreen createScreen() {            // TODO Auto-generated method stub            return new SmallScreen();        }        @Override        public IBody createBody() {            // TODO Auto-generated method stub            return new SmallBody();        }    }        public static class AbstractFactory_Customer {        public static void main(String[] args) {            BigFactory bigFactory = new BigFactory();            bigFactory.createBody();            bigFactory.createScreen();            SmallFactory smallFactory = new SmallFactory();            smallFactory.createBody();            smallFactory.createScreen();        }    }}

例子三
这里写图片描述

interface CPU {    void process();}interface CPUFactory {    CPU produceCPU();}class AMDFactory implements CPUFactory {    public CPU produceCPU() {        return new AMDCPU();    }}class IntelFactory implements CPUFactory {    public CPU produceCPU() {        return new IntelCPU();    }}class AMDCPU implements CPU {    public void process() {        System.out.println("AMD is processing...");    }}class IntelCPU implements CPU {    public void process() {        System.out.println("Intel is processing...");    }}class Computer {    CPU cpu;    public Computer(CPUFactory factory) {        cpu = factory.produceCPU();        cpu.process();    }}public class Client {    public static void main(String[] args) {        new Computer(createSpecificFactory());    }    public static CPUFactory createSpecificFactory() {        int sys = 0; // 基于特定要求        if (sys == 0)             return new AMDFactory();        else            return new IntelFactory();    }}

抽象工厂模式的优点
抽象工厂模式除了具有工厂方法模式的优点外,最主要的优点就是可以在类的内部对产品族进行约束。所谓的产品族,一般或多或少的都存在一定的关联,抽象工厂模式就可以在类内部对产品族的关联关系进行定义和描述,而不必专门引入一个新的类来进行管理。

抽象工厂模式的缺点
产品族的扩展将是一件十分费力的事情,假如产品族中需要增加一个新的产品,则几乎所有的工厂类都需要进行修改。所以使用抽象工厂模式时,对产品等级结构的划分是非常重要的。

适用场景
当需要创建的对象是一系列相互关联或相互依赖的产品族时,便可以使用抽象工厂模式。说的更明白一点,就是一个继承体系中,如果存在着多个等级结构(即存在着多个抽象类),并且分属各个等级结构中的实现类之间存在着一定的关联或者约束,就可以使用抽象工厂模式。假如各个等级结构中的实现类之间不存在关联或约束,则使用多个独立的工厂来对产品进行创建,则更合适一点。

总结
无论是简单工厂模式,工厂方法模式,还是抽象工厂模式,他们都属于工厂模式,在形式和特点上也是极为相似的,他们的最终目的都是为了解耦。在使用时,我们不必去在意这个模式到底工厂方法模式还是抽象工厂模式,因为他们之间的演变常常是令人琢磨不透的。经常你会发现,明明使用的工厂方法模式,当新需求来临,稍加修改,加入了一个新方法后,由于类中的产品构成了不同等级结构中的产品族,它就变成抽象工厂模式了;而对于抽象工厂模式,当减少一个方法使的提供的产品不再构成产品族之后,它就演变成了工厂方法模式。
所以,在使用工厂模式时,只需要关心降低耦合度的目的是否达到了。

========================
补充例子:
1。BoyInterface

package com.test.factorymodel.two4abstractfactory;public interface BoyInterface {    public void beMan();}

2。GirlInterface

package com.test.factorymodel.two4abstractfactory;public interface GirlInterface {    public void beWoman();}

3。PersonFactory

package com.test.factorymodel.two4abstractfactory;public interface PersonFactory {    public BoyInterface getBoyInterface();    public GirlInterface getGirlInterface();}

4。ChineseBoy

package com.test.factorymodel.two4abstractfactory;public class ChineseBoy implements BoyInterface {    @Override    public void beMan() {        // TODO Auto-generated method stub        System.out.println("中国男孩");    }}

5。ChineseGirl

package com.test.factorymodel.two4abstractfactory;public class ChineseGirl implements GirlInterface {    @Override    public void beWoman() {        // TODO Auto-generated method stub        System.out.println("中国女孩");    }}

6。AmericanBoy

package com.test.factorymodel.two4abstractfactory;public class AmericanBoy implements BoyInterface {    @Override    public void beMan() {        // TODO Auto-generated method stub        System.out.println("美国男孩");    }}

7。AmericanGirl

package com.test.factorymodel.two4abstractfactory;public class AmericanGirl implements GirlInterface {    @Override    public void beWoman() {        // TODO Auto-generated method stub        System.out.println("美国女孩");    }}

8。AmericanFactory

package com.test.factorymodel.two4abstractfactory;public class AmericanFactory implements PersonFactory {    @Override    public BoyInterface getBoyInterface() {        // TODO Auto-generated method stub        return new AmericanBoy();    }    @Override    public GirlInterface getGirlInterface() {        // TODO Auto-generated method stub        return  new AmericanGirl();    }}

9。ChineseFactory

package com.test.factorymodel.two4abstractfactory;public class ChineseFactory implements PersonFactory {    @Override    public BoyInterface getBoyInterface() {        // TODO Auto-generated method stub        return new ChineseBoy();    }    @Override    public GirlInterface getGirlInterface() {        // TODO Auto-generated method stub        return new ChineseGirl();    }}

10。Test

package com.test.factorymodel.two4abstractfactory;public class Test {    public static void main(String[] args) {        PersonFactory c_facoty = new ChineseFactory();        BoyInterface c_boy = c_facoty.getBoyInterface();        c_boy.beMan();        GirlInterface c_girl = c_facoty.getGirlInterface();        c_girl.beWoman();        System.out.println("***************************");        PersonFactory a_facoty = new AmericanFactory();        BoyInterface a_boy = a_facoty.getBoyInterface();        a_boy.beMan();        GirlInterface a_girl = a_facoty.getGirlInterface();        a_girl.beWoman();    }}

11。结果

中国男孩中国女孩***************************美国男孩美国女孩
0 0
原创粉丝点击