init commit
This commit is contained in:
62
archive/design pattern/factory/abstract.cpp
Normal file
62
archive/design pattern/factory/abstract.cpp
Normal file
@@ -0,0 +1,62 @@
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
using namespace std;
|
||||
|
||||
class Init
|
||||
{
|
||||
public:
|
||||
virtual void init() = 0;
|
||||
};
|
||||
|
||||
class ArrowKeys
|
||||
{
|
||||
public:
|
||||
virtual void arrowKeys() = 0;
|
||||
};
|
||||
|
||||
class SystemFactory
|
||||
{
|
||||
public:
|
||||
virtual Init *CreateInit() = 0;
|
||||
virtual ArrowKeys *CreateArrowKeys() = 0;
|
||||
};
|
||||
|
||||
class AndroidInit : public Init
|
||||
{
|
||||
public:
|
||||
void init()
|
||||
{
|
||||
cout << "安卓初始化" << endl;
|
||||
}
|
||||
};
|
||||
|
||||
class AndroidArrowKeys : public ArrowKeys
|
||||
{
|
||||
public:
|
||||
void arrowKeys()
|
||||
{
|
||||
cout << "安卓方向键" << endl;
|
||||
}
|
||||
};
|
||||
|
||||
class AndroidFactory : public SystemFactory
|
||||
{
|
||||
public:
|
||||
Init *CreateInit()
|
||||
{
|
||||
return new AndroidInit();
|
||||
}
|
||||
ArrowKeys *CreateArrowKeys()
|
||||
{
|
||||
return new AndroidArrowKeys();
|
||||
}
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
SystemFactory *s = new AndroidFactory();
|
||||
s->CreateInit()->init();
|
||||
s->CreateArrowKeys()->arrowKeys();
|
||||
|
||||
return 0;
|
||||
}
|
91
archive/design pattern/factory/factory.cpp
Normal file
91
archive/design pattern/factory/factory.cpp
Normal file
@@ -0,0 +1,91 @@
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
using namespace std;
|
||||
|
||||
class Image
|
||||
{
|
||||
public:
|
||||
virtual void read() = 0;
|
||||
};
|
||||
|
||||
class ImageFactory
|
||||
{
|
||||
public:
|
||||
virtual Image *create() = 0;
|
||||
virtual void read() = 0;
|
||||
};
|
||||
|
||||
class JpgImage : public Image
|
||||
{
|
||||
public:
|
||||
string filename;
|
||||
|
||||
public:
|
||||
JpgImage(string filename)
|
||||
{
|
||||
this->filename = filename;
|
||||
}
|
||||
|
||||
void read()
|
||||
{
|
||||
cout << "读取jpg文件:" << this->filename << endl;
|
||||
}
|
||||
};
|
||||
|
||||
class PngImage : public Image
|
||||
{
|
||||
public:
|
||||
void read()
|
||||
{
|
||||
cout << "读取png图片" << endl;
|
||||
}
|
||||
};
|
||||
|
||||
class JpgImageFactory : public ImageFactory
|
||||
{
|
||||
private:
|
||||
Image *img;
|
||||
|
||||
public:
|
||||
Image *create()
|
||||
{
|
||||
this->img = new JpgImage("file-name");
|
||||
return this->img;
|
||||
}
|
||||
|
||||
void read()
|
||||
{
|
||||
img->read();
|
||||
}
|
||||
};
|
||||
|
||||
class PngImageFactory : public ImageFactory
|
||||
{
|
||||
private:
|
||||
Image *img;
|
||||
|
||||
public:
|
||||
Image *create()
|
||||
{
|
||||
this->img = new PngImage();
|
||||
return this->img;
|
||||
}
|
||||
void read()
|
||||
{
|
||||
img->read();
|
||||
}
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
ImageFactory *factory = new JpgImageFactory();
|
||||
Image *img = factory->create();
|
||||
img->read();
|
||||
factory->read();
|
||||
|
||||
factory = new PngImageFactory();
|
||||
img = factory->create();
|
||||
img->read();
|
||||
factory->read();
|
||||
return 0;
|
||||
}
|
BIN
archive/design pattern/factory/img/1.png
Normal file
BIN
archive/design pattern/factory/img/1.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 19 KiB |
8
archive/design pattern/factory/readme.md
Normal file
8
archive/design pattern/factory/readme.md
Normal file
@@ -0,0 +1,8 @@
|
||||
## 简单工厂模式
|
||||
> 一个工厂生产多个产品
|
||||
|
||||
## 工厂方法模式
|
||||
> 多个工厂生产多个产品
|
||||
|
||||
|
||||

|
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