Definition:
The construction of a complex object is separated from its representation, allowing the same build process to create different representations. Explanation:
An object (product) that may consist of several parts. For example, a car, mainly by the body, engine, tires, transmission system, and other components. These parts or systems are representations of objects. The builder model separates the construction process of the complex object from the representation of the vehicle. That means the customer just has to tell the builder what kind of car (a car or a big truck) I'd like to have. As for how cars are produced, customers don't care. The same build process, the building of the car is not the same (car or big truck). class Diagram:
Role: Product Class (Products): specific products, in general, is a process of creating more complex products.
Abstract Builder (Builder): An abstract class that defines the abstract method used to create a product.
Builder (ConcreteBuilder): Implements the class, implements the abstract builder class, and implements its abstract method.
Director Class (Director): Responsible for calling the specific builder class to complete the product creation process.
Code:
Product classes Products class Car {private string body;//body private string engine;//engine private string tire;//tires private String
drivesys;//Drive System//Omit Get Set Method}//Abstract builder Builder abstract class Builder {protected car = new car (); public abstract void Buildbody (string body);//build Bodywork public abstract void Buildengine (string engine);//Build engine public Abstra ct void Buildtire (string tire);//build tyre public abstract void Builcdrivesys (string drivesys);//Build drive System//get Auto public car
Getcar () {return car;
}//Car builder ConcreteBuilder class Carbuilder extends Builder {public void Buildbody () {car.setbody ("sedan body");
public void Buildengine () {Car.setengine ("Coupe engine");
public void Buildtire () {Car.settire ("Coupe Tyre");
public void Builcdrivesys () {Car.setdrivesys ("Coupe drive System");
}//Large truck builder ConcreteBuilder class Truckbuilder extends Builder {public void Buildbody () {car.setbody ("Large truck Body");
public void Buildengine () {car.setengine ("Big truck Engine");
public void Buildtire () { Car.settire ("Big truck Tires");
public void Builcdrivesys () {Car.setdrivesys ("Large Truck Drive system");
}//Director classes public class Director {private Builder Builder;
public void Setbuilder (Builder Builder) {this.builder = Builder;
Public car construct () {builder.buildbody ();
Builder.buildengine ();
Builder.buildtire ();
Builder.builcdrivesys ();
return Builder.getcar (); }//Client public class Client {public static void main (string[] args) {Director Director = new Director ();//director Bui
Lder Carbuilder = Carbuilder ();
Director.setbuilder (Carbuilder);
Car cars = director.construct ();//Coupe Builder Truckbuilder = Truckbuilder ();
Director.setbuilder (Truckbuilder); Car truck = director.construct ();//Big Truck}}
Benefits:Conforming to the dependency reversal principle, abstractions should not rely on detail, and details should be dependent on abstraction. The creation of the product relies on the abstract builder class, not on the specific builder class, and the product creation details are implemented in the builder class. Because the specific builder classes are independent of each other, they can be customized to implement different products without affecting other implemented functionality. Easy to scale, if the existing builders are unable to meet the requirements, just add a new builder implementation class without affecting other implemented functionality.
Usage Scenarios:Stable creation process, product object creation process is stable, namely abstract constructor class abstract method is stable, should not appear big change. The creation of product objects is extremely complex, and it is not appropriate to use Builder mode if the creation of product objects is too simple.