前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >设计模式:工厂方法模式

设计模式:工厂方法模式

作者头像
王强
发布2018-08-09 17:25:49
4970
发布2018-08-09 17:25:49
举报
文章被收录于专栏:Python爬虫实战

1 概述

工厂方法(Factory Method)模式,也叫虚拟构造器(Virtual Constructor)模式或者多态工厂(Polymorphic Factory)模式,它属于类创建型模式。 在工厂方法模式中,工厂父类负责定义创建产品对象的公共接口,而子类负责生成具体产品对象,这样做的目的是将产品类的实例化延迟到工厂子类中完成,即通过工厂子类来确定究竟应该实例化哪一个具体产品类。

2 图解

现在有A、B、C三种产品,相对应的有三个工厂:工厂A负责生产A产品,工厂B负责生产B产品,工厂C负责生产C产品。这时候客户不需要告诉工厂生产哪种产品了,只需要告诉对应的工厂生产就可以了。

图解工厂模式

工厂方法模式包含如下角色:

  • Factory 抽象工厂角色: 是工厂方法模式的核心,与应用程序无关。任何在模式中创建的对象的工厂类必须实现这个接口。
  • ConcreteFactory 具体工厂角色(图中的FactoryA、FactoryB、FactoryC) 实现抽象工厂接口的具体工厂类,被应用程序调用以创建产品对象。
  • Product 抽象产品角色: 抽象产品角色是所创建所有对象的父类,负责描述所有实例的公共接口。
  • ConcreteProduct 具体产品角色(图中的ProductA、ProductB、ProductC): 实现了抽象产品角色所定义的接口 ,由专门的具体工厂角色创建。

3 优缺点

优点:

  • 子类提供挂钩。基类为工厂方法提供缺省实现,子类可以重写新的实现,也可以继承父类的实现。-- 加一层间接性,增加了灵活性
  • 屏蔽产品类。产品类的实现如何变化,调用者都不需要关心,只需关心产品的接口,只要接口保持不变,系统中的上层模块就不会发生变化。
  • 典型的解耦框架。高层模块只需要知道产品的抽象类,其他的实现类都不需要关心,符合迪米特法则,符合依赖倒置原则,符合里氏替换原则。
  • 多态性:客户代码可以做到与特定应用无关,适用于任何实体类。

缺点:

  • 添加新产品时,需要编写新的具体产品类 ,而且还要提供与之对应的具体工厂类,系统中类的将成对增加,在一定程度上增加了系统的复杂度。
  • 由于考虑到系统的可扩展性,需要引入抽象层,在客户端代码中均使用抽象层进行定义,增加了系统的抽象性和理解难度,且在实现时可能需要用到DOM、反射等技术,增加了系统的实现难度。

4 应用场景

如果一个对象拥有很多子类,那么创建该对象的子类使用工厂模式是最合适的,不但可以面向接口的编程,为维护以及开发带来方便。

如果创建某个对象时需要进行许多额外的操作,如查询数据库然后将查询到的值赋予要创建的对象(单例初始化时使用比较多),或是需要许多额外的赋值等等。如果查看JDK源码中,会发现许多成员变量在对象构造时,通过工厂方法进行创建的。因为这些成员变量本身的创建也很复杂。不可能创建对象时,在该对象的构造方法里创建成员变量然后再赋值给该成员变量。而且使用工厂模式也提高了代码的重用性。

5 实例

FactoryMethodUml

5.1 C++实现

  • product.h
代码语言:javascript
复制
#ifndef PRODUCT_H
#define PRODUCT_H

#include <iostream>

// 产品基类
class Product
{
public:
    Product() {}
    virtual ~Product(){}

    virtual void detail() const
    {
        std::cout << "This is the base class of product." << std::endl;
    }
};

// 产品A
class ProductA : public Product
{
public:
    ProductA() {}
    ~ProductA() {}

    virtual void detail() const
    {
        std::cout << "This is ProductA." << std::endl;
    }
};

// 产品B
class ProductB : public Product
{
public:
    ProductB() {}
    ~ProductB() {}

    virtual void detail() const
    {
        std::cout << "This is ProductB." << std::endl;
    }
};

// 产品C
class ProductC : public Product
{
public:
   ProductC() {}
   ~ProductC() {}

    virtual void detail() const
    {
        std::cout << "This is ProductC." << std::endl;
    }
};

#endif // PRODUCT_H
  • factory.h
代码语言:javascript
复制
#ifndef FACTORY_H
#define FACTORY_H

#include "product.h"

// 工厂基类
class Factory
{
public:
    Factory(){}
    virtual ~Factory(){}

    static Product* produce()
    {
        return NULL;
    }
};

// 工厂A
class FactoryA : public Factory
{
public:
    FactoryA();
    virtual ~FactoryA();

    static Product* produce()
    {
        return new ProductA();
    }
};

// 工厂B
class FactoryB : public Factory
{
public:
    FactoryB();
    virtual ~FactoryB();

    static Product* produce()
    {
        return new ProductB();
    }
};

// 工厂C
class FactoryC : public Factory
{
public:
    FactoryC();
    virtual ~FactoryC();

    static Product* produce()
    {
        return new ProductC();
    }
};

#endif // FACTORY_H
  • main.cpp
代码语言:javascript
复制
#include <iostream>

#include "factory.h"

using namespace std;

int main()
{
    Product* productA = NULL;
    productA = FactoryA::produce();
    productA->detail();
    cout << "==============" << endl;

    Product* productB = NULL;
    productB = FactoryB::produce();
    productB->detail();
    cout << "==============" << endl;

    Product* productC = NULL;
    productC = FactoryC::produce();
    productC->detail();
    cout << "==============" << endl;

    delete productA;
    delete productB;
    delete productC;

    return 0;
}

运行结果

运行结果

5.2 Python实现

代码语言:javascript
复制
#-*- coding: utf-8 -*-

'''
  工厂模式
'''

class Product:
    '''
    产品基类
    '''
    def detail(self):   
        print('This is the base class of produce')

class ProductA(Product):
    '''
    产品A
    '''
    def detail(self):   
        print('This is ProductA.')

class ProductB(Product):
    '''
    产品B
    '''
    def detail(self):   
        print('This is ProductB.')

class ProductC(Product):
    '''
    产品C
    '''
    def detail(self):   
        print('This is ProductC.')

class Factory:
    '''
    工厂类
    '''
    def produce(self):
        return None

class FactoryA(Factory):
    '''
    工厂A
    '''
    def produce(self):
        return ProductA()

class FactoryB(Factory):
    '''
    工厂B
    '''
    def produce(self):
        return ProductB()

class FactoryC(Factory):
    '''
    工厂C
    '''
    def produce(self):
        return ProductC()

def main():
    factoryA = FactoryA()
    productA = factoryA.produce()
    productA.detail()
    print('==================')
    factoryB = FactoryB()
    productB = factoryB.produce()
    productB.detail()
    print('==================')
    factoryC = FactoryC()
    productC = factoryC.produce()
    productC.detail()
    print('==================')

if __name__ == '__main__':
    main()

运行结果:

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2018-03-19,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 C与Python实战 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1 概述
  • 2 图解
  • 3 优缺点
  • 4 应用场景
  • 5 实例
    • 5.1 C++实现
      • 5.2 Python实现
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档