C++设计模式之抽象工厂(Abstract Factory)模式

抽象工厂模式,是使得用户接口与实际的对象生成相分离,用户使用统一的接口时,系统根据用户的设置或环境,生成出不同的对象。比如我们的电脑系统,创建窗口时统一使用接口CreateWindow,创建滚动条时使用CreateScrollBar,但是系统在我们设置不同的主题风格时,对生成出来的窗口和滚动条有不同的表现,也就是生成了不同的对象。

如下图所示,Factory就是面向用户的接口,只有创建窗口和滚动条的接口,而实际创建工厂我们交给Style类对象m_pStyle。Style是一个抽象类,派生出PMStyle和MotiStyle,这两个派生类只生成自己风格的窗口和滚动条。当用户改变主题风格时,只是让Factory的Style指针指向不同的Style派生类,接口不变。为了让接口统一,我们也将窗口和滚动条抽象出一个基类,新增风格只需增加它的派生类即可。

实现如下:

product.h:

#ifndef __Product_H_
#define __Product_H_

#include <iostream>

using namespace std;

class WindowBase
{
public:
	virtual ~WindowBase(){};

private:

};

class PMWindow : public WindowBase
{
public:
	PMWindow() { cout<<"PMWindows created"<<endl; }
};

class MotiWindow : public WindowBase
{
public:
	MotiWindow() { cout<<"MotiWindow created"<<endl; }
};

class ScrollBarBase
{
public:
	virtual ~ScrollBarBase(){};
};

class PMScrollBar : public ScrollBarBase
{
public:
	PMScrollBar() { cout<<"PMScrollBar created"<<endl; }
};

class MotiScrollBar : public ScrollBarBase
{
public:
	MotiScrollBar() { cout<<"MotiScrollBar created"<<endl; }
};

#endif
style.h:

#ifndef __Style_H_
#define __Style_H_

#include "product.h"

class StyleBase
{
public:
	virtual ~StyleBase(){};

	virtual WindowBase* CreateWindow() = 0;
	virtual ScrollBarBase* CreateScrollBar() = 0;
};

class PMStyle : public StyleBase
{
public:
	PMStyle() {};

	virtual WindowBase* CreateWindow();
	virtual ScrollBarBase* CreateScrollBar();
};

class MotiStyle : public StyleBase
{
public:
	MotiStyle(){};

	virtual WindowBase* CreateWindow();
	virtual ScrollBarBase* CreateScrollBar();
};

inline WindowBase* PMStyle::CreateWindow()
{
	return new PMWindow;
}

inline ScrollBarBase* PMStyle::CreateScrollBar()
{
	return new PMScrollBar;
}

inline WindowBase* MotiStyle::CreateWindow()
{
	return new MotiWindow;
}

inline ScrollBarBase* MotiStyle::CreateScrollBar()
{
	return new MotiScrollBar;
}

#endif
factory.h:

#ifndef __Factory_H_
#define __Factory_H_

#include "product.h"
#include "Style.h"

class Factory
{
public:
	Factory() : m_pStyle(0) {}
	~Factory();
	
	void SetStyle(StyleBase* style);

	WindowBase* CreateWindow();
	ScrollBarBase* CreateScrollBar();

private:
	StyleBase* m_pStyle;
};

Factory::~Factory()
{
	if(m_pStyle)
		delete m_pStyle;
}

void Factory::SetStyle(StyleBase* style)
{
	if(m_pStyle)
		delete m_pStyle;
	m_pStyle =style;
}

WindowBase* Factory::CreateWindow()
{
	if(! m_pStyle)
	{
		cout<<"Unknow style"<<endl;
		return NULL;
	}
	return m_pStyle->CreateWindow();
}

ScrollBarBase* Factory::CreateScrollBar()
{
	if(! m_pStyle)
	{
		cout<<"Unknow style"<<endl;
		return NULL;
	}
	return m_pStyle->CreateScrollBar();
}

#endif
测试如下:

#include "stdafx.h"
#include "Style.h"
#include "product.h"
#include "Factory.h"

int _tmain(int argc, _TCHAR* argv[])
{
	Factory factory;
	factory.CreateWindow();

	factory.SetStyle(new PMStyle);
	WindowBase* w = factory.CreateWindow();
	ScrollBarBase* s = factory.CreateScrollBar();

	delete w;
	delete s;

	factory.SetStyle(new MotiStyle);
	w = factory.CreateWindow();
	s = factory.CreateScrollBar();

	delete w;
	delete s;

	return 0;
}
输出:

Unknow style
PMWindows created
PMScrollBar created
MotiWindow created
MotiScrollBar created

可以看到,设置Style为PMStyle时,调用接口创建出来的都是PMStyle类型的控件。当然,这里也可以让Style基类的创建接口创建出默认风格的控件,当有新的Style派生类时,只需要重写具有新风格控件的接口,没有改变风格的控件就用基类Style的。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Abstract Factory Pattern是一种设计模式,它是创建对象的工厂模式的变体,允许对象在运行时被替换。 Abstract Factory模式提供了一种方法,可以创建一组相关或相互依赖的对象,而不需要明确指定其具体类。 下面是一个C语言的代码示例,该代码实现了一个抽象工厂模式,该模式创建一组车辆: ```c #include <stdio.h> typedef struct IVehicle IVehicle; struct IVehicle { void (*Drive)(IVehicle *); }; typedef struct Car Car; struct Car { IVehicle base; int wheelCount; }; void Car_Drive(IVehicle *vehicle) { Car *car = (Car *)vehicle; printf("Driving a car with %d wheels\n", car->wheelCount); } typedef struct Bike Bike; struct Bike { IVehicle base; int pedalCount; }; void Bike_Drive(IVehicle *vehicle) { Bike *bike = (Bike *)vehicle; printf("Riding a bike with %d pedals\n", bike->pedalCount); } typedef struct IVehicleFactory IVehicleFactory; struct IVehicleFactory { IVehicle *(*CreateVehicle)(IVehicleFactory *); }; typedef struct CarFactory CarFactory; struct CarFactory { IVehicleFactory base; }; IVehicle *CarFactory_CreateVehicle(IVehicleFactory *factory) { Car *car = (Car *)malloc(sizeof(Car)); car->base.Drive = &Car_Drive; car->wheelCount = 4; return (IVehicle *)car; } typedef struct BikeFactory BikeFactory; struct BikeFactory { IVehicleFactory base; }; IVehicle *BikeFactory_CreateVehicle(IVehicleFactory *factory) { Bike *bike = (Bike *)malloc(sizeof(Bike)); bike->base.Drive = &Bike_Drive; bike->pedalCount = 2; return (IVehicle *)bike; } int main(int argc, char *argv[]) { CarFactory carFactory = { { &CarFactory_CreateVehicle } }; IVehicle *vehicle = carFactory.base.CreateVehicle((IVehicleFactory *)

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值