No one buys a car with only one tire or steering wheel. Everyone buys a complete car with multiple components, such as tires, steering wheel, and engine. How to assemble these components into a complete car and return them to the user is a problem that needs to be solved by the builder mode. The builder mode is also known as the generator mode. It is a complex creation mode with a low usage frequency. The builder mode is not a simple product returned by the client, but a complex product composed of multiple components.
8.1 game role design
The game development team of Sunny software company decided to develop an online game named "Sunny heroes", which adopts the mainstream role playing game (Role Playing Game) model, players can play a specific role in the virtual world in the game. The role has different abilities based on different game plots and Statistics (such as power, magic, and skill, the role will also have more powerful capabilities as it continues to upgrade. As an important part of an RPG Game, You need to design the game role and add new roles as the game is upgraded. Different types of game roles have different external features, such as gender, face, clothing, and hair styles. For example, "angel" has beautiful faces and long hair with a shawl and is wearing a white dress; the "devil" is very ugly, with a bald head and a dazzling black coat. Sunny decided to develop a small tool to create game roles. Different types of roles can be created and new roles can be flexibly added. |
Developers at Sunny found through analysis that a game role is a complex object that contains multiple components, such as gender and face. The components of different game roles are different, as shown in 8-1:
Figure 8-1 different game role shapes
(Note: The model of the game role in this figure comes from the network)
Regardless of the shape of a game role, its creation steps are similar, you need to gradually create its components, and then assemble the components into a complete game role. How to create a complex object that contains multiple components step by step, the builder mode is born to solve such problems.
8.2 builder mode Overview
The builder mode is a complex creation mode. It separates the client from the creation process of complex objects that contain multiple components (or components, the client does not need to know the internal components and assembly methods of complex objects, but only needs to know the type of the builder to be created. It focuses on how to create a complex object step by step. Different builders define different creation processes, and the specific builders are independent of each other. It is very convenient to add new Builders without having to modify the existing code, the system has good scalability.
The builder mode is defined as follows:
Builder pattern: separates the construction of a complex object from its representation, so that different representations can be created during the same construction process. The builder mode is an object creation mode. |
The builder mode creates a complex object step by step. It allows users to build complex objects only by specifying the type and content of the complex objects. users do not need to know the specific internal building details. The builder mode structure is 8-2:
Figure 8-2 builder mode structure
The builder mode structure includes the following roles:
● Builder: It specifies an abstract interface for each part of a product object. Two methods are generally declared in this interface. One method is buildpartx (), they are used to create various parts of a complex object. The other method is getresult (), which is used to return complex objects. Builder can be either an abstract class or an interface.
● Concretebuilder: it implements the builder interface, implements the specific construction and assembly methods of each component, and defines and clarifies the complex objects it creates, you can also provide a method to return the created complex product object.
● Product (Product role): It is a complex object to be built and contains multiple components. A specific builder creates an internal representation of the product and defines its assembly process.
● Ctor (conductor): the conductor, also known as the director class, is responsible for arranging the construction order of complex objects. There is an association between the conductor and the abstract builder. It can be found in its construct () the component construction and assembly methods of the builder object are called in the Construction Method to complete the construction of complex objects. Generally, the client only needs to interact with the conductor, determine the type of the specific builder on the client, and instantiate the specific builder object (you can also use the configuration file and reflection mechanism ), then, the object is passed into the conductor class through the constructor or setter method.
Mentioned in the builder mode definitionComplex objectsSo what are complex objects? Simply put, a complex object is an object that contains multiple Member attributes, also known as a component or part, such as a car, including a steering wheel, an engine, or a tire, an email contains the sender, recipient, subject, content, attachments, and other components. A typical complex object code example is as follows:
Class Product { Private string parta; // defines a part. The part can be of any type, including value type and reference type. Private string partb; Private string partc; // The getter method and setter method of parta are omitted. // The getter method and setter method of partb are omitted. // The getter method and setter method of partc are omitted. } |
The abstract builder class defines the product creation and return methods. The typical code is as follows:
Abstract class Builder { // Create a product object Protected product Product = new product (); Public abstract void buildparta (); Public abstract void buildpartb (); Public abstract void buildpartc (); // Return the product object Public Product getresult () { Return product; } } |
In the abstract class builder, a series of abstract buildpartx () methods are used to create various components of complex products. The specific construction process is implemented in concretebuilder, and the factory method getresult () is also provided (), returns a complete product.
The buildpartx () method is implemented in concretebuilder. by calling the setpartx () method of product, you can set a value for the member attribute of the product object. Different Specific Builders will be different when implementing the buildpartx () method. For example, the parameters of the setpartx () method may be different. In some specific builder classes, some setpartx () the method does not need to be implemented (an empty implementation is provided ). These do not concern the client. The client only needs to know the specific builder type.
In the builder mode structure, a conductor class director is introduced, which has two main roles: on the one hand, it isolates the customer from the creation process; on the other hand, it controls the product creation process, including whether a buildpartx () method is called and the order of multiple buildpartx () methods. The conductor programming for the abstract builder. The client only needs to know the type of the builder and can call the relevant methods of the builder through the conductor class to return a complete product object. In real life, there are roles similar to the conductor. If a customer buys a computer, the computer sales staff is equivalent to the conductor, as long as the customer determines the computer type, computer sales personnel can notify the computer assembly personnel to assemble a computer for the customer. The code example of the conductor class is as follows:
Class director { Private Builder; Public Director (Builder) { This. Builder = builder; } Public void setbuilder (Builder) { This. Builder = builer; } // Product construction and assembly methods Public Product construct (){ Builder. buildparta (); Builder. buildpartb (); Builder. buildpartc (); Return builder. getresult (); } } |
An abstract builder type object can be injected into the conductor class. The core of the object is to provide a construct () construction method, in which the construct () method of the builder object is called, finally, a product object is returned.
For the client, you only need to care about the specific builder. Generally, the client-class code snippets are as follows:
...... Builder = new concretebuilder (); // It can be implemented through the configuration file Director dire= new director (builder ); Product = Director. Construct (); ...... |
You can use the configuration file to store the Class Name of the concretebuilder class, so that you do not need to modify the source code when replacing the new builder, making system expansion more convenient. In the client code, you only need to specify the type of the specific builder without worrying about the specific assembly process of the product object.
The builder mode is similar to the abstract factory mode, but the builder mode returns a complete and complex product, while the abstract factory mode returns a series of related products. In the abstract factory mode, the client selects a factory to generate the desired object. In the builder mode, the client specifies a specific builder type and guides the director class to generate an object, focusing on building a complex object step by step, then return the result. If we regard the abstract factory model as an auto parts manufacturer and generate different types of auto parts, then the builder model is a car assembly factory that returns a complete car by assembling the accessories.
|
Thoughts If there is no conductor Director, how will the client build a complex product? |
[Author: Liu Wei http://blog.csdn.net/lovelion] |