设计模式 抽象工厂模式

1、abstractfactory.h

#ifndef ABSTRACTFACTORY_H
#define ABSTRACTFACTORY_H

#include <iostream>
#include <string>
using namespace std;

class Linux //产品, AbstractProductA
{
public:
    virtual ~Linux();
    virtual void Start() = 0; //产品使用公共接口
};

class LinuxMobile : public Linux //Product1 for AbstractProductA
{
public:
    LinuxMobile();
    virtual ~LinuxMobile();
    virtual void Start();
};

class LinuxPC : public Linux //Product2 for AbstractProductA
{
public:
    LinuxPC();
    virtual ~LinuxPC();
    virtual void Start();
};

class Windows //产品, AbstractProductB
{
public:
    virtual ~Windows();
    virtual void Start() = 0; //产品使用公共接口
};

class WindowsMobile : public Windows //Product1 for AbstractProductB
{
public:
    WindowsMobile();
    virtual ~WindowsMobile();
    virtual void Start();
};

class WindowsPC : public Windows //Product2 for AbstractProductB
{
public:
    WindowsPC();
    virtual ~WindowsPC();
    virtual void Start();
};

class Factory //工厂,AbstractFactory
{
public:
    virtual ~Factory();
    virtual Linux* CreateLinux() = 0; //产品族有个产品组件
    virtual Windows* CreateWindows() = 0; //产品族有个产品组件
};

class MobileFactory : public Factory //ConcreteFactory1
{
public:
    MobileFactory();
    virtual ~MobileFactory();
    virtual Linux* CreateLinux();
    virtual Windows* CreateWindows();
};

class PCFactory : public Factory //ConcreteFactory2
{
public:
    PCFactory();
    virtual ~PCFactory();
    virtual Linux* CreateLinux();
    virtual Windows* CreateWindows();
};
#endif // ABSTRACTFACTORY_H

2、abstractfactory.cpp

#include "abstractfactory.h"

Linux::~Linux(){};

LinuxMobile::LinuxMobile()
{
    cout << "create linux mobile." << endl;
}

LinuxMobile::~LinuxMobile(){}

void LinuxMobile::Start()
{
    cout << "linux mobile start." << endl;
}

LinuxPC::LinuxPC()
{
    cout << "create linux PC." << endl;
}

LinuxPC::~LinuxPC(){}

void LinuxPC::Start()
{
    cout << "linux PC start." << endl;
}

Windows::~Windows(){}

WindowsMobile::WindowsMobile()
{
    cout << "create windows mobile." << endl;
}

WindowsMobile::~WindowsMobile(){}

void WindowsMobile::Start()
{
    cout << "windows mobile start." << endl;
}

WindowsPC::WindowsPC()
{
    cout << "create windows PC." << endl;
}

WindowsPC::~WindowsPC(){}

void WindowsPC::Start()
{
    cout << "windows PC start." << endl;
}

Factory::~Factory(){}

MobileFactory::MobileFactory()
{
    cout << "create mobile factory." << endl;
}

MobileFactory::~MobileFactory(){}

Linux* MobileFactory::CreateLinux()
{
    return new LinuxMobile;
}

Windows* MobileFactory::CreateWindows()
{
    return new WindowsMobile;
}

PCFactory::PCFactory()
{
    cout << "create PC factory." << endl;
}

PCFactory::~PCFactory(){}

Linux* PCFactory::CreateLinux()
{
    return new LinuxPC;
}

Windows* PCFactory::CreateWindows()
{
    return new WindowsPC;
}

