C++设计模式——享元模式(高屋建瓴)
原网址:https://blog.csdn.net/CoderAldrich/article/details/83183271
做出如下修改:
#include <iostream>
#include <map>
#include <vector>
using namespace std;
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) : color_(color){}PIECECOLOR GetColor() {return color_;}void SetPoint(Point point) {point_ = point;}Point GetPoint() {return point_;}protected:PIECECOLOR color_;Point point_;
};class CGomoku : public CPiece {public:CGomoku(PIECECOLOR color) : CPiece(color){}
};// 用vec存贮多个CPiece
class CPieceFactory {public:// 判断颜色(白色或者黑色)是否已经创建,来避免重复创建CPieceCPiece *GetPiece(PIECECOLOR color) {CPiece *piece = NULL;if (piece_vec_.empty()) {piece = new CGomoku(color);piece_vec_.push_back(piece);} else {for (vector<CPiece *>::iterator it = piece_vec_.begin(); \it != piece_vec_.end(); ++it) { if ((*it)->GetColor() == color) {piece = *it;//<------------------------->避免重复创建break;}}if (piece == NULL) {piece = new CGomoku(color);piece_vec_.push_back(piece);}}return piece;}~CPieceFactory() {for (vector<CPiece *>::iterator it = piece_vec_.begin(); \it != piece_vec_.end(); ++it) {if (*it != NULL) {delete *it;}}}private:vector<CPiece *> piece_vec_;
};/*
将 CPieceFactory里面判断好的piece(避免重复制造)以及对应的坐标点,
存到一个map里面,以便于全部展现
*/
class ChessBoard {public:void Draw(CPiece *piece) {if (piece->GetColor() == WHITE) {cout << "Draw a White" << " at (" << piece->GetPoint().x_ << ","\<< piece->GetPoint().y_ << ")" << endl;} else if (piece->GetColor() == BLACK) { cout << "Draw a Black" << " at (" << piece->GetPoint().x_ << ","\<< piece->GetPoint().y_ << ")" << endl;}point_piece_map_.insert(pair<Point, CPiece *>(piece->GetPoint(), piece));}void ShowAllPieces() {for (map<Point, CPiece *>::iterator it = point_piece_map_.begin();\it != point_piece_map_.end(); ++it) {if (it->second->GetColor() == WHITE) {cout << "(" << it->first.x_ << "," << it->first.y_ << \")has a white chece ." << endl;} else if (it->second->GetColor() == BLACK) {cout << "(" << it->first.x_ << "," << it->first.y_ << \")has a black chece ." << endl;}}}private:map<Point, CPiece *> point_piece_map_;
};int main() {CPieceFactory *piece_factory = new CPieceFactory();ChessBoard *chess_board = new ChessBoard();CPiece *piece = piece_factory->GetPiece(WHITE);// 判断是否已经创建了白色棋子piece->SetPoint(Point(2, 3));//结构体不需要new对象chess_board->Draw(piece);piece = piece_factory->GetPiece(BLACK);piece->SetPoint(Point(4, 5));chess_board->Draw(piece);piece = piece_factory->GetPiece(WHITE);piece->SetPoint(Point(2, 4));chess_board->Draw(piece);piece = piece_factory->GetPiece(BLACK);piece->SetPoint(Point(3, 5));chess_board->Draw(piece);// show all chesscout<<"Show all cheses"<<endl;chess_board->ShowAllPieces();if (piece_factory != NULL) {delete piece_factory;piece_factory = NULL;}if (chess_board != NULL) {delete chess_board;chess_board = NULL;}
}
运行结果:
Draw a White at (2,3)
Draw a Black at (4,5)
Draw a White at (2,4)
Draw a Black at (3,5)
Show all cheses
(2,3)has a white chece .
(2,4)has a white chece .
(3,5)has a black chece .
(4,5)has a black chece .
C++设计模式——享元模式(高屋建瓴)相关推荐
- Python设计模式-享元模式
Python设计模式-享元模式 基于Python3.5.2,代码如下 #coding:utf-8class Coffee:name = ""price = 0def __init_ ...
- 10-Python与设计模式--享元模式
10-Python与设计模式--享元模式 一.网上咖啡选购平台 假设有一个网上咖啡选购平台,客户可以在该平台上下订单订购咖啡,平台会根据用户位置进行线下配送.假设其咖啡对象构造如下: class Co ...
- 设计模式--享元模式实现C++
/********************************* *设计模式--享元模式实现 *C++语言 *Author:WangYong *Blog:http://www.cnblogs.co ...
- 【设计模式】Java设计模式 - 享元模式
[设计模式]Java设计模式 - 享元模式
- Unity设计模式——享元模式(附代码)
Unity设计模式--享元模式(附源码) 享元Flyweight模式是什么 享元模式是一种结构型设计模式, 它摒弃了在每个对象中保存所有数据的方式, 通过共享多个对象所共有的相同状态, 让你能在有限的 ...
- 第二十二章 Caché 设计模式 享元模式
文章目录 第二十二章 Caché 设计模式 享元模式 定义 优点 使用场景 结构图 描述 完整示例 实体类 抽象享元角色 实现享元角色 享元工厂 调用 思考 第二十二章 Caché 设计模式 享元模式 ...
- JavaScript设计模式-享元模式
JavaScript设计模式-享元模式 概念 例子 内部状态与外部状态 享元模式的通用结构 例子 总结 github仓库地址:点击 [设计模式例子](https://github.com/fanhua ...
- JAVA 设计模式 享元模式
用途 享元模式 (Flyweight) 运用共享技术有效地支持大量细粒度的对象. 享元模式是一种结构型模式. 结构 图-享元模式结构图 Flyweight : 它是所有具体享元类的超类或接口,通过这个 ...
- java设计模式---享元模式
Java深入到一定程度,就不可避免的碰到设计模式这一概念,了解设计模式,将使自己 对java中的接口或抽象类应用有更深的理解.设计模式在java的中型系统中应用广 泛,遵循一定的编程模式,才能使自己的 ...
最新文章
- python dump函数_python中实现php的var_dump函数功能
- 常见排序算法效率比较
- MIT一招霸气颠覆传统:勇敢换导师,成本我买单,学生沸腾了
- fedora8 使用小记之:终端字体设置
- Facebook有1万名员工在研发AR/VR设备 占员工总数近1/5
- SQL Server死锁问题:事务(进程 ID x)与另一个进程被死锁在 锁 | 通信缓冲区资源上并且已被选作死锁牺牲品。请重新运行该事务。...
- php扩展返回字符数组,PHP扩展之数组字符串处理
- seo外链重要性_为什么网站速度对于SEO至关重要?以及如何加快网站速度
- prometheus命令_Prometheus+Grafana 基础及简单搭建
- django模型_Django模型
- iOS 工程中引入另一个工程,多工程管理
- 一个简单的MDX案例及说明 (转载)
- matlab自动交易系统设计4 随笔
- 微服务架构:统一身份认证和授权技术解决方案
- Windows 8 开启 NetFX3
- 仓库体积过大,如何减小?
- Vblog#2 DAY1
- 如何举报YouTube视频和评论
- ChatGPT在线网页版和接口
- 联想E420麦克风没有声音的方案