-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcell_draw_strategy.h
executable file
·87 lines (74 loc) · 1.83 KB
/
cell_draw_strategy.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#ifndef CELL_DRAW_STRATEGY_H
#define CELL_DRAW_STRATEGY_H
#include <memory>
#include <QPixmap>
#include <QPainter>
#include "chinese_chess.h"
#include "itemdispencer.h"
namespace ChineseChess
{
// a class to draw a single cell
struct CellDrawStrategy
{
typedef std::shared_ptr<CellDrawStrategy> ptr;
virtual void draw(QPainter &painter, int width, int height) =0;
virtual ~CellDrawStrategy() {}
};
struct EmptyCellDrawStrategy: CellDrawStrategy
{
void draw(QPainter &painter, int width, int height)
{
painter.drawPixmap(0, 0, width, height, *dispencer<QPixmap>().Get("empty"));
}
};
/*
//typedef std::map<std::string, CellDrawStrategy::ptr> strategy_dispencer;
// init code:
///
class Singleton
{
public:
static Singleton& ReturnInstance()
{
static Singleton instance_;
return instance_;
}
CellDrawStrategy::ptr get_strategy(std::string key)
{
return dispencer_[key];
}
void put_strategy(std::string key, CellDrawStrategy::ptr strategy)
{
dispencer_[key] = strategy;
}
private:
std::map<std::string, CellDrawStrategy::ptr> dispencer_;
};
//init code
Singleton &sing = Singleton::ReturnInstance();
sing.put_strategy("busy", BusyCellDrawStrategy)
// use code
// singleton user:
#include "Singletone.h"
Singletone sing;
sing.GetIn..(); // wrong!
//
#include "Singletone.h"
Singletone::ReturnInstance().getInt();
*/
struct FreeCellDrawStrategy: CellDrawStrategy
{
void draw(QPainter &painter, int width, int height)
{
painter.drawPixmap(0, 0, width, height, *dispencer<QPixmap>().Get("free"));
}
};
struct BusyCellDrawStrategy: CellDrawStrategy
{
void draw(QPainter &painter, int width, int height)
{
painter.drawPixmap(0, 0, width, height, *dispencer<QPixmap>().Get("busy"));
}
};
}
#endif // CELL_DRAW_STRATEGY_H