3、main.cpp
/*
作者:jhluroom弹   QQ:454676244  MSN:jhlu0815@hotmail.com
开发IDE:qt creater
开发环境:QT C++
参考网站:神秘果:http://www.shenmiguo.com/

定义:
提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类。

理解:
1.AbstractFactory是抽象工厂类,声明创建产品族的接口。具体工厂创建具体的产品族(Product Family),可以创建出分属于不同
  产品等级的一个产品族中的所有对象。ConcreteFactory1和ConcreteFactory2分别生产2个不同产品族。举例:手机工厂生产的产品族
  中,产品有Linux系统的手机和Windows系统的手机。PC工厂生产的产品族中,产品有Linux系统的PC和Windows系统的PC。
2.AbstractProductA和AbstractProductB是产品基类。它们的派生类是同产品等级结构(产品类型)的具的体产品。
  举例:Linux的具体产品类型包括Linux手机版和Linux PC版,Windows的具体产品类型包括Windows手机版和Windows PC版。
  产品族和产品等级的关系:

要点:
1.抽象工厂应用场景:
  一个系统要独立于它的产品的创建、组合和表示时。
  一个系统要由多个产品系列中的一个来配置时。
  当你要强调一系列相关的产品对象的设计以便进行联合使用时。
  当你提供一个产品类库,而只想显示它们的接口而不是实现时。
2.新增产品复杂。抽象工厂增加产品组件时,需要更改所有工厂的接口。如增加产品ProductC,则工厂基类和具体工厂需要增加接口CreateProductC。
3. 抽象工厂模式与工厂方法模式的区别。
    a.重点不同。工厂方法模式强调的是不同的创建者根据自身需求去生产不同的具体产品,重点是生产具体产品;而抽象工厂模式则定位为
      “在不指定实体类别的前提下,提供了一个可以创建一系列相关或互相依赖之组件的接口”,重点是创建相关组件。
    b.抽象工厂提供了的“相关组件”可以看成是具体的产品(如ProductA1),抽象工厂模式的“相关组件”可由工厂模式实现。
       ConcreteFactory1.CreateProuductA()生产的具体产品,可以用工厂方法模式实现,即每一个产品用一个工厂方法实现。
    c.工厂方法模式只有一个抽象产品类,而抽象工厂模式有多个。工厂方法模式的具体工厂类只能创建一个具体产品类的实例,
       而抽象工厂模式可以创建多个。
    d. 抽象工厂不能直接创建产品,只能创建工厂,即抽象工厂创建的产品是工厂。虽然它也定义了创建产品的方法,但需要创建出来的
       具体的工厂实现,即抽象工厂创建出的工厂创建产品。
    e.工厂方法采用的是类继承机制(生成一个子类,重写该工厂方法,在该方法中生产一个对象);而抽象工厂采用的是对象组合机制,
       专门定义“工厂”对象来负责对象的创建。对象组合的方式就是把“工厂”对象作为参数传递。

应用:
1.抽象工厂模式可应用:支持多种数据库的数据库接口访问层;界面设计各种风格界面等。
2.源码中通过PC工厂和手机工厂和预装的操作系统产品来举例实现模式。

以上文字说明,从网上整理而来,有可能部分与其他同仁相同,请谅解,希望我们能够共同交流,谢谢!
*/

#include <QtCore/QCoreApplication>

#include "abstractfactory.h"

void Test(Factory* pFactory)
{
    Linux* pLinux = NULL;
    Windows* pWindows = NULL;

    pLinux = pFactory->CreateLinux();
    pLinux->Start();

    pWindows = pFactory->CreateWindows();
    pWindows->Start();

    delete pWindows;
    delete pLinux;
};

int main(int argc, char *argv[])
{
    cout << "=== jhluroom start ========" << endl;

    Factory* pFactory = NULL;

    pFactory = new MobileFactory; //手机工厂,生产手机产品族,种类有Linux和Windows
    Test(pFactory);
    delete pFactory;
    cout << endl;

    pFactory= new PCFactory; //PC工厂,生产PC产品族,种类有Linux和Windows
    Test(pFactory);
    delete pFactory;

    cout << "=== jhluroom finish _^_ ===" << endl;
    return 0;
}

运行结果:
=== jhluroom start ========
create mobile factory.
create linux mobile.
linux mobile start.
create windows mobile.
windows mobile start.
create PC factory.
create linux PC.
linux PC start.
create windows PC.
windows PC start.

=== jhluroom finish _^_ ===


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值