Design Mode C ++ implementation (9) -- metadata Mode

Source: Internet
Author: User

The design patterns in the software field provide developers with an effective way to use expert design experience. The design patterns use the important features of object-oriented programming languages: encapsulation, inheritance, and polymorphism. It may be a long process to truly comprehend the essence of the design patterns, which requires a lot of practical experience. I recently read a book on design patterns. I wrote a small example in C ++ for each pattern to help me better understand it. Refer to "big talk Design Patterns" and "design patterns: the basis for reusable object-oriented software" (DP. This topic describes how to implement the metadata mode.

For example, if the chess board of Go has a total of 361 cells, you can put 361 pieces. What should I do if I want to implement a go program? The first thing to consider is the implementation of the chess board. You can define a chess board class. The member variables include the color, shape, position, and other information of the chess board. In addition, you can define a chess board class, there is a container in the member variable to store the pawns object. The following code indicates:

The definition of the pawnpiece. Of course, the attributes of the pawnpiece include color and position. These two attributes are sufficient to illustrate the problem.

// Chess piece color Enum piececolor {black, white}; // chess piece position struct piecepos {int X; int y; piecepos (int A, int B): X (), Y (B) {}}; // define the class piece {protected: piececolor m_color; // color piecepos m_pos; // position public: piece (piececolor color, piecepos POS ): m_color (color), m_pos (POS ){}~ Piece () {}virtual void draw () {}}; class blackpiece: Public piece {public: blackpiece (piececolor, piecepos POS): piece (color, POS ){}~ Blackpiece () {} void draw () {cout <"" <Endl ;}}; class whitepiece: Public piece {public: whitepiece (piececolor, piecepos): piece (color, POS ){}~ Whitepiece () {} void draw () {cout <"drawing a white chess game" <Endl ;}};

Chessboard definition:

Class pieceboard {PRIVATE: vector <piece *> m_vecpiece; // The string m_blackname; // The black name string m_whitename; // The white name public: pieceboard (string black, string white): m_blackname (black), m_whitename (white ){}~ Pieceboard () {clear ();} void setpiece (piececolor color, piecepos POS) // move a piece of chess to put a piece of chess on the board {piece * piece = NULL; if (color = black) // {piece = new blackpiece (color, POS) under black ); // obtain the cout of a black game <m_blackname <"in the position (" <POS. x <',' <POS. Y <")"; piece-> draw (); // draw a pawn on the board} else {piece = new whitepiece (color, POS ); cout <m_whitename <"in location (" <POS. x <',' <POS. Y <")"; piece-> draw ();} m_vecpiece.push_back (piece); // Add to container} void clear () // release the memory {int size = m_vecpiece.size (); For (INT I = 0; I <size; I ++) delete m_vecpiece [I] ;}};

The customer's usage is as follows:

int main(){PieceBoard pieceBoard("A","B");pieceBoard.SetPiece(BLACK, PiecePos(4, 4));pieceBoard.SetPiece(WHITE, PiecePos(4, 16));pieceBoard.SetPiece(BLACK, PiecePos(16, 4));pieceBoard.SetPiece(WHITE, PiecePos(16, 16));}

It can be found that the board container stores the bottom piece, and each piece contains all the attributes of the piece. A game often needs to contain hundreds of pawns. The above implementation takes up too much space. How can we improve it? Use the metadata mode. It is defined as: using the sharing technology to effectively support a large number of fine-grained objects.

In go, chess pieces are a large number of fine-grained objects. Its Attributes are internal, such as color and shape, as well as external, such as the position on the board. Internal attributes can be shared, but they are distinguished by external attributes. Therefore, it can be designed in this way. You only need to define the objects of two chess pieces, a black game and a white game. These two objects contain the internal attributes of the chess piece, and the external attributes of the chess piece, that is, the position on the board can be extracted and stored in a separate container. Compared with the previous scheme, the current container only stores the location attribute, while the original is the pawn object. Obviously, the current solution greatly reduces the need for space.

Focus on the pieceboard container, which was previously vector <piece *> m_vecpiece, And now vector <piecepos> m_vecpos. Here is the key.

The new definition of a pawn contains only internal attributes:

// Chess piece color Enum piececolor {black, white}; // chess piece position struct piecepos {int X; int y; piecepos (int A, int B): X (), Y (B) {}}; // define the class piece {protected: piececolor m_color; // color public: piece (piececolor color): m_color (color ){}~ Piece () {}virtual void draw () {}}; class blackpiece: Public piece {public: blackpiece (piececolor color): piece (color ){}~ Blackpiece () {} void draw () {cout <"draw a \ n" ;}}; class whitepiece: Public piece {public: whitepiece (piececolor color ): piece (color ){}~ Whitepiece () {} void draw () {cout <"draw a white chess \ n ";}};

The corresponding chessboard is defined:

Class pieceboard {PRIVATE: vector <piecepos> m_vecpos; // the position where the pawns are stored in piece * m_blackpiece; // The * pipiece * m_whitepiece; // The string m_blackname; string m_whitename; public: pieceboard (string black, string white): m_blackname (black), m_whitename (white) {m_blackpiece = NULL; m_whitepiece = NULL ;}~ Pieceboard () {Delete m_blackpiece; Delete m_whitepiece;} void setpiece (piececolor color, piecepos POS) {If (color = black) {If (m_blackpiece = NULL) // only one black player m_blackpiece = new blackpiece (color); cout <m_blackname <"in position (" <POS. x <',' <POS. Y <")"; m_blackpiece-> draw ();} else {If (m_whitepiece = NULL) m_whitepiece = new whitepiece (color ); cout <m_whitename <"in location (" <POS. x <',' <POS. Y <")"; m_whitepiece-> draw ();} m_vecpos.push_back (POS );}};

The customer's usage is the same. Here we do not repeat it. Now we provide the UML diagram of the enjoy meta mode. Take go as an example. The Board contains two shared objects, black games and white pawns. The external attributes of all pawns are stored in a separate container.

I enjoy the copyright of blog articles, reprint please indicate the source http://blog.csdn.net/wuzhekai1985

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.