The application of design pattern in game-builder mode (ix)

Source: Internet
Author: User
Tags int size

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

In UML class diagrams, we can see that the builder pattern uses the three relationships of aggregation, inheritance, and dependency. The first question is why we use aggregations, and if we don't use aggregations, we always get a stable build process, for example, every NPC in our game has 2 hands, and when we want our game to have a hand on some occasions, We need to add the logic of generating one hand to each concretebuilder, very redundant.
The second question is why we use inheritance, which is a relatively good answer, because we can make each inherited builder subclass need to implement the pure virtual function defined in builder. Preventing certain concretebuilder from forgetting to write certain functions leads to the absence of an arm-short leg.
The last question, naturally, is why you use dependencies, which is also a good answer, because we want to generate a product. At the same time we can conclude from 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 to invoke the Porduct method.
There are all kinds of log in the game, and log often has a public library, we use these log when we do not want to change the internal structure of log, just use this log provides a variety of methods. Log can be divided into various types, such as debugging information, error messages, warning messages, and custom information. We want the color of each log under Windows to be different for us to see, like the color of each log under Linux.
The following diagram of the code:

MVC.cpp: Defines the entry point for a console application.

#include "stdafx.h" #include <iostream> #include <string> #include <sstream> using namespace std;
    Class Log {Public:log (): M_color (0), m_size (0), M_iswritefile (false) {};
    void SetSize (int size) {m_size = size;
    } void SetColor (int color) {m_color = color;
    } void WriteFile (bool value) {m_iswritefile = value; } void Printinfo () {cout<< "M_color:" <<m_color<<endl<< "m_size:" <<m_size<
        <endl<< "M_iswritefile" <<m_isWriteFile<<endl;
    cout<< "----------------------------" <<endl;
    } Private:int M_color;
    int m_size;
BOOL M_iswritefile;

};
    Class Ilogbuider {Public:ilogbuider () {};
    Virtual ~ilogbuider () {};
    virtual void SetColor () = 0;
    virtual void SetSize () = 0;
virtual void WriteFile () = 0;

};
Class Dlogbuiler:public Ilogbuider {Public:dlogbuiler ()    : M_product (New Log ()) {} void SetSize () {m_product->setsize (1);
    } void SetColor () {m_product->setcolor (1);
    } void WriteFile () {m_product->writefile (true);
    } log* GetLog () {return m_product.get ();
} private:auto_ptr<log> m_product;

};
        Class Elogbuiler:public Ilogbuider {Public:elogbuiler (): M_product (New Log ()) {} void SetSize () {
    M_product->setsize (2);
    } void SetColor () {M_product->setcolor (2);
    } void WriteFile () {m_product->writefile (true);
    } log* GetLog () {return m_product.get ();
} private:auto_ptr<log> m_product;

};
    Class Wdirector {Public:wdirector () {};
        void construct (ilogbuider* builder) {Builder->setcolor ();
        Builder->setsize ();
    Builder->writefile ();

}
};
  Class Ldirector {Public:ldirector () {};  void construct (ilogbuider* builder) {builder->setsize ();
    Builder->writefile ();



}
};
    void Main () {//windows;
    cout<< "------------Windows-----------------" <<endl;
    Auto_ptr<wdirector> Wdirector (New Wdirector ());
    Auto_ptr<ldirector> Ldirector (New Ldirector ());
    Auto_ptr<dlogbuiler>dbuilder (New Dlogbuiler ());

    Auto_ptr<elogbuiler>ebuilder (New Elogbuiler ());
    Wdirector->construct (Dbuilder.get ());

    Wdirector->construct (Ebuilder.get ());
    Dbuilder->getlog ()->printinfo ();

    Ebuilder->getlog ()->printinfo ();
    Linux cout<< "------------linux-----------------" <<endl;
    Dbuilder.reset (New Dlogbuiler ());

    Ebuilder.reset (New Elogbuiler ());
    Ldirector->construct (Dbuilder.get ());

    Ldirector->construct (Ebuilder.get ());
    Dbuilder->getlog ()->printinfo ();
Ebuilder->getlog ()->printinfo ();
 }

The

Builder pattern is a form of creation that encapsulates the product to accommodate different situations, while there is a stable creation process within the product.

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.