The builder mode of the Novice design mode series notes

Source: Internet
Author: User

Provides a "encapsulation mechanism" to isolate changes in "parts of complex objects".

In order to keep the "stable building algorithm" in the system, it does not change with the demand. ———— Builder Mode

Construction mode is the creation mode of the object

Construction mode separates the internal representation of a product from the production process,

This can be a construction process that generates product objects with different internal representations.

Intent:

Separates the construction of a complex object from its representation so that the same build process can create different representations

Motivation:

During the construction of complex objects, allowing the same construction process to add new constructed elements, structured construction process

The construction model uses a Director object and the concrete builder object to build all the parts one by one, thus creating the complete object.

The builder model hides the structure of the product and the part construction process of the product to the client, separating the responsibility for directing the construction process from the responsibility of the concrete builder's parts, to achieve the purpose of division of responsibility and encapsulation.


Builder: Specify an abstract interface for each part of creating a Product object

Concretebulider:

Implement the builder interface to construct and assemble the various parts of the product;

Define and affirm the representations that he creates;

Provides an interface for retrieving products

Director:

Enough to make an object that uses the builder interface

Product

Represents a constructed complex object that contains the classes that define the constituent parts, including the interfaces that these parts are assembled into the final product


In the online shooter game, every character in the game needs to provide a complete
Role modeling, including character styling, clothing, weapons, etc., how to create a
The full game character?



public class Clothing {public clothing () {super ();//TODO auto-generated constructor stubSystem.out.println ("Clothing New ");}}

public class Character {public Character () {super ();//TODO auto-generated constructor StubSystem.out.println (" Character new ");}}

public class Weapon {public weapon () {super ();//TODO auto-generated constructor StubSystem.out.println ("Weapon New");}}

public class Role {private clothing clothing;private Character character;private weapon Weapon;public Role () {System.out. println ("Role () New");} Public Role (clothing clothing, Character Character, weapon weapon) {super (); System.out.println ("Role (CCP) new"); this.clothing = Clothing;this.character = Character;this.weapon = weapon;} Public clothing getclothing () {return clothing;} public void setclothing (clothing clothing) {this.clothing = clothing;} Public Character Getcharacter () {return Character;} public void Setcharacter (Character Character) {this.character = Character;} Public Weapon Getweapon () {return weapon;} public void Setweapon (weapon weapon) {this.weapon = weapon;}}

Public interface Rolebuilder {public void buildclothing ();p ublic void Buildcharacter ();p ublic void Buildweapon ();p ublic Role getrole ();}
public class Concreterolebuilder implements Rolebuilder {protected Role role=new role (); @Overridepublic void Buildclothing () {//TODO auto-generated method stubrole.setclothing (new clothing ());} @Overridepublic void Buildcharacter () {//TODO auto-generated method Stubrole.setcharacter (New Character ());} @Overridepublic void Buildweapon () {//TODO auto-generated method Stubrole.setweapon (New weapon ());} @Overridepublic role Getrole () {//TODO auto-generated method Stubreturn role;}}
Public class Director {private Rolebuilder Rolebuilder;public director (Rolebuilder rolebuilder) {super (); System.out.println ("director New"); this.rolebuilder = Rolebuilder;} public void Setbulider (Rolebuilder rolebuilder) {this.rolebuilder=rolebuilder;} Public Role construct () {rolebuilder.buildcharacter (); rolebuilder.buildclothing (); Rolebuilder.buildweapon (); return Rolebuilder.getrole ();}}

public class Client {public static final void main (string[] args) {Rolebuilder builder =new concreterolebuilder ();D Irector Director=new Director (builder); Role role =director.construct ();}}


Builder Mode Features:

1, the builder mode of "processing technology" is exposed. This makes the builder model more flexible.

2, decoupling the assembly process and creating specific components, so that we do not have to care about how each part is assembled.


Key points of implementation:

1. The builder mode is mainly used to "construct a complex object in steps", in which the "step" is a stable algorithm, while the parts of complex objects change frequently.

2, the product does not need abstract class, it is unlikely to extract an abstract product class.

3. The interface method for creating subassemblies in the creator is not an abstract method, but an empty method, without any action.

The specific creator only needs to overwrite the method to be able, but this is not absolute, particularly despise similar text conversion in this case, the default method will input intact output is a reasonable default operation.

4, the abstract factory model to solve the "series of objects" demand changes, builder mode to solve the "object part" of the change in demand,

The builder pattern is often used in combination with the combined pattern.


Builder Mode Application Scenario:

(1) The need to generate a product object has a complex internal structure. Each internal component itself can be an object, or it can make an object an integral part.

(2) The properties of the product objects that need to be generated depend on each other. The construction model can enforce a step-by-stage construction process.

(3) Some other objects in the system are used during object creation, which are not easily available during the creation of the Product object



The builder mode of the Novice design mode series notes

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.