In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the knowledge of "how to understand the builder pattern of Java design pattern". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
I. what is the builder model
Builder pattern, also known as generator pattern definition: separates the construction of a complex object from its representation, so that the same construction process can create different representations (dependency inversion)
Product category: generally, it is a more complex object, that is, the process of creating an object is more complex, and there is generally a large amount of code. In this class diagram, the product class is a concrete class, not an abstract class. In practical programming, a product class can be composed of an abstract class and its different implementations, or multiple abstract classes and their implementations.
Abstract builder: the purpose of introducing an abstract builder is to hand over the concrete process of construction to its subclasses. This makes it easier to expand. There are generally at least two abstract methods, one to build the product and one to return the product.
Builder: implement all the unimplemented methods of an abstract class, specifically, two tasks: build the product; return the built product.
Command class: responsible for calling the appropriate builder to build the product, the command class generally does not depend on the product class, and the builder class interacts directly with the command class. In general, command classes are used to encapsulate volatile parts of the program.
Second, the application scenario of the builder model.
1. The algorithm for creating complex objects is independent of the components that make up the object.
two。 The same creation process requires product objects with different internal representations
Example: to build a house, no matter what house is built, it is inseparable from the foundation, columns, layers and walls. The builders build these components one by one, and finally connect them together to build a building.
Third, the advantages and disadvantages of the builder model
Advantages
1. The client does not need to know the details of the internal composition of the product, decoupling the product itself from the product creation process, so that the same creation process can create different product objects.
two。 Each specific builder is independent, so it is convenient to replace specific builders or add new specific builders, and users can get different product objects by using different specific builders.
3. The creation process of the product can be controlled more finely. Decomposing the creation steps of complex products into different methods makes the creation process clearer and more convenient to use the program to control the creation process.
4. Adding a new concrete builder does not need to modify the code of the original class library, and the director class is programmed for the abstract builder class, so the system expansion is convenient and conforms to "opening and closing".
Shortcoming
1. When there are too many builders, there are many classes that are difficult to maintain.
two。 The products created by the builder model generally have more in common, and their components are similar. if there are great differences between products, it is not suitable to use the model, so its scope of use is limited.
3. If the internal changes of the product are complex, it may lead to the need to define many specific builder classes to implement this change, resulting in a large system.
Fourth, the comparison between factory model and builder model.
The factory pattern is used to deal with the problem of how to get the instance object, and the builder schema is used to deal with the problem of how to build the instance object.
The builder model is very similar to the factory model. On the whole, the builder model only has one more "command" role than the factory model. In the class diagram of the builder pattern, if you think of this command class as the final calling client, then the rest can be seen as a simple factory pattern.
Compared with the factory pattern, the builder pattern is generally used to create more complex objects, because the object creation process is more complex, so the object creation process is separated to form a new class-command class.
In other words, the factory pattern encapsulates the whole creation process of the object in the factory class, and the factory class provides the final product to the client, while in the builder mode, the builder class generally only provides the construction of each component in the product class. and the specific construction process is delivered to the command class. The command class is responsible for assembling each component into a product according to specific rules, and then delivering the assembled product to the client.
Fifth, the realization of the builder model
Product class-product class, made up of multiple parts
/ / Product class, composed of multiple parts public class Product {List parts = new List (); / / add product parts public void Add (string part) {parts.Add (part);} public void show () {Console.WriteLine ("\ n create product"); foreach (var item in parts) {Console.WriteLine ("item");}
Builder class-abstract builder class, determines that the product consists of two components, PartA and PartB, and declares a method GetResult to get the results of the product after construction.
Abstract class Builder {public abstract void BuilderPartA (); public abstract void BuilderPartB (); public abstract Product GetResult ();}
Specific builder class
/ / ConcreteBuilder1 class-specific builder class class ConcreteBuilder1: Builder {private Product product = new Product (); / / build two specific parts public override void BuilderPartA () {product.Add ("part A");} public override void BuilderPartB () {product.Add ("part B");} public override Product GetResult () {return product }} / / ConcreteBuilder2 class-specific builder class class ConcreteBuilder2: Builder {private Product product = new Product (); / / build two specific parts public override void BuilderPartA () {product.Add ("part X");} public override void BuilderPartB () {product.Add ("part Y");} public override Product GetResult () {return product;}}
Director class-commander class
Class Director {public void Construct (Builder builder) {/ / is used to direct the construction process builder.BuilderPartA (); builder.BuilderPartB ();}}
Client code-the customer does not need to know the specific construction process
Static void Main (string [] args) {Director director = new Director (); Builder b1 = new ConcreteBuilder1 (); Builder b2 = new ConcreteBuilder2 (); / / the commander builds the product director.Construct (b1) using the ConcreteBuilder1 method; Product p1 = b1.GetResult (); p1.show (); / / the commander builds the product director.Construct (b2) using the ConcreteBuilder2 method; Product p2 = b2.GetResult (); p2.show () Console.Read ();} "how to understand the builder pattern of the Java design pattern" ends here. Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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
© 2024 shulou.com SLNews company. All rights reserved.