-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFlyweight.cpp
More file actions
99 lines (81 loc) · 3.22 KB
/
Flyweight.cpp
File metadata and controls
99 lines (81 loc) · 3.22 KB
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
88
89
90
91
92
93
94
95
96
97
98
99
//
// Flyweight.hpp
// COns
//
// Created by zhTian on 16/4/16.
// Copyright © 2016年 zhTian. All rights reserved.
//
#ifndef Flyweight_hpp
#define Flyweight_hpp
#include <iostream>
#include <vector>
#define NUMBER_OF_SAME_TYPE_CHARS 3;
class FlyweightCharacter;
class FlyweightCharacterAbstractBuilder
{
public:
FlyweightCharacterAbstractBuilder(){}
FlyweightCharacterAbstractBuilder(const FlyweightCharacterAbstractBuilder&){}
FlyweightCharacterAbstractBuilder& operator=(const FlyweightCharacterAbstractBuilder&) = delete;
virtual ~FlyweightCharacterAbstractBuilder(){}
public:
static void setFontsAndNames();
static FlyweightCharacter createFlyweightCharacter(unsigned short fontSizeIndex,
unsigned short fontNameIndex,
unsigned short positionInStream);
public:
static std::vector<float> s_fontSizes;
static std::vector<std::string> s_fontNames;
};
std::vector<float> fontSizes(3);
std::vector<std::string> fontNames(3);
void FlyweightCharacterAbstractBuilder::setFontsAndNames() {
fontSizes[0] = 1.0; fontSizes[1] = 1.5; fontSizes[2] = 2.0;
fontNames[0] = "first_font"; fontNames[1] = "second_font"; fontNames[2] = "third_font";
}
class FlyweightCharacter
{
unsigned short fontSizeIndex; // index instead of actual font size
unsigned short fontNameIndex; // index instead of font name
unsigned positionInStream;
public:
FlyweightCharacter(unsigned short fontSizeIndex,
unsigned short fontNameIndex,
unsigned short positionInStream):
fontSizeIndex(fontSizeIndex),
fontNameIndex(fontNameIndex),
positionInStream(positionInStream) {}
void print() {
std::cout << "Font Size: " <<
::fontSizes[fontSizeIndex] <<
", font Name: " <<
::fontNames[fontNameIndex] <<
", character stream position: " <<
positionInStream <<
std::endl;
}
~FlyweightCharacter() {}
};
FlyweightCharacter createFlyweightCharacter(unsigned short fontSizeIndex,
unsigned short fontNameIndex,
unsigned short positionInStream) {
FlyweightCharacter fc(fontSizeIndex, fontNameIndex, positionInStream);
return fc;
}
class FlyweightDemo
{
public:
void excute()
{
std::vector<FlyweightCharacter> chars;
FlyweightCharacterAbstractBuilder::setFontsAndNames();
unsigned short limit = NUMBER_OF_SAME_TYPE_CHARS;
for (unsigned short i = 0; i < limit; i++) {
chars.push_back(FlyweightCharacterAbstractBuilder::createFlyweightCharacter(0, 0, i));
chars.push_back(FlyweightCharacterAbstractBuilder::createFlyweightCharacter(1, 1, i + 1 * limit));
chars.push_back(FlyweightCharacterAbstractBuilder::createFlyweightCharacter(2, 2, i + 2
* limit));
}
}
};
#endif /* Flyweight_hpp */