举个围棋的例子,围棋的棋盘共有361格,即可放361个棋子。现在要实现一个围棋程 序,该怎么办呢?首先要考虑的是棋子棋盘的实现,可以定义一个棋子的类,成员变量包括棋子的颜色、形状、位置等信息,另外再定义一个棋盘的类,成员变量中 有个容器,用于存放棋子的对象。下面给出代码表示:

棋子的定义,当然棋子的属性除了颜色和位置,还有其他的,这里略去。这两个属性足以说明问题。

#include <iostream>
#include <vector>
#include <string> enum PieceColor {BLACK,WHITE};
using namespace std; class Pos
{
public:
Pos(int x,int y):m_x(x),m_y(y)
{ }
int getX()
{
return m_x;
}
int getY()
{
return m_y;
}
private:
int m_x;
int m_y;
}; class Piece
{
public:
Piece(PieceColor color,Pos pos):m_color(color),m_pos(pos){};
~Piece() {}
virtual void Draw() {}
protected:
PieceColor m_color;
Pos m_pos; }; class WhitePiece : public Piece
{
public:
WhitePiece(PieceColor color, Pos pos):Piece(color,pos){}
~WhitePiece();
virtual void Draw() {
cout << "draw a white piece" << endl;
}
}; class BlackPiece:public Piece
{
public:
BlackPiece(PieceColor color,Pos pos):Piece(color,pos){}
~BlackPiece();
virtual void Draw(){ cout << "draw a black piece" <<endl; }
}; class PieceBoard
{
public:
PieceBoard(string black,string white):m_blackName(black),m_whiteName(white)
{ }
~PieceBoard() {
Clear();
}
void SetPiece(PieceColor color,Pos pos)
{
Piece * piece = NULL;
if(color == BLACK)
{
piece = new BlackPiece(color,pos);
std::cout << m_blackName << "在位置(" << pos.getX() << ","<<pos.getY() <<")" <<endl ;
piece->Draw();
m_pieceArray.push_back(piece);
}
else
{
piece = new WhitePiece(color,pos);
std::cout << m_whiteName << "在位置(" << pos.getX() << ","<<pos.getY() <<")" <<endl;
piece->Draw();
m_pieceArray.push_back(piece);
}
}
void Clear()
{
int size = m_pieceArray.size();
for(int i = ; i < size ; i++)
delete m_pieceArray[i];
}
private:
std::vector<Piece*> m_pieceArray;
std::string m_blackName;
std::string m_whiteName;
};

主函数:

#include "flyweight.h"
int main()
{
PieceBoard pieceBoard("A","B");
pieceBoard.SetPiece(BLACK,Pos(,));
pieceBoard.SetPiece(WHITE,Pos(,)); system("pause");
return ;
}

可以发现,棋盘的容器中存放了已下的棋子,而每个棋子包含棋子的所有属性。一盘棋往往需要含上百颗棋子,采用上面这种实现,占用的空间太大了。如何改进呢?用享元模式。其定义为:运用共享技术有效地支持大量细粒度的对象。

在围棋中,棋子就是大量细粒度的对象。其属性有内在的,比如颜色、形 状等,也有外在的,比如在棋盘上的位置。内在的属性是可以共享的,区分在于外在属性。因此,可以这样设计,只需定义两个棋子的对象,一颗黑棋和一颗白棋, 这两个对象含棋子的内在属性;棋子的外在属性,即在棋盘上的位置可以提取出来,存放在单独的容器中。相比之前的方案,现在容器中仅仅存放了位置属性,而原 来则是棋子对象。显然,现在的方案大大减少了对于空间的需求。

关注PieceBoard 的容器,之前是vector<Piece*> m_vecPiece,现在是vector<PiecePos> m_vecPos。这里是关键。

棋子的新定义,只包含内在属性:

#include <iostream>
#include <vector>
#include <string> enum PieceColor {BLACK,WHITE};
using namespace std; class Pos
{
public:
Pos(int x,int y):m_x(x),m_y(y)
{ }
int getX()
{
return m_x;
}
int getY()
{
return m_y;
}
private:
int m_x;
int m_y;
}; class Piece
{
public:
Piece(PieceColor color):m_color(color){};
~Piece() {}
virtual void Draw() {}
protected:
PieceColor m_color; }; class WhitePiece : public Piece
{
public:
WhitePiece(PieceColor color):Piece(color){}
~WhitePiece();
virtual void Draw() {
cout << "draw a white piece" << endl;
}
}; class BlackPiece:public Piece
{
public:
BlackPiece(PieceColor color):Piece(color){}
~BlackPiece();
virtual void Draw(){ cout << "draw a black piece" <<endl; }
}; class PieceBoard
{
public:
PieceBoard(string black,string white):m_blackName(black),m_whiteName(white)
{ }
~PieceBoard() {
Clear();
}
void SetPiece(PieceColor color,Pos pos)
{
Piece * piece = NULL;
if(color == BLACK)
{
piece = new BlackPiece(color);
std::cout << m_blackName << "在位置(" << pos.getX() << ","<<pos.getY() <<")" <<endl ;
piece->Draw();
m_blackPosArray.push_back(pos);
}
else
{
piece = new WhitePiece(color);
std::cout << m_whiteName << "在位置(" << pos.getX() << ","<<pos.getY() <<")" <<endl;
piece->Draw();
m_whitePosArray.push_back(pos);
}
}
void Clear()
{
//int size = m_pieceArray.size();
//for(int i = 0; i < size ; i++)
// delete m_pieceArray[i];
}
private:
std::vector<Pos> m_whitePosArray;
std::vector<Pos> m_blackPosArray;
std::string m_blackName;
std::string m_whiteName;
};

