(This article aims to review personal knowledge)
We recommend a look at a simple factory pattern , because my case is modified on a simple factory pattern.
1, the details:
Factory Method pattern consists of four roles:
① Abstract Factory role: This is the core factory method pattern, she has nothing to do with the application. It is an interface or a parent class must inherit the role of specific factory must implement. (Factory's in the present embodiment)
② concrete factory class roles: Include relevant and specific business logic code. By an application specific call to create corresponding product. (In this example AddFactory, SubFactory, MulFactory)
③ abstract Product role: specific product class inherits the parent class or have to implement. (MyAlgorithm in this embodiment)
④ role of specific products: concrete factory object instance created role. (In this example MyAdd, MySub, MyMul)
2, Application:
Factory Method pattern applicable, include: a class does not know it needs class objects; a class is specified by a subclass which object is created; to create multi-task entrusted to an object, the client multiple sub-factory class in use is no need to be concerned about which products to create a factory subclass subclasses need to be re-specified dynamically.
3, UML class diagram:
4, code implementation:
MyAlgorithm.h
#pragma once
//算法接口类
class MyAlgorithm
{
public:
MyAlgorithm();
~MyAlgorithm();
public:
virtual double GetResult(double param1, double param2) = 0;
};
MyAlgorithm.cpp
#include "stdafx.h"
#include "MyAlgorithm.h"
MyAlgorithm::MyAlgorithm()
{
}
MyAlgorithm::~MyAlgorithm()
{
}
MyAdd.h
#pragma once
#include "MyAlgorithm.h"
class MyAdd:public MyAlgorithm
{
public:
MyAdd();
~MyAdd();
public:
double GetResult(double param1, double param2);
};
MyAdd.cpp
#include "stdafx.h"
#include "MyAdd.h"
MyAdd::MyAdd()
{
}
MyAdd::~MyAdd()
{
}
double MyAdd::GetResult(double param1, double param2)
{
return param1 + param2;
}
MySub.h
#pragma once
#include "MyAlgorithm.h"
class MySub:public MyAlgorithm
{
public:
MySub();
~MySub();
public:
double GetResult(double param1, double param2);
};
MySub.cpp
#include "stdafx.h"
#include "MySub.h"
MySub::MySub()
{
}
MySub::~MySub()
{
}
double MySub::GetResult(double param1, double param2)
{
return param1 - param2;
}
MyMul.h
#pragma once
#include "MyAlgorithm.h"
class MyMul:public MyAlgorithm
{
public:
MyMul();
~MyMul();
public:
double GetResult(double param1, double param2);
};
MyMul.cpp
#include "stdafx.h"
#include "MyMul.h"
MyMul::MyMul()
{
}
MyMul::~MyMul()
{
}
double MyMul::GetResult(double param1, double param2)
{
return param1 * param2;
}
Factory.h
#pragma once
#include "MyAlgorithm.h"
class Factory
{
public:
Factory();
~Factory();
public:
virtual MyAlgorithm* GetAlgorithm() = 0;
};
Factory.cpp
#include "stdafx.h"
#include "Factory.h"
Factory::Factory()
{
}
Factory::~Factory()
{
}
AddFactory.h
#pragma once
#include "Factory.h"
class AddFactory:public Factory
{
public:
AddFactory();
~AddFactory();
public:
MyAlgorithm* GetAlgorithm();
};
AddFactory.cpp
#include "stdafx.h"
#include "AddFactory.h"
#include "MyAdd.h"
AddFactory::AddFactory()
{
}
AddFactory::~AddFactory()
{
}
MyAlgorithm* AddFactory::GetAlgorithm()
{
return new MyAdd();
}
SubFactory.h
#pragma once
#include "Factory.h"
class SubFactory: public Factory
{
public:
SubFactory();
~SubFactory();
public:
MyAlgorithm* GetAlgorithm();
};
SubFactory.cpp
#include "stdafx.h"
#include "SubFactory.h"
#include "MySub.h"
SubFactory::SubFactory()
{
}
SubFactory::~SubFactory()
{
}
MyAlgorithm* SubFactory::GetAlgorithm()
{
return new MySub();
}
MulFactory.h
#pragma once
#include "Factory.h"
class MulFactory:public Factory
{
public:
MulFactory();
~MulFactory();
public:
MyAlgorithm* GetAlgorithm();
};
MulFactory.cpp
#include "stdafx.h"
#include "MulFactory.h"
#include "MyMul.h"
MulFactory::MulFactory()
{
}
MulFactory::~MulFactory()
{
}
MyAlgorithm* MulFactory::GetAlgorithm()
{
return new MyMul();
}
Calling code:
// FactoryMethodMemo.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "MyAlgorithm.h"
#include "AddFactory.h"
#include "SubFactory.h"
#include "MulFactory.h"
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
MyAlgorithm* pAlgorithmm = NULL;
Factory* pFactory = new AddFactory();
pAlgorithmm = pFactory->GetAlgorithm();
cout << "add:" << pAlgorithmm->GetResult(3.0, 5.0) << endl;
delete pAlgorithmm;
pAlgorithmm = NULL;
delete pFactory;
pFactory = NULL;
pFactory = new SubFactory();
pAlgorithmm = pFactory->GetAlgorithm();
cout << "sub:" << pAlgorithmm->GetResult(3.0, 5.0) << endl;
delete pAlgorithmm;
pAlgorithmm = NULL;
delete pFactory;
pFactory = NULL;
pFactory = new MulFactory();
pAlgorithmm = pFactory->GetAlgorithm();
cout << "mul:" << pAlgorithmm->GetResult(3.0, 5.0) << endl;
delete pAlgorithmm;
pAlgorithmm = NULL;
system("pause");
return 0;
}
Output: