Design Patterns: factory method pattern

Core: the generated instance to the subclass, the parent class only defines the interface to generate an instance of

Understanding: Thinking contrast template method pattern is very similar to the Template Method Template Method pattern is understood as an abstract method to create an object, is no longer a process framework, it becomes a factory method pattern, but the specific approach is to create objects

Advantages: hide implementation details of the specific class

example:

class Apple
{
public:
	void show()
	{
		cout << "Success" << endl; 
	}
};

template<typename T>
class Factory
{
public:
	virtual T* create() = 0;
};

class AppleFactory: public Factory<Apple>
{
public:
	Apple* create()
	{
		return new Apple();
	}
};
int main() 
{
	Factory<Apple>* p = new AppleFactory();
	p->create()->show();
	return 0;
}

  

Guess you like

Origin www.cnblogs.com/chusiyong/p/11433043.html