Follow the example learning design pattern-generator mode (builder)

Source: Internet
Author: User

The builder pattern is a created design pattern.

Design intent: Separate a complex class representation from its construction so that the same build process can derive different representations.

Instance class diagram:


Ivehiclebuilder: Abstract builder, to create a vehicle object and create an abstract interface specified by its related parts, the production process of the product is broken down into different steps, so that the concrete builders have more flexibility in the concrete construction steps, thus creating different representations of the product. (This is the car and the truck)

Carbuilder, Trunkbuilder: Concrete Builders, implement Ivehiclebuilder interfaces, construct and assemble each part of the product definition and define the representation it creates, and provide an interface to return the product.

Director: Conductor, build an object that uses the Ivehiclebuilder interface.

Vehicle: Automotive products, complex objects that are built, concrete product builders, create an internal representation of the product and define its assembly process.

Let's take a look at what the Director has done!

Package Com.builder;import Com.factory.abstrcatfactory.model.engine;import Com.factory.abstrcatfactory.model.Tank ; Import com.factory.abstrcatfactory.model.vehicle;/** Builder's command builder mode can build two series of items in the same step * in the series of products that build the same steps, the amount of code is better than the abstract factory pattern. But the increase in the number of different steps of each product increases the abstraction of the factory, since the functional class is better insulated. * @author Gaoxu * Practice the truth! */public class Director {private Ivehiclebuilder _vehiclebuilder = Null;public Director (ivehiclebuilder vehiclebuilder) {_vehiclebuilder = Vehiclebuilder;} Public Vehicle createvehicle () {Engine engine = _vehiclebuilder.builderengine (); Tank Tank = _vehiclebuilder.buildertank (); Vehicle Vehicle = _vehiclebuilder.buildervehicle (Engine,tank); return Vehicle;}}

We see that the conductor is responsible for building the product, and then looking at the client code, we know how important the conductor is, and the generator pattern must have a conductor.

Package Com.builder;import com.factory.abstrcatfactory.model.vehicle;/** * @author Gaoxu * Practice a genuine knowledge! */public class Client {public static void main (string[] para) {//trolley Ivehiclebuilder Carbuilder = new Carbuilder ();D irector D Irector = new Director (Carbuilder); Vehicle car = director.createvehicle ();//truck Ivehiclebuilder Trunkbuilder = new Trunkbuilder ();D irector directort = new Dir Ector (Trunkbuilder); Vehicle trunk = Directort.createvehicle ();}}

As we look at the code of the concrete builders, we know that each concrete builder is a concrete implementation of the overall process of building a product.

Car concrete builders, build engines, build fuel tanks, build cars. These are encapsulated in a concrete builder, so that the separation of the build and presentation is displayed, and the shadow that appears on the client side without any concrete builder content is completely encapsulated.

Package Com.builder;import Com.factory.abstrcatfactory.model.engine;import Com.factory.abstrcatfactory.model.Tank Import com.factory.abstrcatfactory.model.vehicle;/** Trolley Concrete Implementation Class * @author Gaoxu * Practice the truth! */public class Carbuilder implements Ivehiclebuilder {@Overridepublic Engine builderengine () {//TODO auto-generated Meth OD stubreturn null;} @Overridepublic Tank Buildertank () {//TODO auto-generated method Stubreturn null;} @Overridepublic Vehicle buildervehicle (Engine engine,tank Tank) {//TODO auto-generated method Stubreturn null;}}

Package Com.builder;import Com.factory.abstrcatfactory.model.engine;import Com.factory.abstrcatfactory.model.Tank Import com.factory.abstrcatfactory.model.vehicle;/** Truck Concrete Implementation class * @author Gaoxu * practical knowledge! */public class Trunkbuilder implements Ivehiclebuilder {@Overridepublic Engine builderengine () {//TODO auto-generated Me Thod Stubreturn null;} @Overridepublic Tank Buildertank () {//TODO auto-generated method Stubreturn null;} @Overridepublic Vehicle buildervehicle (Engine engine,tank Tank) {//TODO auto-generated method Stubreturn null;}}

The example above is also the creation of the car, we can see the same is the creation of the car, the generator mode is much less than the abstract factory pattern of code a lot more, of course, in terms of decoupling or abstract factory is better, because the builder in the concrete builder in the build process of the product parts, If this process is more complex then the concrete builder will be very large and the error rate will be very high, and the abstract factory is better in this respect, each component is a separate building interface and class.


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

Follow the example learning design pattern-generator mode (builder)

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.