博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【设计模式】抽象工厂模式
阅读量:7079 次
发布时间:2019-06-28

本文共 5677 字,大约阅读时间需要 18 分钟。

使用频率:★★★★★

一、什么是抽象工厂模式

就是对一组具有相同主题的工厂进行封装(维基百科解释的很到位);

例如:生产一台PC机,使用工厂方法模式的话,一般会有cpu工厂,内存工厂,显卡工厂...但是使用抽象工厂模式的话,只有一个工厂就是PC工厂,但是一个PC工厂涵盖了cpu工厂,内存工厂,显卡工厂等要做的所有事;

二、补充说明

  • 注意这里的“相同主题”的概念,表示的是同一个产品族,不能将cpu工厂,面粉工厂封装成一个工厂,因为他们不属于同一个产品族;
  • 另外,还有一个产品等级的概念,还是以生产PC机为例,所谓的产品等级指的是不同厂商生产的CPU,如Intel和AMD的CPU,他们是同一个产品等级,如果只涉及产品等级的话,是不需要应用抽象工厂模式,使用工厂方法模式即可;
  • 工厂方法模式解决的范畴是产品等级(AMD处理器,Intel处理器等);抽象工厂模式解决的范畴是产品族等级(联想PC、惠普PC等);

三、角色

  • 抽象工厂
  • 具体工厂
  • 抽象产品
  • 具体产品
  • 产品使用者

说明:

  • 具体工厂“继承”抽象工厂;
  • 具体产品”继承“抽象产品;
  • 每个具体工厂(如PC工厂)包含若干个子工厂方法(如cpu工厂方法、显卡工厂方法...),子工厂方法负责生产对应的具体子产品,所有具体子产品(cpu、内存、显卡...)组合成一个具体产品(如惠普XXX型号PC);
  • 产品使用者使用每个具体工厂生产的具体产品;

四、例子

这里就不用PC这个例子了,继续前一个工厂模式的例子,在上一篇工厂模式的例子中,我们使用的是创建父亲对象这个例子,其中中国父亲和美国父亲指的就是同一个产品等级;

但是当我们要创建一个家庭对象的时候,需要创建父亲对象、母亲对象、孩子对象等等,所谓的父亲、母亲、孩子就构成了一个产品族,中国家庭、美国家庭就是产品族等级;这个时候就需要使用抽象工厂模式了;

类之间的关系图:

代码实现:

先创建抽象产品(抽象母亲、抽象父亲),具体产品(具体母亲、具体父亲):

package com.pichen.dp.creationalpattern.abstractfactory;public interface IMother {    public void printName();}
package com.pichen.dp.creationalpattern.abstractfactory;public interface IFather {    public void printName();}
package com.pichen.dp.creationalpattern.abstractfactory;public class ChineseMother implements IMother{    private String name;    public ChineseMother(String name) {        this.name = name;        System.out.println("create a cn mother.");    }    /**     * @return the name     */    public String getName() {        return name;    }    /**     * @param name the name to set     */    public void setName(String name) {        this.name = name;    }    @Override    public void printName() {        System.out.println(this.getClass().getName() + ":" + this.name);            }}
package com.pichen.dp.creationalpattern.abstractfactory;public class AmericanMother implements IMother{    private String name;    public AmericanMother(String name) {        this.name = name;        System.out.println("create a us mother.");    }    /**     * @return the name     */    public String getName() {        return name;    }    /**     * @param name the name to set     */    public void setName(String name) {        this.name = name;    }    @Override    public void printName() {        System.out.println(this.getClass().getName() + ":" + this.name);            }}
package com.pichen.dp.creationalpattern.abstractfactory;public class ChineseFather implements IFather{    private String name;    public ChineseFather(String name) {        this.name = name;        System.out.println("create a cn father.");    }    /**     * @return the name     */    public String getName() {        return name;    }    /**     * @param name the name to set     */    public void setName(String name) {        this.name = name;    }    @Override    public void printName() {        System.out.println(this.getClass().getName() + ":" + this.name);            }}
package com.pichen.dp.creationalpattern.abstractfactory;public class AmericanFather implements IFather{    private String name;    public AmericanFather(String name) {        this.name = name;        System.out.println("create a us father.");    }    /**     * @return the name     */    public String getName() {        return name;    }    /**     * @param name the name to set     */    public void setName(String name) {        this.name = name;    }    @Override    public void printName() {        System.out.println(this.getClass().getName() + ":" + this.name);            }}

