Application of design pattern in game-builder mode (ix)

Source: Internet
Author: User

Builder pattern: Separates the construction of a complex object from its representation so that the same build process can create different representations. Builder mode is an object-creation pattern. By this definition, we can conclude that the builder is a creation pattern, that is, the output of the builder pattern is an object, that is, product in a UML class diagram.
Let's look at the UML class diagram for Builder mode:

As we can see in the UML class diagram, the builder pattern uses three relationships of aggregation, inheritance, and dependency. The first question is why aggregations are used, and if we do not use aggregations, we always get stable build processes, such as 2 hands per NPC in our game, when we want our game to be a hand on some occasions, We need to add the logic of generating one hand to each concretebuilder, which is very redundant.
The second question is why we use inheritance, which is a good answer, because with this foundation we can let each inherited builder subclass need to implement the pure virtual function defined in builder. Prevents certain concretebuilder from forgetting to write certain functions that cause a lack of arms in the case of a missing leg.
The last question naturally is why use dependency, in fact this is also better answer, because we want to generate a product. At the same time we can conclude through this dependency that the builder pattern is encapsulated on the basis of product, which means that we do not like to change the product but instead call the Porduct method.
There will be a variety of logs in the game, and log often has a common library, we use these log when we do not want to change the internal structure of log, just use the various methods provided by this log. Log can be categorized into various types, such as debug information, error messages, warning messages, and custom information. We want each log to be different in color under Windows, so we could look at the same color as each log under Linux.
Code such as:

//MVC.cpp: Defines the entry point of the console application. //#include "stdafx.h"#include <iostream>#include <string>#include <sstream>usingNamespace Std;class log{ Public:Log():M_color(0),m_size(0),M_iswritefile(false){};voidSetSize (intSize) {m_size = size; }voidSetColor (intcolor) {M_color = color; }voidWriteFile (BOOL value) {M_iswritefile =value; }voidPrintinfo () {cout<<"M_color:"<<m_color<<endl<<"M_size:"<<m_size<<endl<<"M_iswritefile"<<m_isWriteFile<<endl; cout<<"----------------------------"<<endl; }Private:intM_color;intM_size;BOOLM_iswritefile;}; Class ilogbuider{ Public:Ilogbuider(){};Virtual~ilogbuider () {};Virtual voidSetColor () =0;Virtual voidSetSize () =0;Virtual voidWriteFile () =0;}; Class Dlogbuiler: Publicilogbuider{ Public:Dlogbuiler()    :m_product(New Log())    {}voidSetSize () {m_product->setsize (1); }voidSetColor () {M_product->setcolor (1); }voidWriteFile () {M_product->writefile (true); } log* GetLog () {returnM_product.Get(); }Private: Auto_ptr<log> m_product;}; Class Elogbuiler: Publicilogbuider{ Public:Elogbuiler()    :m_product(New Log())    {}voidSetSize () {m_product->setsize (2); }voidSetColor () {M_product->setcolor (2); }voidWriteFile () {M_product->writefile (true); } log* GetLog () {returnM_product.Get(); }Private: Auto_ptr<log> m_product;}; Class wdirector{ Public:Wdirector(){};voidConstruct (ilogbuider* builder) {Builder->setcolor ();        Builder->setsize ();    Builder->writefile (); }};class ldirector{ Public:Ldirector(){};voidConstruct (ilogbuider* builder) {builder->setsize ();    Builder->writefile (); }};voidMain () {//windows;cout<<"------------Windows-----------------"<<endl; Auto_ptr<wdirector> Wdirector (NewWdirector ()); Auto_ptr<ldirector> Ldirector (NewLdirector ()); Auto_ptr<dlogbuiler>dbuilder (NewDlogbuiler ()); Auto_ptr<elogbuiler>ebuilder (NewElogbuiler ()); Wdirector->construct (Dbuilder.Get()); Wdirector->construct (Ebuilder.Get());    Dbuilder->getlog ()->printinfo (); Ebuilder->getlog ()->printinfo ();//linuxcout<<"------------Linux-----------------"<<endl; Dbuilder.reset (NewDlogbuiler ()); Ebuilder.reset (NewElogbuiler ()); Ldirector->construct (Dbuilder.Get()); Ldirector->construct (Ebuilder.Get());    Dbuilder->getlog ()->printinfo (); Ebuilder->getlog ()->printinfo ();}

The builder pattern is a pattern of creation, primarily to encapsulate the product so that it adapts to different situations, while the product has a stable creation process inside it.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Application of design pattern in game-builder mode (ix)

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.