Created
August 7, 2013 11:19
-
-
Save ErikZhou/6173203 to your computer and use it in GitHub Desktop.
Structural Patterns - Flyweight
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Flyweight.h | |
| #include <string> | |
| #include <map> | |
| #include <vector> | |
| #include <iostream> | |
| using namespace std; | |
| class AbstractFont // Flyweight抽象类 | |
| { | |
| protected: | |
| string font_name; // IntrinsicState,即内部状态 | |
| public: | |
| virtual void set_font_name(string font_name) = 0; // operation | |
| virtual string get_font_name() = 0; | |
| virtual string display() = 0; | |
| public: | |
| virtual ~AbstractFont() | |
| { | |
| cout << "in the destructor of AbstractFont..." << endl; | |
| } | |
| }; | |
| class SongTiFont : public AbstractFont // ConcreteFlyweight | |
| { | |
| public: | |
| SongTiFont(string font_name) | |
| { | |
| this->font_name = font_name; | |
| } | |
| string display() | |
| { | |
| return font_name; | |
| } | |
| string get_font_name() | |
| { | |
| return font_name; | |
| } | |
| void set_font_name(string font_name) | |
| { | |
| this->font_name = font_name; | |
| } | |
| public: | |
| ~SongTiFont() | |
| { | |
| cout << "in the destructor of SongTiFont..." << endl; | |
| } | |
| }; | |
| class FontFactory // FlyweightFactory | |
| { | |
| private: | |
| map<string, AbstractFont*> fonts; // 这就是pool of flyweights | |
| public: | |
| AbstractFont* get_font(string font_name) // GetFlyweight,font_name就是key | |
| { | |
| AbstractFont *font; | |
| // 根据key font_name查找对应的Flyweight | |
| map<string, AbstractFont*>::iterator it = fonts.find(font_name); | |
| if(it == fonts.end()) // 如果要找的Flyweight不存在 | |
| { | |
| font = new SongTiFont(font_name); // 创建一个新的Flyweight | |
| fonts.insert(pair<string, AbstractFont*>(font_name, font)); // 加入到pool of flyweights | |
| } | |
| else // 如果要找的Flyweight已经存在 | |
| { | |
| font = it->second; | |
| } | |
| return font; // 返回Flyweight | |
| } | |
| size_t get_number() | |
| { | |
| return fonts.size(); | |
| } | |
| public: | |
| ~FontFactory() | |
| { | |
| cout << "in the destructor of FontFactory..." << endl; | |
| for(map<string, AbstractFont*>::iterator iter = fonts.begin(); iter != fonts.end(); ) | |
| { | |
| delete iter->second; // 在STL中,如果容器中装载的是指针而非值对象,则内存回收 | |
| iter = fonts.erase(iter); // 的工作,要程序员自己完成。 | |
| //iter--; | |
| } | |
| } | |
| }; | |
| class FontedCharacter // 这是一个应用类 | |
| { | |
| private: | |
| string character; | |
| AbstractFont *font; | |
| public: | |
| FontedCharacter() // 之所以要有缺省构造函数,因为Flyweight.cpp中,FontedCharacter chars[10]; | |
| { | |
| } | |
| FontedCharacter(string character, AbstractFont *font) | |
| { | |
| this->character = character; | |
| this->font = font; | |
| } | |
| string set_font() | |
| { | |
| return character + " - " + font->display(); | |
| } | |
| public: | |
| ~FontedCharacter() | |
| { | |
| cout << "in the destructor of FontedCharacter..." << endl; | |
| } | |
| }; | |
| // Flyweight.cpp | |
| //#include "Flyweight.h" | |
| int main(int argc, char **argv) | |
| { | |
| FontFactory *ff = new FontFactory(); | |
| FontedCharacter chars[10]; | |
| chars[0] = FontedCharacter("A", ff->get_font("宋体红色")); // 1 | |
| chars[1] = FontedCharacter("B", ff->get_font("宋体红色")); // 2 | |
| chars[2] = FontedCharacter("C", ff->get_font("宋体红色")); // 3 | |
| chars[3] = FontedCharacter("D", ff->get_font("宋体红色")); // 4 | |
| chars[4] = FontedCharacter("E", ff->get_font("宋体红色")); // 5 | |
| chars[5] = FontedCharacter("F", ff->get_font("宋体黑色")); // 6 | |
| chars[6] = FontedCharacter("G", ff->get_font("宋体黑色")); // 7 | |
| chars[7] = FontedCharacter("H", ff->get_font("宋体黑色")); // 8 | |
| chars[8] = FontedCharacter("I", ff->get_font("宋体黑色")); // 9 | |
| chars[9] = FontedCharacter("J", ff->get_font("宋体黑色")); // 10 | |
| cout << endl; | |
| for(int i = 0; i < 10; i++) | |
| { | |
| cout << chars[i].set_font() << endl; | |
| } | |
| cout << "\n实际有" << ff->get_number() << " 种字体" <<endl; | |
| delete ff; | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment