Design Pattern 3 -- builder Pattern

Source: Internet
Author: User

1. Interpretation 1.1 Definition

Separates the construction of a complex object from its representation so that different representations can be created for the same build.

1.2 Analysis

First, let's look at the general method of instantiating an object, as shown in the following code:

Roboter roboter = new Roboter();roboter.setmArm("arm");roboter.setmBody("body");roboter.setmHead("head");roboter.setmFoot("foot");

The Roboter class contains the set and get functions of the corresponding attributes. The value assignment (build) and value acquisition (Presentation) are completed in the same class. Such a structure is not separated.

So what is the separation like, as shown in the class diagram of the builder mode below:


First, there is a class definition build InterfaceHe is the Builder in the figure and defines the setmArm (), setmBody (), setmHead (), and setmFoot () above ().

Then there is a class command to buildHe is the ctor in the figure. It solves the problem of how to build. For example, you only need to build the setmArm (), setmBody () attributes, Or you only need to build setmArm (), setmBody (), setmHead () these three attributes.

Then there is a class to implement or inherit the interfaces defined by the Builder.Is responsible for setting the attribute to a different value, which is the ConcreteBuilder in the figure. It solves the problem of building, such as setmArm ("arm") or setmArm ("not a arm ").

The builder mode divides the building into several parts. Through a unified building process, different Derector and ConcreteBuilder are used to construct objects of different forms.

1.3 advantages

1. There are many attributes to be initialized to prevent missing attributes from being initialized.. If Human has seven attributes, do you miss the number?

Human human = new Human();human.setmAge("18");human.setmDepardment("E");human.setmHeight("170");human.setmName("Alba");human.setmNumber("10003021");human.setmPhoneNumber("13194697638");

2. Reduce the number of constructor and reduce the parameters of the constructor.. You must have seen a similar code. Maybe you have written it again. Do you hate it?

public Human(String pName) {this.mName = pName;}public Human(String pName, String pSex) {this.mName = pName;this.mSex = pSex;}public Human(String pName, String pSex, String pNumber) {this.mName = pName;this.mSex = pSex;this.mNumber = pNumber;}public Human(String pName, String pSex, String pNumber, String pDepardment){this.mName = pName;this.mSex = pSex;this.mNumber = pNumber;this.mDepardment = pDepardment;}

3. It's just for neat appearance. If there is only one line of initialization below, do you think it should be neat. (A little far-fetched)

Human human = new Human();human.setmAge("18");human.setmDepardment("E");human.setmHeight("170");human.setmName("Aha");human.setmNumber("10003021");human.setmPhoneNumber("13194697638");human.display();

1.4 Use the character model in the scene game. The game contains various characters. Their attributes are the same, but their performance is different. For example, different monsters have different blood volume, attack, defense, experience, and appearance. Different ConcreteBuilder can be used to create different characters with the same attributes.
2. code example 2.1 Standard writing

Following the class diagram in the previous builder mode, the class diagram below is drawn and implemented.


RoboterBuilder defines all attribute setting interfaces and defines an interface to return the Roboter object.Note that there must be a method to return the Roboter object.:

public interface RoboterBuilder {void buildHead();void buildArm();void buildBody();void buildFoot();Roboter buildRoboter();}

RoboterDirector defines the attributes to be set for building objects. Note that it requires materials, which is the implementation class of RoboterBuilder.Note that he calls all the methods of RoboterBuilder and returns a Roboter object.:

public class RoboterDirector {public Roboter createRoboter(RoboterBuilder pBuilder) {pBuilder.buildHead();pBuilder.buildBody();pBuilder.buildArm();pBuilder.buildFoot();return pBuilder.buildRoboter();}}

SimpleRoboterBuilder implements the methods defined by RoboterBuilder. You can set different values for attributes as needed.Note that the buildRoboter () method returns a Roboter object. Director needs this method.:

public class SimpleRoboterBuilder implements RoboterBuilder {private Roboter mRoboter;public SimpleRoboterBuilder() {mRoboter = new Roboter();}@Overridepublic void buildHead() {mRoboter.setmHead("head");}@Overridepublic void buildArm() {mRoboter.setmArm("arm");}@Overridepublic void buildBody() {mRoboter.setmBody("body");}@Overridepublic void buildFoot() {mRoboter.setmFoot("foot");}@Overridepublic Roboter buildRoboter() {return mRoboter;}}

Roboter is an object that defines related attributes as well as various sets and get. There is no important logic here. You can skip this class:

public class Roboter {private String mHead;private String mArm;private String mBody;private String mFoot;public String getmHead() {return mHead;}public void setmHead(String mHead) {this.mHead = mHead;}public String getmArm() {return mArm;}public void setmArm(String mArm) {this.mArm = mArm;}public String getmBody() {return mBody;}public void setmBody(String mBody) {this.mBody = mBody;}public String getmFoot() {return mFoot;}public void setmFoot(String mFoot) {this.mFoot = mFoot;}}

How to use it is actually to call the createRoboter () method of RoboterDirector. If we need to build other style objects, modify the createRoboter () method or modify the SimpleRoboterBuilder class:

RoboterDirector director = new RoboterDirector();Roboter roboter = director.createRoboter(new SimpleRoboterBuilder());

2.2 common statements

Do you think "it seems that the builder mode you see is not like this, it is simpler than this ". The following describes another method, which is common.

There is an internal class Builder in the Entity class. Builder attributes correspond to those of RoboterWithBuilder, and there are many Set methods that are mainly used to Set corresponding attributes.The return value of each Set method is Builder, and a build () method returns the object we created.

public class RoboterWithBuilder {private String mHead;private String mArm;private String mBody;private String mFoot;public String getmHead() {return mHead;}public String getmArm() {return mArm;}public String getmBody() {return mBody;}public String getmFoot() {return mFoot;}private RoboterWithBuilder(Builder pBuilder) {mHead = pBuilder.mBuilderHead;mBody = pBuilder.mBuilderBody;mArm = pBuilder.mBuilderArm;mFoot = pBuilder.mBuilderFoot;}public static class Builder {private String mBuilderHead;private String mBuilderArm;private String mBuilderBody;private String mBuilderFoot;public Builder setmBuilderHead(String mBuilderHead) {this.mBuilderHead = mBuilderHead;return this;}public Builder setmBuilderArm(String mBuilderArm) {this.mBuilderArm = mBuilderArm;return this;}public Builder setmBuilderBody(String mBuilderBody) {this.mBuilderBody = mBuilderBody;return this;}public Builder setmBuilderFoot(String mBuilderFoot) {this.mBuilderFoot = mBuilderFoot;return this;}public RoboterWithBuilder build() {return new RoboterWithBuilder(this);}}}

Let's look at how to call, call the internal class Builder method to set properties, and finally call the build () method to return this object. (This is not enough)

RoboterWithBuilder roboter  = new RoboterWithBuilder.Builder().setmBuilderArm("arm").setmBuilderBody("body").setmBuilderHead("head").setmBuilderFoot("foot").build();

If this is the case, I do not need to remember the constructors of the object, and it is convenient to set attributes.

Code: https://github.com/bird7310/DesignPatternExample.git

3. Conclusion

This is a little hasty. I have read a group of books and wrote down the essence, but I think this explanation should be clearer than the previous two articles.

The text is referenced in the previous article, which does not change much. I feel that the article is not very attractive, and it is not deep enough. It is difficult to start with everything. The current goal is to clarify things. If you write more things, consider other improvements.

I hope you will give more comments and learn and grow together.

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.