创建一个抽象家庭工厂接口:

package com.pichen.dp.creationalpattern.abstractfactory;/** *  * abstract factory * father + mother + sister + ... = Product Family * cnfather + usfather + ukfather + ... = Product grade  //factory method * 〈功能详细描述〉 * @author    pi chen * @version   cp-lib V1.0.0, 2015年11月25日 * @see        * @since     cp-lib V1.0.0 */public interface IFamilyFactory {    public IFather createFather(String name);    public IMother createMother(String name);}

分别创建具体的中国家庭工厂和美国家庭工厂:

package com.pichen.dp.creationalpattern.abstractfactory;public class ChineseFamilyFactory implements IFamilyFactory{    @Override    public IFather createFather(String name) {        return new ChineseFather(name);    }    @Override    public IMother createMother(String name) {        return new ChineseMother(name);    }}
package com.pichen.dp.creationalpattern.abstractfactory;public class AmericanFamilyFactory implements IFamilyFactory{    @Override    public IFather createFather(String name) {        return new AmericanFather(name);    }    @Override    public IMother createMother(String name) {        return new AmericanMother(name);    }}

创面产品使用者main方法:

package com.pichen.dp.creationalpattern.abstractfactory;public class Main {    public static void main(String[] args) {        IFamilyFactory cnFamilyFactory = new ChineseFamilyFactory();        IFamilyFactory usFamilyFactory = new AmericanFamilyFactory();                IFather cnFather = cnFamilyFactory.createFather("cn father-test");        IMother cnMother = cnFamilyFactory.createMother("cn mother-test");                IFather usFather = usFamilyFactory.createFather("us father-test");        IMother usMother = usFamilyFactory.createMother("us mother-test");                cnFather.printName();        cnMother.printName();        usFather.printName();        usMother.printName();    }}

结果打印如下,功能正常:

create a cn father.
create a cn mother.
create a us father.
create a us mother.
com.pichen.dp.creationalpattern.abstractfactory.ChineseFather:cn father-test
com.pichen.dp.creationalpattern.abstractfactory.ChineseMother:cn mother-test
com.pichen.dp.creationalpattern.abstractfactory.AmericanFather:us father-test
com.pichen.dp.creationalpattern.abstractfactory.AmericanMother:us mother-test

转载于:https://www.cnblogs.com/chenpi/p/5156801.html

你可能感兴趣的文章
慧荣科技发布全球首款商用SD 5.1控制器解决方案
查看>>
spark 计算结果写入mysql 案例及常见问题解决
查看>>
杨博:医疗大数据服务任重道远
查看>>
Mysql复制技术中的主键冲突
查看>>
有源音箱和无源音箱的主要区别
查看>>
浅谈性能测试、压力测试和负载测试
查看>>
视频02.MediaPlayer和VideoView,TextureView的使用
查看>>
thinkphp-union
查看>>
Android JSON解析库Gson和Fast-json的使用对比和图书列表小案例
查看>>
日本展示机器人Erica,看脸还不错
查看>>
一步一步搞定InfoPath(01)——提交表单到Access数据库
查看>>
SET 语句选项
查看>>
ubuntu ufw防火墙
查看>>
Kali 2.0使用SSH进行远程登录
查看>>
类的属性总结
查看>>
在64位Win7下为HP LaserJet 1012安装网络打印机驱动
查看>>
电子邮件地址策略
查看>>
如何快速安装Webmin(linux系统web管理配置工具)
查看>>
PHP带头大哥学习的三部曲!
查看>>
apache忽略文件后缀
查看>>