Factory Method

Intent

Define an interface for creating an object, but let subclasses decide which class to
instantiate. Factory Method lets a class defer instantiation to subclasses.

cpp 复制代码
#include <iostream>
#include <share.h>
#include <list>
#include <vector>
#include <memory>

class Document {
public:
	virtual void Open() {
		std::cout << "Document Open" << std::endl;
	}
	virtual void Close() {
	}
	virtual void Save() {
	}
	virtual void Revert() {
	}
};
class MyDocument : public Document {
public:
	virtual void Open() {
		std::cout << "MyDocument Open" << std::endl;
	}
	virtual void Close() {
	}
	virtual void Save() {
	}
	virtual void Revert() {
	}
};

class Application {
public:
	virtual std::shared_ptr<Document> CreateDocument() {
		return nullptr;
	}
	void NewDocument() {
		std::shared_ptr<Document> doc = CreateDocument();
		docs.push_back(doc);
		OpenDocument(doc);
	}
	void OpenDocument(std::shared_ptr<Document> doc) {
		doc->Open();
	}
private:
	std::vector<std::shared_ptr<Document> > docs{};
};

class MyApplication : public Application {
public:
	virtual std::shared_ptr<Document> CreateDocument() {
		std::cout << "MyApplication Construct" << std::endl;
		return std::make_shared<MyDocument>();
	}
};


int main()
{
	std::shared_ptr<Application> app = std::make_shared<MyApplication>();
	app->NewDocument();
}

Using templates to avoid subclassing

cpp 复制代码
// DesignPattern.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>
#include <share.h>
#include <list>
#include <vector>
#include <memory>

class Product {
public:
	Product() {
	}
	virtual void callProductFunc() {
		std::cout << __FUNCTION__ << std::endl;
	}
};

class MyProduct1 : public Product {
public:
	MyProduct1() {
	}
	virtual void callProductFunc() {
		std::cout << __FUNCTION__ << std::endl;
	}
};

class MyProduct2 : public Product {
public:
	MyProduct2() {
	}
	virtual void callProductFunc() {
		std::cout << __FUNCTION__ << std::endl;
	}
};

class Creator {
public: 
	virtual std::shared_ptr<Product> CreateProduct() = 0;
};

// Using templates to avoid subclassing
template<class TheProduct>
class StandardCreator : public Creator {
public:
	virtual std::shared_ptr<Product> CreateProduct() {
		return std::make_shared<TheProduct>();
	}
};

class Application {
public:
	virtual void NewProduct() {
		
	}
protected:
	StandardCreator<MyProduct1> creator_1_;
	StandardCreator<MyProduct2> creator_2_;
};

class MyApplication : public Application {
public:
	virtual void NewProduct() {
		creator_1_.CreateProduct()->callProductFunc();
		creator_2_.CreateProduct()->callProductFunc();
	}
};

int main()
{
	std::shared_ptr<Application> app = std::make_shared<MyApplication>();
	app->NewProduct();
}
相关推荐
学步_技术11 小时前
Python编码系列—Python工厂方法模式:构建灵活对象的秘诀
开发语言·python·工厂方法模式
kimloner11 小时前
工厂模式(二):工厂方法模式
java·设计模式·工厂方法模式
java_heartLake1 天前
设计模式之工厂方法模式
java·设计模式·工厂方法模式
coffee_baby8 天前
Java实现常见的工厂模式(包含在Springboot中实战开发)
java·后端·设计模式·简单工厂模式·工厂方法模式·抽象工厂模式·工厂模式
SAO&asuna20 天前
设计模式-简单工厂模式&工厂方法模式
设计模式·简单工厂模式·工厂方法模式
luciferau21 天前
设计模式-工厂方法模式
设计模式·工厂方法模式
我是回頭呀24 天前
设计模式之工厂方法模式
java·设计模式·工厂方法模式
WineMonk1 个月前
设计模式 1 工厂方法模式
设计模式·工厂方法模式
心之语歌1 个月前
设计模式 抽象工厂方法模式
设计模式·工厂方法模式
不是仙人的闲人1 个月前
C++ 设计模式——工厂方法模式
c++·设计模式·工厂方法模式