Enjoy meta mode FlyWeight

Source: Internet
Author: User
Tags gety

As an example of Go, the chess board of Go has a total of 361 squares, can put 361 pieces. Now to implement a go program, how to do it? The first thing to consider is the implementation of the chess board, you can define a pawn of the class, the member variables include the color of the pieces, shape, position and other information, and then define a chessboard class, member variables have a container for the object to hold the pieces. The following code shows:

The definition of a piece, of course, except the color and position of the pieces, there are other, omitted here. These two properties are sufficient to illustrate the problem.

#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):P iece (color,pos) {} ~whitepiece ();    virtual void Draw () {cout << "Draw a white piece" << Endl;    }};class blackpiece:public piece{public:blackpiece (piececolor color,pos Pos):P iece (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 << "in position (" << pos.getx () << "," <<pos.gety () << ")" <<            Endl;            Piece->draw ();        M_piecearray.push_back (piece);            } else {piece = new whitepiece (Color,pos); Std::cout << m_whitename << "in position (" << pos.getx () << "," <<pos.gety () << ")" <<            Endl            Piece->draw ();        M_piecearray.push_back (piece);        }} void Clear () {int size = M_piecearray.size ();    for (int i = 0; i < size; i++) Delete m_piecearray[i];    }private:std::vector<piece*> M_piecearray;    Std::string M_blackname; Std::string m_whitename;};

Main function:

#include "flyweight.h" int main () {    pieceboard pieceboard ("A", "B");    Pieceboard.setpiece (Black,pos (bis));    Pieceboard.setpiece (White,pos (16,6));    System ("pause");    return 0;}

It can be found that the Board's container holds the pieces that have been placed, and each piece contains all the attributes of the pawn. A chess game often needs to contain hundreds of pieces, using the above realization, occupy too much space. How to improve it? Use the enjoy meta mode. It is defined as: the use of shared technology to effectively support a large number of fine-grained objects.

In Weiqi, a piece is a large number of fine-grained objects. Its properties are intrinsic, such as color, shape, etc., as well as external, such as the position on the board. Intrinsic properties can be shared and differentiated by external attributes. Therefore, it can be designed so that only two pieces of the object, a black chess and a white chess, these two objects contain the intrinsic properties of the pieces, the external properties of the chess piece, that is, the position on the board can be extracted, stored in a separate container. Compared to the previous scenario, the container now only holds the position attribute, whereas the original is a pawn object. Clearly, the current programme has greatly reduced the need for space.

Focus on the Pieceboard container, previously vector<piece*> m_vecpiece, now vector<piecepos> M_vecpos. Here is the key.

A new definition of a pawn that contains only intrinsic properties:

#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):P iece (color) {} ~whitepiece ();    virtual void Draw () {cout << "Draw a white piece" << Endl;    }};class blackpiece:public piece{public:blackpiece (piececolor color):P iece (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 << "in position (" << pos.getx () << "," <<pos.gety () << ")" <<            Endl;            Piece->draw ();        M_blackposarray.push_back (POS);            } else {piece = new whitepiece (color); Std::cout << m_whitename << "in position (" << 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;};

Main function:

#include "flyweight.h" int main () {    pieceboard pieceboard ("A", "B");    Pieceboard.setpiece (Black,pos (bis));    Pieceboard.setpiece (White,pos (16,6));    System ("pause");    return 0;}
Reprinted from: http://www.cnblogs.com/onlycxue/p/3487863.html

Enjoy meta mode FlyWeight

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.