对象模式所属类别简介-对象创建模式
对象创建模式-通过对象创建模式绕开对象创建new,避免new导致的紧耦合。典型模式有factory method,abstract method,prototype,builder
当前模式简介
factory method是abstract method的特例,当只有一种产品情况下,只有一种产品类型的工厂。抽象工厂可以实现不同产品的不同工厂。
需求
设计一个文件分割器。当前文件类型只有二进制文件。点击按钮即可分割,点击另一个按钮实现合并。(相当于分割产品,合并产品)
设计一
无
需求更改
无
设计一更改版本
无
违反原则
无
设计二。
#include "stdafx.h"
#include <iostream>
using namespace std;
class IBuild;
class CBanaryFileB;
class CVedioFileB;
class CPictureFileB;
class ISplitter
{
public:
virtual ~ISplitter(){}
virtual void splitter() = 0;
};
class CBanaryFile : public ISplitter
{
public:
virtual void splitter() { std::cout << "CBanaryFile" << std::endl; }
};
class CVedioFile : public ISplitter
{
public:
virtual void splitter() { std::cout << "CVedioFile" << std::endl; }
};
class CPictureFile : public ISplitter
{
public:
virtual void splitter() { std::cout << "CPictureFile" << std::endl; }
};
//
class ISplitterFactory
{
public:
virtual ISplitter* CreateSplitter() = 0;
};
class CBanaryFileFactory : public ISplitterFactory
{
public:
virtual ISplitter* CreateSplitter() { return new CBanaryFile; }
};
class CVedioFileFactory : public ISplitterFactory
{
public:
virtual ISplitter* CreateSplitter() { return new CVedioFile; }
};
class CPictureFileFactory : public ISplitterFactory
{
public:
virtual ISplitter* CreateSplitter() { return new CPictureFile; }
};
///合成//
class IBuild
{
public:
virtual ~IBuild(){}
virtual void build() = 0;
};
class CBanaryFileB : public IBuild
{
public:
virtual void build() { std::cout << "CBanaryFile" << std::endl; }
};
class CVedioFileB : public IBuild
{
public:
virtual void build() { std::cout << "CVedioFile" << std::endl; }
};
class CPictureFileB : public IBuild
{
public:
virtual void build() { std::cout << "CPictureFile" << std::endl; }
};
class IBuildFactory
{
public:
virtual IBuild* CreateBuild() = 0;
};
class CBanaryFileFactory2 : public IBuildFactory
{
public:
virtual IBuild* CreateBuild() { return new CBanaryFileB; }
};
class CVedioFileFactory2 : public IBuildFactory
{
public:
virtual IBuild* CreateBuild() { return new CVedioFileB; }
};
class CPictureFileFactory2 : public IBuildFactory
{
public:
virtual IBuild* CreateBuild() { return new CPictureFileB; }
};
///
class CMainForm
{
public:
ISplitterFactory *m_factory;
CMainForm(ISplitterFactory *factory){ this->m_factory = factory; }
void BottomClick()
{
ISplitter *splinter = m_factory->CreateSplitter(); //多态new
splinter->splitter();
}
//无法实现,因为ISplitterFactory已经固定,因该使用IBuildFactory创建m_factory
//void BottomClick1()
//{
// IBuild *splinter = m_factory->CreateBuild();
// splinter->build();
//}
};
///合成//
int main() //调用
{
CMainForm main(new CVedioFileFactory);
main.BottomClick();
//main.BottomClick1();
return 0;
}
1.违法设计模式一针对接口而不是实现。111行ISplitterFactory *m_factory;
设计二更改版本
#include "stdafx.h"
#include <iostream>
using namespace std;
//合成产品
///合成//
class IBuild
{
public:
virtual ~IBuild(){}
virtual void build() = 0;
};
class CBanaryFileB : public IBuild
{
public:
virtual void build() { std::cout << "CBanaryFileB" << std::endl; }
};
class CVedioFileB : public IBuild
{
public:
virtual void build() { std::cout << "CVedioFileB" << std::endl; }
};
class CPictureFileB : public IBuild
{
public:
virtual void build() { std::cout << "CPictureFileB" << std::endl; }
};
//合成产品
//分割产品
class ISplitter
{
public:
virtual ~ISplitter(){}
virtual void splitter() = 0;
};
class CBanaryFile : public ISplitter
{
public:
virtual void splitter() { std::cout << "CBanaryFile" << std::endl; }
};
class CVedioFile : public ISplitter
{
public:
virtual void splitter() { std::cout << "CVedioFile" << std::endl; }
};
class CPictureFile : public ISplitter
{
public:
virtual void splitter() { std::cout << "CPictureFile" << std::endl; }
};
//分割产品
class IFactory
{
public:
virtual ISplitter* CreateSplitter() = 0;
virtual IBuild* CreateBuild() = 0;
};
class CBanaryFileFactory : public IFactory
{
public:
virtual ISplitter* CreateSplitter() { return new CBanaryFile; }
virtual IBuild* CreateBuild() { return new CBanaryFileB; }
};
class CVedioFileFactory : public IFactory
{
public:
virtual ISplitter* CreateSplitter() { return new CVedioFile; }
virtual IBuild* CreateBuild() { return new CBanaryFileB; }
};
class CPictureFileFactory : public IFactory
{
public:
virtual ISplitter* CreateSplitter() { return new CPictureFile; }
virtual IBuild* CreateBuild() { return new CBanaryFileB; }
};
///
class CMainForm
{
public:
IFactory *m_factory;//写到属性而不直接new是因为每次点击buttom可以指向不同的对象。如果写到bottom还要传递进来,一般触发bottom没有传递东西
CMainForm(IFactory *factory){ this->m_factory = factory; }
void BottomClick() //分割
{
ISplitter *splinter = m_factory->CreateSplitter(); //多态new
splinter->splitter();
}
void BottomClick2() //合并
{
IBuild *splinter = m_factory->CreateBuild(); //多态new
splinter->build();
}
};
int main() //调用
{
CMainForm main(new CVedioFileFactory);
main.BottomClick();
main.BottomClick2();
return 0;
}
//打印
CVedioFile
CBanaryFileB
设计二比设计一区别
1.违法设计模式一针对接口而不是实现。111行ISplitterFactory *m_factory;
2.在工厂模式的时候,有三种文件类型,但是只有一种分割功能,现在需要增加合并功能。
3.工厂模式是抽象工厂特例,把抽象工厂的变化虚函数只留下一个,就是工厂模式
模式定义
提供一个接口,让该接口负责创建一系列“相关或者相互依赖的对象”,无需指定它们具体的类。
模式结构
要点总结
class IFactory
{
public:
virtual ISplitter* CreateSplitter() = 0;
virtual IBuild* CreateBuild() = 0;
};
缺点:IFactory需要是稳定的,不能随影Create新的产品