工厂方法模式 Factory Pattern
定义
定义用于创建对象的接口, 让子类决定实例哪一个类
感觉这个是最好理解的, 单例和工厂是一眼看过去你就知道他想干嘛的反正挺好懂也特别好用
案例1
背景
实现一个导出数据的接口, 让客户选择数据导出方式
没有用工厂模式之前
#include <string>
// 实现导出数据的接口, 导出数据的格式包含 xml,json,文本格式txt 后面可能扩展excel格式csv
class IExport {
public:
virtual bool Export(const std::string &data) = 0;
virtual ~IExport(){}
};
class ExportXml : public IExport {
public:
virtual bool Export(const std::string &data) {
return true;
}
};
class ExportJson : public IExport {
public:
virtual bool Export(const std::string &data) {
return true;
}
};
// csv
class ExportTxt : public IExport {
public:
virtual bool Export(const std::string &data) {
return true;
}
};
// class ExportCSV : public IExport {
// public:
// virtual bool Export(const std::string &data) {
// return true;
// }
// };
// =====1
int main() {
std::string choose/* = */;
if (choose == "txt") {
/***/
IExport *e = new ExportTxt();
/***/
e->Export("hello world");
} else if (choose == "json") {
/***/
IExport *e = new ExportJson();
/***/
e->Export("hello world");
} else if (choose == "xml") {
IExport *e = new ExportXml();
e->Export("hello world");
} else if (choose == "csv") {
IExport *e = new ExportXml();
e->Export("hello world");
}
}
这个东西的问题很大, 等到你有100种格式的时候你看看你的main方法有多长效率有多低可读性有多差
浅用一下
#include <string>
// 实现导出数据的接口, 导出数据的格式包含 xml,json,文本格式txt 后面可能扩展excel格式csv
class IExport {
public:
virtual bool Export(const std::string &data) = 0;
virtual ~IExport(){}
};
class ExportXml : public IExport {
public:
virtual bool Export(const std::string &data) {
return true;
}
};
class ExportJson : public IExport {
public:
virtual bool Export(const std::string &data) {
return true;
}
};
class ExportTxt : public IExport {
public:
virtual bool Export(const std::string &data) {
return true;
}
};
class IExportFactory {
public:
virtual IExport * NewExport(/* ... */) = 0;
};
class ExportXmlFactory : public IExportFactory {
public:
IExport * NewExport(/* ... */) {
// 可能有其它操作,或者许多参数
IExport * temp = new ExportXml;
// 可能之后有什么操作
return temp;
}
};
class ExportJsonFactory : public IExportFactory {
public:
IExport * NewExport(/* ... */) {
// 可能有其它操作,或者许多参数
IExport * temp = new ExportJson;
// 可能之后有什么操作
return temp;
}
};
class ExportTxtFactory : public IExportFactory {
public:
IExport * NewExport(/* ... */) {
// 可能有其它操作,或者许多参数
IExport * temp = new ExportTxt;
// 可能之后有什么操作
return temp;
}
};
class ExportData {
public:
ExportData(IExportFactory *factory) : _factory(factory) {}
~ExportData() {
if (_factory) {
delete _factory;
_factory = nullptr;
}
}
bool Export(const std::string &data) { // 稳定的流程 往基类放
IExport * e = _factory->NewExport();
e->Export(data);
}
private:
IExportFactory *_factory;
};
int main() {
ExportData ed(new ExportTxtFactory);
ed.Export("hello world");
return 0;
}
感觉造了贼多个类但是代码可能代码结构会稍微清晰点(虽然我觉得差不多), 但这么写的确是符合工厂模式的
更好一点的做法
#include <string>
// 实现导出数据的接口, 导出数据的格式包含 xml,json,文本格式txt 后面可能扩展excel格式csv
class IExport {
public:
virtual bool Export(const std::string &data) = 0;
virtual ~IExport(){}
};
class ExportXml : public IExport {
public:
virtual bool Export(const std::string &data) {
return true;
}
};
class ExportJson : public IExport {
public:
virtual bool Export(const std::string &data) {
return true;
}
};
class ExportTxt : public IExport {
public:
virtual bool Export(const std::string &data) {
return true;
}
};
class ExportCSV : public IExport {
public:
virtual bool Export(const std::string &data) {
return true;
}
};
class IExportFactory {
public:
IExportFactory() {
_export = nullptr;
}
virtual ~IExportFactory() {
if (_export) {
delete _export;
_export = nullptr;
}
}
bool Export(const std::string &data) {
if (_export == nullptr) {
_export = NewExport();
}
return _export->Export(data);
}
protected:
virtual IExport * NewExport(/* ... */) = 0;
private:
IExport* _export;
};
class ExportXmlFactory : public IExportFactory {
protected:
virtual IExport * NewExport(/* ... */) {
// 可能有其它操作,或者许多参数
IExport * temp = new ExportXml();
// 可能之后有什么操作
return temp;
}
};
class ExportJsonFactory : public IExportFactory {
protected:
virtual IExport * NewExport(/* ... */) {
// 可能有其它操作,或者许多参数
IExport * temp = new ExportJson;
// 可能之后有什么操作
return temp;
}
};
class ExportTxtFactory : public IExportFactory {
protected:
IExport * NewExport(/* ... */) {
// 可能有其它操作,或者许多参数
IExport * temp = new ExportTxt;
// 可能之后有什么操作
return temp;
}
};
class ExportCSVFactory : public IExportFactory {
protected:
virtual IExport * NewExport(/* ... */) {
// 可能有其它操作,或者许多参数
IExport * temp = new ExportCSV;
// 可能之后有什么操作
return temp;
}
};
int main () {
IExportFactory *factory = new ExportTxtFactory();
factory->Export("hello world");
return 0;
}
这个就好看很多了, 而且很好理解
要点
主要是解决创造过程比较复杂, 希望对外隐藏这些细节的场景:
- 连接池, 线程池
- 隐藏对象真实类型(这个是我经常使用的途径)
- 对象创建会有很多参数来决定如何创建 (这人也是)
- 创建对象有复杂的依赖关系 (感觉有复杂依赖可以用创建者)
本质
延迟到子类来选择实现