init commit
This commit is contained in:
52
archive/design pattern/factory/simple.cpp
Normal file
52
archive/design pattern/factory/simple.cpp
Normal file
@ -0,0 +1,52 @@
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
class Graph
|
||||
{
|
||||
public:
|
||||
virtual void show() = 0;
|
||||
};
|
||||
|
||||
class Triangle : public Graph
|
||||
{
|
||||
public:
|
||||
void show()
|
||||
{
|
||||
std::cout << "三角形" << std::endl;
|
||||
}
|
||||
};
|
||||
|
||||
class Round : public Graph
|
||||
{
|
||||
public:
|
||||
void show()
|
||||
{
|
||||
std::cout << "圆形" << std::endl;
|
||||
}
|
||||
};
|
||||
|
||||
class GraphFactory
|
||||
{
|
||||
public:
|
||||
static Graph *CreateGraph(std::string name)
|
||||
{
|
||||
if (name == "triangle")
|
||||
{
|
||||
return new Triangle();
|
||||
}
|
||||
else if (name == "round")
|
||||
{
|
||||
return new Round();
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
Graph *a = GraphFactory::CreateGraph("triangle");
|
||||
a->show();
|
||||
a = GraphFactory::CreateGraph("round");
|
||||
a->show();
|
||||
return 0;
|
||||
}
|
Reference in New Issue
Block a user