-
Notifications
You must be signed in to change notification settings - Fork 0
/
piecefactory.cpp
52 lines (45 loc) · 1.19 KB
/
piecefactory.cpp
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
/**
AUTHOR: Matayay Karuna
Project: Checkers
File: piecefactory.cpp
Description: Flyweight factory for creating pieces.
**/
#include "piecefactory.h"
#include "SilverPiece.h"
#include "GoldPiece.h"
#include "SilverKing.h"
#include "GoldKing.h"
#include "Empty.h"
#include "Error.h"
std::unordered_map<PieceType, std::shared_ptr<Piece>> PieceFactory::pieceCache_;
std::shared_ptr<Piece> PieceFactory::createPiece(PieceType type) {
auto it = pieceCache_.find(type);
if (it != pieceCache_.end()) {
return it->second;
}
std::shared_ptr<Piece> piece;
switch (type) {
case PieceType::Silver:
piece = std::make_shared<SilverPiece>();
break;
case PieceType::Gold:
piece = std::make_shared<GoldPiece>();
break;
case PieceType::SilverKing:
piece = std::make_shared<SilverKing>();
break;
case PieceType::GoldKing:
piece = std::make_shared<GoldKing>();
break;
case PieceType::Empty:
piece = std::make_shared<Empty>();
break;
case PieceType::Error:
piece = std::make_shared<Error>();
break;
}
if (piece) {
pieceCache_[type] = piece;
}
return piece;
}