亨元模式.cpp
#include <iostream>
#include <memory>
#include <string>
#include <unordered_map>
#include <ctime>
#include <memory>
#include <list>
#include <vector>
using namespace std;
namespace ns1
{
enum EnumColor
{
Black,
White
};
struct Position
{
int m_x;
int m_y;
Position(int tmpx, int tmpy) : m_x(tmpx), m_y(tmpy) {}
};
class Piece
{
EnumColor m_color;
Position m_pos;
public:
Piece(EnumColor tmpcolor, Position tmppos) : m_color(tmpcolor), m_pos(tmppos) {}
void draw() const
{
switch (m_color)
{
case Black:
cout << "position: (" << m_pos.m_x << ", " << m_pos.m_y << ") draw a black piece!" << endl;
break;
case White:
cout << "position: (" << m_pos.m_x << ", " << m_pos.m_y << ") draw a white piece!" << endl;
break;
}
}
};
}
namespace ns2
{
enum EnumColor
{
Black,
White
};
struct Position
{
int m_x;
int m_y;
Position(int tmpx, int tmpy) : m_x(tmpx), m_y(tmpy) {}
};
class Piece
{
public:
virtual ~Piece() {}
virtual void draw(Position tmppos) const = 0;
};
class BlackPiece : public Piece
{
public:
void draw(Position tmppos) const override
{
cout << "position: (" << tmppos.m_x << ", " << tmppos.m_y << ") draw a black piece!" << endl;
}
};
class WhitePiece : public Piece
{
public:
void draw(Position tmppos) const override
{
cout << "position: (" << tmppos.m_x << ", " << tmppos.m_y << ") draw a white piece!" << endl;
}
};
class pieceFactory
{
unordered_map<EnumColor, shared_ptr<Piece>> m_FlyWeihgtMap;
public:
shared_ptr<Piece> getFlyWeight(EnumColor tmpcolor)
{
auto iter = m_FlyWeihgtMap.find(tmpcolor);
if (iter == m_FlyWeihgtMap.end())
{
shared_ptr<Piece> tmpfw;
switch (tmpcolor)
{
case Black:
tmpfw.reset(new BlackPiece());
break;
case White:
tmpfw.reset(new WhitePiece());
break;
}
m_FlyWeihgtMap.insert(make_pair(tmpcolor, tmpfw));
return tmpfw;
}
else
{
return iter->second;
}
}
};
}
namespace ns3
{
class Shape
{
public:
virtual ~Shape() = default;
virtual void draw() const = 0;
};
class ColorShape : public Shape
{
string color;
public:
ColorShape(const string &m_color) : color(m_color) {}
void draw() const override
{
cout << "ColorShape: Draw() [Color : " << color << "]" << endl;
}
};
class ShapeFactory
{
public:
static shared_ptr<Shape> getColorShape(const string &color)
{
static unordered_map<string, shared_ptr<Shape>> colorShapeMap;
if (colorShapeMap.count(color) > 0)
return colorShapeMap[color];
cout << "Creating ColorShape of color : " << color << endl;
shared_ptr<Shape> shape = make_shared<ColorShape>(color);
colorShapeMap[color] = shape;
return shape;
}
};
static double random()
{
return (rand() % 10) * 0.1;
}
static string colors[] = {"Red", "Green", "Blue", "White", "Black"};
static string getRandomColor()
{
return colors[(int)(random() * sizeof(colors) / sizeof(colors[0]))];
}
}
int main()
{
#if 0
using namespace ns1;
shared_ptr<Piece> p_piece1(new Piece(Black, Position(3, 3)));
shared_ptr<Piece> p_piece2(new Piece(White, Position(5, 5)));
shared_ptr<Piece> p_piece3(new Piece(Black, Position(4, 6)));
shared_ptr<Piece> p_piece4(new Piece(White, Position(5, 7)));
list<shared_ptr<Piece>> piecelist;
piecelist.push_back(p_piece1);
piecelist.push_back(p_piece2);
piecelist.push_back(p_piece3);
piecelist.push_back(p_piece4);
for (const shared_ptr<Piece> &piece : piecelist)
piece->draw();
#endif
#if 0
using namespace ns2;
shared_ptr<pieceFactory> pfactory(new pieceFactory());
shared_ptr<Piece> p_piece1 = pfactory->getFlyWeight(Black);
p_piece1->draw(Position(3, 3));
shared_ptr<Piece> p_piece2 = pfactory->getFlyWeight(White);
p_piece2->draw(Position(5, 5));
shared_ptr<Piece> p_piece3 = pfactory->getFlyWeight(Black);
p_piece3->draw(Position(4, 6));
shared_ptr<Piece> p_piece4 = pfactory->getFlyWeight(White);
p_piece4->draw(Position(5, 7));
vector<pair<EnumColor, Position>> piecevector;
piecevector.push_back(pair<EnumColor, Position>(Black, Position(3, 3)));
piecevector.push_back(make_pair(White, Position(5, 5)));
piecevector.push_back(make_pair(Black, Position(4, 6)));
piecevector.push_back(make_pair(White, Position(5, 7)));
for (const auto &piece : piecevector)
pfactory->getFlyWeight(piece.first)->draw(piece.second);
#endif
#if 1
using namespace ns3;
srand(time(0));
for (int i = 0; i < 20; ++i)
{
shared_ptr<Shape> colorShape = ShapeFactory::getColorShape(getRandomColor());
colorShape->draw();
}
#endif
cout << "Over!\n";
return 0;
}