主函数:

#include "flyweight.h"
int main()
{
PieceBoard pieceBoard("A","B");
pieceBoard.SetPiece(BLACK,Pos(,));
pieceBoard.SetPiece(WHITE,Pos(,)); system("pause");
return ;
}

c++ 享元模式(flyweight)的更多相关文章

  1. 二十四种设计模式:享元模式(Flyweight Pattern)

    享元模式(Flyweight Pattern) 介绍运用共享技术有效地支持大量细粒度的对象. 示例有一个Message实体类,某些对象对它的操作有Insert()和Get()方法,现在要运用共享技术支 ...

  2. 设计模式(十)享元模式Flyweight(结构型)

    设计模式(十)享元模式Flyweight(结构型) 说明: 相对于其它模式,Flyweight模式在PHP实现似乎没有太大的意义,因为PHP的生命周期就在一个请求,请求执行完了,php占用的资源都被释 ...

  3. 乐在其中设计模式(C#) - 享元模式(Flyweight Pattern)

    原文:乐在其中设计模式(C#) - 享元模式(Flyweight Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 享元模式(Flyweight Pattern) 作者:weba ...

  4. 享元模式 FlyWeight 结构型 设计模式(十五)

    享元模式(FlyWeight)  “享”取“共享”之意,“元”取“单元”之意. 意图 运用共享技术,有效的支持大量细粒度的对象. 意图解析 面向对象的程序设计中,一切皆是对象,这也就意味着系统的运行将 ...

  5. 享元模式-Flyweight(Java实现)

    享元模式-Flyweight 享元模式的主要目的是实现对象的共享,即共享池,当系统中对象多的时候可以减少内存的开销,通常与工厂模式一起使用. 本文中的例子如下: 使用享元模式: 小明想看编程技术的书, ...

  6. Java享元模式(Flyweight Pattern)

    享元模式(Flyweight Pattern)主要用于减少创建的对象数量,并减少内存占用并提高性能. 这种类型的设计模式属于结构模式,因为该模式提供了减少对象计数的方法,从而改善应用的对象结构. 享元 ...

  7. 设计模式-11享元模式(Flyweight Pattern)

    1.模式动机 在面向对象程序设计过程中,有时会面临要创建大量相同或相似对象实例的问题.创建那么多的对象将会耗费很多的系统资源,它是系统性能提高的一个瓶颈. 享元模式就是把相同或相似对象的公共部分提取出 ...

  8. 设计模式系列之享元模式(Flyweight Pattern)——实现对象的复用

    说明:设计模式系列文章是读刘伟所著<设计模式的艺术之道(软件开发人员内功修炼之道)>一书的阅读笔记.个人感觉这本书讲的不错,有兴趣推荐读一读.详细内容也可以看看此书作者的博客https:/ ...

  9. 享元模式 - Flyweight

    Flyweight(享元模式) 定义 GOF:运用共享技术有效地支持大量细粒度的对象. GOF的定义比较专业化,通俗来说,当你有大量相似的实例时,你把其中相同的实例取出来共享. 例子 在你的游戏场景中 ...

  10. 享元模式/Flyweight模式/对象结构型/设计模式

    flyweight 享元模式(对象结构型) Flyweight在拳击比赛中指最轻量级,即"蝇量级"或"雨量级",这里选择使用"享元模式"的意 ...

随机推荐

  1. python time模块 sys模块 random模块

    1,time模块 python中的内置模块 #1,显示当前时间戳 print(time.time()) #2,字符串格式化 print(time.strftime('%Y-%m-%d-%H-%M-%S ...

  2. Android Java访问本地方法(JNI)

    当功能需要本地代码实现的时候,Java 代码就需要调用本地代码. 在调用本地代码时,首先要保证本地代码被加载到 Java 执行环境中并与 Java 代码连接在一起,这样 Java 代码在调用本地方法时 ...

  3. ubuntu 进入单用户模式

    进入单用户模式:  按shift进入 1.开机到grub时,用上下键移到第二行的恢复模式,按e(注意不是回车) 即Ubuntu,With Linux 3.2.0-23-generic(recovery ...

  4. c#实现QQ群成员列表导出及邮件群发之群列表及群成员获取

    主题已迁移至:http://atiblogs.com/ ITO-神奇的程序员

  5. python中scipy学习——随机稀疏矩阵及操作

    1.生成随机稀疏矩阵: scipy中生成随机稀疏矩阵的函数如下: scipy.sparse.rand(m,n,density,format,dtype,random_state) 1 参数介绍: 参数 ...

  6. nginx web服务优化

    nginx基本安全优化 1. 调整参数隐藏nginx软件版本号信息 软件的漏洞和版本有关,我们应尽量隐藏或消除web服务对访问用户显示各类敏感信息(例如web软件名称及版本号等信息),这样恶意的用户就 ...

  7. 5月3日上课笔记-properties文件,junit测试,mvc封层等

    StringBuffer 线程安全,效率低 StringBuilder 线程不安全,效率高 判断数组是null还是空数组 null 空数组 int[] array=null; int[] array2 ...

  8. 安装OpenResty

    ./configure --prefix=/data/openresty_10002 --with-luajit --with-http_stub_status_module OpenResty,也被 ...

  9. thymeleaf switch在表格中的使用,遇到的空行问题

    switch在表格中的使用时 如果把<td>写在<div th:switch="${data.isShow}"> 里面导致外面出现很多空的<div&g ...

  10. Tkinter Anchors(锚)

    Python GUI - Tkinter Anchors: 锚是用来定义文本的相对位置参考点   锚是用来定义文本的相对位置参考点. 这里是锚属性可以使用的常数列表. NW N NE W CENTER ...