抽象工厂
Posted kuikuitage
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了抽象工厂相关的知识,希望对你有一定的参考价值。
抽象工厂是对多个相关联的工厂基类创建接口的合并。
比如有二进制文件,文本文件,图片文件三个类型的三个分割操作对象和三个合并操作对象。
应用不管文件类型,但是要保证文件的分割和合并操作的是同一个类型的文件。那么最好就是把分割操作对象和合并操作对象的创建接口进行统一到一个工厂,保证执行分割和合并的是同一个文件类型。
factory.h
#pragma once
#include "imp.h"
class IFactory
{
public:
virtual ISplitter* CreateSplitter() = 0;
virtual IMerger* CreateMerger() = 0;
virtual ~IFactory(){}
};
class CBinaryFactory : public IFactory
{
public:
ISplitter* CreateSplitter();
IMerger* CreateMerger();
};
class CTxtFactory : public IFactory
{
public:
ISplitter* CreateSplitter();
IMerger* CreateMerger();
};
class CPictureFactory : public IFactory
{
public:
ISplitter* CreateSplitter();
IMerger* CreateMerger();
};
factory.cpp
#include "factory.h"
#include "imp.h"
ISplitter* CBinaryFactory::CreateSplitter()
{
return new BinarySplitter();
}
IMerger* CBinaryFactory::CreateMerger()
{
return new BinaryMerger();
}
ISplitter* CTxtFactory::CreateSplitter()
{
return new TxtSplitter();
}
IMerger* CTxtFactory::CreateMerger()
{
return new TxtMerger();
}
ISplitter* CPictureFactory::CreateSplitter()
{
return new PictureSplitter();
}
IMerger* CPictureFactory::CreateMerger()
{
return new PictureMerger();
}
imp.h
#pragma once
class ISplitter
{
public:
virtual void split() = 0;
virtual ~ISplitter(){}
};
class BinarySplitter : public ISplitter
{
public:
void split();
};
class TxtSplitter : public ISplitter
{
public:
void split();
};
class PictureSplitter : public ISplitter
{
public:
void split();
};
class IMerger
{
public:
virtual void merge() = 0;
virtual ~IMerger(){}
};
class BinaryMerger : public IMerger
{
public:
void merge();
};
class TxtMerger : public IMerger
{
public:
void merge();
};
class PictureMerger : public IMerger
{
public:
void merge();
};
imp.cpp
#include <iostream>
#include "imp.h"
using namespace std;
void BinarySplitter::split()
{
cout << "split BinarySplitter" << endl;
}
void TxtSplitter::split()
{
cout << "split TxtSplitter" << endl;
}
void PictureSplitter::split()
{
cout << "split PictureISplitter" << endl;
}
void BinaryMerger::merge()
{
cout << "merge BinaryMerger" << endl;
}
void TxtMerger::merge()
{
cout << "merge TxtMerger" << endl;
}
void PictureMerger::merge()
{
cout << "merge PictureMerger" << endl;
}
main.cpp
#include <cstdio>
#include <cstdlib>
#include "factory.h"
#include "imp.h"
class Fun
{
IFactory* m_pFactory;
public:
Fun(IFactory* factory)
{
m_pFactory = factory;
}
~Fun(){}
void test()
{
ISplitter *pSplitter = m_pFactory->CreateSplitter();
pSplitter->split();
delete pSplitter;
pSplitter = NULL;
IMerger* pMerger = m_pFactory->CreateMerger();
pMerger->merge();
delete pMerger;
pMerger = NULL;
}
};
int main()
{
IFactory* factory = new CPictureFactory();
Fun form(factory);
form.test();
ISplitter *pSplitter = factory->CreateSplitter();
pSplitter->split();
delete pSplitter;
pSplitter = NULL;
IMerger* pMerger = factory->CreateMerger();
pMerger->merge();
delete pMerger;
pMerger = NULL;
return 0;
}
以上是关于抽象工厂的主要内容,如果未能解决你的问题,请参考以下文章