- 浏览: 356998 次
- 性别:
- 来自: 苏州
文章分类
- 全部博客 (335)
- C++ (190)
- 设计模式 (43)
- 数据库技术 (5)
- 网络编程 (11)
- 自动化测试 (6)
- Linux (13)
- OpenSSL (10)
- MS Crypt API (5)
- SCM (2)
- English (4)
- Android (10)
- EMV规范 (1)
- Saturn Platform (0)
- C (10)
- SQL (2)
- ASP.NET (3)
- 英语口语学习 (3)
- 调试工具 (21)
- 编译技术 (5)
- UML (1)
- 项目管理 (5)
- 敏捷开发 (2)
- Http Server (6)
- 代码审查、代码分析 (5)
- 面试基础 (10)
- 重点知识 (16)
- STL (6)
- Efficient C++资料 (8)
- 数据结构和算法 (7)
- 读书笔记 (0)
- 开源项目 (4)
- 多线程 (2)
- Console App (6)
- 个人开源项目 (4)
- IBM DevelopWorks (4)
- Java (16)
- 内存泄漏相关调试和检测 (13)
- 软件测试相关技术 (2)
- C# (11)
- Apple Related (1)
- 软件测试和管理 (2)
- EMV (1)
- Python (1)
- Node.js (6)
- JavaScript (5)
- VUE (1)
- Frontend (1)
- Backend (4)
- RESTful API (3)
- Firebase (3)
最新评论
-
u013189503:
来个密码吧
[C++][Logging] 项目中写日志模块的实现 -
wyf_vc:
来个密码啊!!
[C++][Logging] 项目中写日志模块的实现
头文件
//FlyweightPattern.h #ifndef FLYWEIGHT_PATTERN_H #define FLYWEIGHT_PATTERN_H #include <Windows.h> #include <iostream> #include <map> #include <vector> using namespace std; namespace FlyweightPattern { typedef struct pointTag { int x; int y; pointTag(){} pointTag(int a, int b) { x = a; y = b; } bool operator <(const pointTag& other) const { if (x < other.x) { return true; } else if (x == other.x) { return y < other.y; } return false; } }POINT; typedef enum PieceColorTag { BLACK, WHITE }PIECECOLOR; class CPiece { public: CPiece(PIECECOLOR color); PIECECOLOR GetColor(); // Set the external state void SetPoint(POINT point); POINT GetPoint(); protected: // Internal state PIECECOLOR m_color; // external state POINT m_point; }; class CGomoku : public CPiece { public: CGomoku(PIECECOLOR color); }; class CPieceFactory { public: CPiece *GetPiece(PIECECOLOR color); ~CPieceFactory(); private: vector<CPiece *> m_vecPiece; }; class CChessboard { public: void Draw(CPiece *piece); void ShowAllPieces(); private: map<POINT, CPiece *> m_mapPieces; }; ////////////////////////////////////////////////////////////////////////// void FlyweightPattern_Test(); } #endif
实现
#include "FlyweightPattern.h" namespace FlyweightPattern { CPiece::CPiece(PIECECOLOR color) : m_color(color) {} PIECECOLOR CPiece::GetColor() { return m_color; } // Set the external state void CPiece::SetPoint(POINT point) { m_point = point; } POINT CPiece::GetPoint() { return m_point; } ////////////////////////////////////////////////////////////////////////// CGomoku::CGomoku(PIECECOLOR color) : CPiece(color) {} ////////////////////////////////////////////////////////////////////////// CPiece* CPieceFactory::GetPiece(PIECECOLOR color) { CPiece *pPiece = NULL; if (m_vecPiece.empty()) { pPiece = new CGomoku(color); m_vecPiece.push_back(pPiece); } else { for (vector<CPiece *>::iterator it = m_vecPiece.begin(); it != m_vecPiece.end(); ++it) { if ((*it)->GetColor() == color) { pPiece = *it; break; } } if (pPiece == NULL) { pPiece = new CGomoku(color); m_vecPiece.push_back(pPiece); } } return pPiece; } CPieceFactory::~CPieceFactory() { for (vector<CPiece *>::iterator it = m_vecPiece.begin(); it != m_vecPiece.end(); ++it) { if (*it != NULL) { delete *it; *it = NULL; } } } ////////////////////////////////////////////////////////////////////////// void CChessboard::Draw(CPiece *piece) { if (WHITE == piece->GetColor()) { cout<<"Draw a White"<<" at ("<<piece->GetPoint().x<<","<<piece->GetPoint().y<<")"<<endl; } else { cout<<"Draw a Black"<<" at ("<<piece->GetPoint().x<<","<<piece->GetPoint().y<<")"<<endl; } m_mapPieces.insert(pair<POINT, CPiece *>(piece->GetPoint(), piece)); } void CChessboard::ShowAllPieces() { for (map<POINT, CPiece *>::iterator it = m_mapPieces.begin(); it != m_mapPieces.end(); ++it) { if (WHITE == it->second->GetColor()) { cout<<"("<<it->first.x<<","<<it->first.y<<") has a White cheese."<<endl; } else { cout<<"("<<it->first.x<<","<<it->first.y<<") has a Black cheese."<<endl; } } } ////////////////////////////////////////////////////////////////////////// void FlyweightPattern_Test() { CPieceFactory *pPieceFactory = new CPieceFactory(); CChessboard *pCheseboard = new CChessboard(); // The player1 get a white piece from the pieces bowl CPiece *pPiece = pPieceFactory->GetPiece(WHITE); pPiece->SetPoint(POINT(2, 3)); pCheseboard->Draw(pPiece); // The player2 get a black piece from the pieces bowl pPiece = pPieceFactory->GetPiece(BLACK); pPiece->SetPoint(POINT(4, 5)); pCheseboard->Draw(pPiece); // The player1 get a white piece from the pieces bowl pPiece = pPieceFactory->GetPiece(WHITE); pPiece->SetPoint(POINT(2, 4)); pCheseboard->Draw(pPiece); // The player2 get a black piece from the pieces bowl pPiece = pPieceFactory->GetPiece(BLACK); pPiece->SetPoint(POINT(3, 5)); pCheseboard->Draw(pPiece); /*......*/ //Show all cheses cout<<"Show all cheses"<<endl; pCheseboard->ShowAllPieces(); if (pCheseboard != NULL) { delete pCheseboard; pCheseboard = NULL; } if (pPieceFactory != NULL) { delete pPieceFactory; pPieceFactory = NULL; } } }
客户端
#include "FlyweightPattern.h" #include <iostream> using namespace std; using namespace FlyweightPattern; void main() { FlyweightPattern_Test(); }
运行结果
- FlyweightPattern.zip (1.7 KB)
- 下载次数: 0
- Diagram.zip (558 KB)
- 下载次数: 0
- Diagram-raw.zip (216.7 KB)
- 下载次数: 0
发表评论
-
FreeRTOS
2022-03-05 16:31 213Ref https://blog.csdn.net/weix ... -
串口通讯相关
2018-11-02 13:44 371https://bbs.csdn.net/wap/topics ... -
[转]C++验证IP是否可以PING通
2018-10-30 17:54 1246https://www.cnblogs.com/guoyz13 ... -
C++/MFC 換皮膚
2018-10-20 11:05 433https://blog.csdn.net/u01123991 ... -
WinCE 截屏 - C++ 代碼
2018-08-31 09:45 536// this function create a bmp ... -
Android NDK搭建環境
2017-11-27 13:25 536https://www.cnblogs.com/ut2016- ... -
8583协议相关
2017-10-17 13:38 5288583相关资料,整理中... -
Java高级应用之JNI
2017-06-19 09:00 559参考link http://www.cnblogs.com/l ... -
C++实现ping功能
2017-04-18 11:21 2051基础知识 ping的过程是向目的IP发送一个type=8的I ... -
OpenSSL 编译环境搭建
2017-03-27 15:01 8661 安裝VS2008到 c:\Program Files (x ... -
最优非对称加密填充(OAEP)
2017-03-25 14:53 1528OpenSSL命令---rsautl http://blog. ... -
[Platform Builder] 设置SVM OS build Env
2016-11-10 11:39 01 copy one OSDesign Project to ... -
[Windows] System Error Codes(GetLastError )0-----5999
2016-10-26 13:28 1839ERROR_SUCCESS 0 (0x0) T ... -
开源Windows驱动程序框架
2016-09-17 21:35 825转自 http://code.csdn.net/news/28 ... -
c/c++代码中执行cmd命令
2016-09-14 14:50 1824转自 http://blog.csdn.net/slixinx ... -
C#使用C++标准DLL实例(包含callback)
2016-09-11 19:44 1010C++编写标准Win32DLL如下 头文件 /***** ... -
C#调用C++的DLL搜集整理的所有数据类型转换方式
2016-09-09 16:07 934转自 http://www.cnblogs.com/zeroo ... -
WinCE CPU使用率计算 测试工具
2016-09-08 16:14 942转自 http://blog.csdn.net/jan ... -
switch在C++与C#中的一些差异
2016-09-08 15:19 767参考链接 http://blog.csdn.net/weiwe ... -
C++ 鼠标模拟程序
2016-09-04 12:09 1547转自 http://blog.csdn.net/weixinh ...
相关推荐
组合模式(Composite Pattern)-结构型模式 (0%) <br>讲义书写模式 在实际的讲解中我会用一套模式来讲述我对设计模式的理解 1.给出当前章节模式的名称 2.讲述一个小故事,提出问题 3.回答这个...
结构型模式:外观、代理、适配器、桥接等模式,有案例和图解说明,有助于理解
设计模式:详细描述创建型、结构型、行为型,简单容易理解
NULL 博文链接:https://jacky-dai.iteye.com/blog/2298680
NULL 博文链接:https://jacky-dai.iteye.com/blog/2297527
设计模式10-代理模式、结构型模式大复习 设计模式11-行为模式-责任链、命令模式 设计模式12-解释器模式 设计模式13-迭代器模式 设计模式14-中介者模式、备忘录模式 设计模式15-观察者模式、状态模式 设计模式16-策略...
使用设计模式是为了可重用代码、让代码更容易被他人理解、保证代码可靠性。 毫无疑问,设计模式于己于他人于系统都是多赢的,设计模式使代码编制真正工程化,设计模式是软 件工程的基石,如同大厦的一块块砖石一样。...
NULL 博文链接:https://jacky-dai.iteye.com/blog/2299576
NULL 博文链接:https://jacky-dai.iteye.com/blog/2297785
NULL 博文链接:https://jacky-dai.iteye.com/blog/2302930
NULL 博文链接:https://jacky-dai.iteye.com/blog/2298491