C++设计模式——享元模式(高屋建瓴)

本文介绍了一个使用C++实现的五子棋游戏,包括棋子类CPiece、棋子颜色枚举PIECECOLOR、棋盘类ChessBoard和棋子工厂类CPieceFactory。棋子工厂类负责避免重复创建相同颜色的棋子,而棋盘类用于绘制棋子并展示所有棋子的位置。代码中详细展示了各个类的定义和使用方法。
摘要由CSDN通过智能技术生成

原网址: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:
 // 判断颜色(白色或者黑色)是否已经创建,来避免重复创建CPiece
 CPiece *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 chess
  cout<<"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 .
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值