"Onlookers" design mode (9)--creation type creator pattern (Builder pattern)

Source: Internet
Author: User

Builder pattern (English: Builder pattern) is a design pattern, AKA: Build Mode, is an object building pattern. It can abstract the construction process of complex objects (abstract category) so that different implementations of this abstract process can construct objects of different manifestations (attributes). ----WIKIPEDIA


Personal Understanding:

The builder mode can set different properties or behaviors to create objects that are not the same. For example, the production of automobiles, if there are three important processes, shape, assembly, color. Then it can be set by the creator mode, the three processes in the different vehicles, the third process may be not the same. creating objects by creating a pattern is naturally different from the way they are represented.


Using the example above, draw a class diagram of his

The main code is shown below

public class Car {private string Shape;private string assemble;private string color;}

Public abstract class Carbuilder {protected Car car = new Car ();p ublic abstract void Setshape (String shape);p ublic Abstrac t void Setassemble (string assemble);p ublic abstract void SetColor (string color);p ublic Car Getcar () {return this.car;}}

public class Hongqicarbuilder extends carbuilder{@Overridepublic void Setshape (String shape) {this.car.setShape (SHAPE) ;} @Overridepublic void setassemble (String assemble) {this.car.setAssemble (assemble);} @Overridepublic void SetColor (String color) {this.car.setColor (color);}}

This example is written according to the definition and the understanding of the creator pattern, and the core idea is that it is the essence of this design to use different implementations to create different objects.


Also refer to other books as described in the Zen design pattern to create different behaviors.

Suppose such a scenario, the car from start, drive, stop three links, and then I want to be, I specify that the number of links in which the action of which links will occur. In this case, the action or behavior of the created object is different.


The main code

Public abstract class Carbuilder {public abstract void Setorder (list<string> orderlist);p ublic abstract Car Getcar ( );}
Public abstract class Car {private list<string> exeorderlist = new arraylist<string> ();p ublic abstract void St Artup ();p ublic abstract void Run ();p ublic abstract void Stop ();p ublic final void Templatemethod () {for (String exeorder:e Xeorderlist) {if ("StartUp". Equals (Exeorder)) {This.startup ();} else if ("Run". Equals (Exeorder)) {This.run ();} else if ("Stop". Equals (Exeorder)) {this.stop ();}}} Public final void Setorder (list<string> exeorderlist) {this.exeorderlist = Exeorderlist;}}

public class Hongqicar extends car{@Overridepublic void startup () {System.out.println ("Hongqi car Start");} @Overridepublic void Run () {System.out.println ("Red flag car in Motion");} @Overridepublic void Stop () {System.out.println ("Red Flag car Stop");}}
created byImplementation Class
public class Hongqicarbuilder extends carbuilder{private hongqicar hongqicar = new Hongqicar (); @Overridepublic void Setor Der (list<string> orderlist) {this.hongQiCar.setOrder (orderlist);} @Overridepublic Car Getcar () {return this.hongqicar;}}

the resulting object behaves differently, and this pattern is explained in a later post by using the template method pattern.


Scope of application

1. The same method, the order of different products produced is not the same.

2. A number of properties, assembled when the different assembled products are not the same.

3. The product class is complex, or the sequence of calls in the product class produces different performance.


Comparison

Compared to the factory method pattern, the creator is concerned with the type of part and the order of assembly, but the factory approach focuses on creating objects that can be parts, but it does not care about the assembly order.


Source code: Download Design pattern sources



"Onlookers" design mode (9)--creation type creator pattern (Builder pattern)

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.