In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "how to use the builder pattern in the design pattern series". The content in the article is simple and clear, and it is easy to learn and understand. let's study and learn how to use the builder pattern in the design pattern series.
1. Overview
The Builder pattern is a creative design pattern that allows you to create complex objects step by step. This mode allows you to use the same creation code to generate different types and forms of objects.
2. Applicable scenarios
1) avoid overlapping constructors, for example, a class has a lot of attributes, and the constructor needs to pass a lot of parameters. In order to avoid this, a constructor with relatively few parameters will be rewritten. However, other parameters still need to be assigned default values.
2) when you need to create different product types, in this case, similar product types, you can include the assignment methods of most product attributes in the top-level builder.
3. Examples
There are the following scenarios, which are implemented in regular mode and builder mode, respectively:
There is currently a car factory that can produce ordinary,sport cars. In addition to cars, it is also possible to produce an operation manual manual for cars. The automobile has the following attributes: 1, type type 2, seats seat number 3, engine engine 4, GPS navigation to realize the production process of the car and the manual respectively.
Do not use builder mode
Create the car car and manual manual, respectively, as well as their internal properties, which are the same in the current example.
Data public class Car {private CarType type; private int seats; private Engine engine; private GPS GPS; public Car (CarType type, int seats, Engine engine, com.cloud.bssp.designpatterns.builder.withouttdesign.entity.GPS GPS) {this.type = type; this.seats = seats; this.engine = engine; this.GPS = GPS } public void detail () {System.out.println ("this is" + type + "car"); System.out.println ("the seats is:" + seats); System.out.println ("the engine is:" + engine); System.out.println ("this GPS is:" + GPS);}} @ Data public class Manual {private CarType type; private int seats; private Engine engine Private GPS GPS; public Manual (CarType type, int seats, Engine engine, com.cloud.bssp.designpatterns.builder.withouttdesign.entity.GPS GPS) {this.type = type; this.seats = seats; this.engine = engine; this.GPS = GPS;} public void detail () {System.out.println ("this is" + type + "car") System.out.println ("the seats is:" + seats); System.out.println ("the engine is:" + engine); System.out.println ("this GPS is:" + GPS);} public enum CarType {SPORT,ORDINARY;} / * car engine * / @ Data public class Engine {/ * emissions * / private final double volume / * * mileage * / private double mileage; public Engine (double volume, double mileage) {this.volume = volume; this.mileage = mileage;}} @ Data public class GPS {/ * Route * / private String route; public GPS (String route) {this.route = route;}}
Test class:
/ * * client * / @ RunWith (SpringRunner.class) @ SpringBootTest (classes = TestApplication.class) public class TestDemo {@ Test public void test () {/ / production sports car Car sportCar = new Car (CarType.SPORT, 2, new Engine (3.0,0) New GPS ("Shanghai Oriental Pearl Tower to Shanghai Zoo") SportCar.detail (); System.out.println ("-") / / production of ordinary cars Car ordinaryCar = new Car (CarType.ORDINARY, 4, new Engine (2.0,0), new GPS ("Shanghai Oriental Pearl Tower to Shanghai Zoo"); ordinaryCar.detail () System.out.println ("-") / / production car operation manual Manual manual = new Manual (CarType.ORDINARY, 4, new Engine (2.0,0), new GPS ("Shanghai Oriental Pearl Tower to Shanghai Zoo"); manual.detail () System.out.println ("-");}
Results:
This is SPORT car the seats is: 2 the engine is: Engine (volume=3.0, mileage=0.0) this GPS is: GPS (route= Shanghai Oriental Pearl Tower to Shanghai Zoo)-- this is ORDINARY car the seats is: 4 the engine is: Engine (volume=2.0 Mileage=0.0) this GPS is: GPS (route= Shanghai Oriental Pearl Tower to Shanghai Zoo)-- this is ORDINARY car the seats is: 4 the engine is: Engine (volume=2.0 Mileage=0.0) this GPS is: GPS (route= Shanghai Oriental Pearl Tower to Shanghai Zoo)--
Using the builder pattern to implement
After using the builder pattern, there is a lot more code than the above method:
Create a top-level Builder
Public interface Builder {void setCarType (CarType carType); void setSeats (int seats); void setEngine (Engine engine); void setGPS (GPS gps);}
Create the entity class, which is the same as above, which will not be repeated here.
Create a builder for car:
@ Data public class CarBuilder implements Builder {private CarType carType; private int seats; private Engine engine; private GPS GPS; public Car getResult () {return new Car (carType, seats, engine, GPS);}}
Create a car operation manual builder:
@ Data public class ManualBuilder implements Builder {private CarType carType; private int seats; private Engine engine; private GPS GPS; public Manual getResult () {return new Manual (carType, seats, engine, GPS);}}
Create a builder manager:
Public class Director {public void constructSportsCar (Builder builder) {builder.setCarType (CarType.SPORT); builder.setSeats (2); builder.setEngine (new Engine (3.0,0)); builder.setGPS (new GPS ("Shanghai Oriental Pearl Tower to Shanghai Zoo");} public void constructOrdinaryCar (Builder builder) {builder.setCarType (CarType.ORDINARY); builder.setSeats (4) Builder.setEngine (new Engine (2.0,0); builder.setGPS (new GPS ("Shanghai Oriental Pearl Tower to Shanghai Zoo");}}
Test class:
@ RunWith (SpringRunner.class) @ SpringBootTest (classes = TestApplication.class) public class TestDemo {@ Test public void test () {Director director = new Director (); / / production sports car CarBuilder carBuilder = new CarBuilder (); director.constructSportsCar (carBuilder); Car sportCar = carBuilder.getResult (); sportCar.detail () System.out.println ("- -"); / / production of ordinary car director.constructOrdinaryCar (carBuilder); Car ordinaryCar = carBuilder.getResult (); ordinaryCar.detail () System.out.println ("- -"); / / production vehicle operation manual ManualBuilder manualBuilder = new ManualBuilder (); director.constructOrdinaryCar (manualBuilder); Manual manual = manualBuilder.getResult (); manual.detail () System.out.println ("-");}
Results:
This is SPORT car the seats is: 2 the engine is: Engine (volume=3.0, mileage=0.0) this GPS is: GPS (route= Shanghai Oriental Pearl Tower to Shanghai Zoo)-- this is ORDINARY car the seats is: 4 the engine is: Engine (volume=2.0 Mileage=0.0) this GPS is: GPS (route= Shanghai Oriental Pearl Tower to Shanghai Zoo)-- this Manual ORDINARY car the Manual seats is: 4 the Manual engine is: Engine (volume=2.0 Mileage=0.0) this GManual PS is: GPS (route= Shanghai Oriental Pearl Tower to Shanghai Zoo)--
4. Analysis
The builder model basically has the following roles:
As the results of the above two ways show, it is possible to produce cars and car manuals.
There was no difference in the result.
In a way that does not use the builder:
The parameters of the production car are specified by the client itself, and need to pass a lot of parameters, the actual work may need more parameters, some of the parameters may not be needed.
Use the builder mode
Users do not need to specify multiple parameters, which is more friendly to the client.
Builder: put the product new () into the builder and provide the property setting method owned by the product, which can be used for product creation by a class of products.
Director: as the manager of builder, you mainly control the property settings of the product. In this class, you specify the construction of the product that can be produced, assign values to the properties, and finally return a builder that the user needs.
The client call only needs to create the required product type builder, sets the properties of the builder through the manager director, and finally the client gets the final product by calling the method of builder.
Greatly reduce and optimize the client-side code, while the manager director limits the variety of products.
From the expansion layer level:
Those who do not use the builder: add the corresponding product category, and the client new directly.
Use builder mode: add builder and add builder constructs for creatable products in director.
5. Summary
Finally, summarize the advantages and disadvantages of using the abstract factory approach in the above example:
Advantages:
1) abide by the single principle.
2) the same product creation process can be reused for different products.
3) simplify the client call mode. A way to remove multi-parameter structures.
4) create objects step by step.
Disadvantages:
With the addition of multiple classes, the code complexity increases.
Thank you for your reading. the above is the content of "how to use the builder pattern in the design pattern series". After the study of this article, I believe you have a deeper understanding of the problem of how to use the builder pattern in the design pattern series. the specific use also needs to be verified by practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
Use the Description property to get a short error description
© 2024 shulou.com SLNews company. All rights reserved.