Design mode builder mode (difference from factory mode and application of StringBuilder class in JDK)


Some conclusions about the construction mode are also expected to be pointed out.



The Builder Pattern uses multiple simple objects to build a complex object step by step . This type of design pattern is a creational pattern, which provides an optimal way to create objects.

Intent: Separate a complex construction from its representation so that the same construction process can create different representations.
Main solution: Mainly solve the problem of creating a "complex object" in the software system, which is usually composed of sub-objects of each part with a certain algorithm; due to changes in demand, each part of this complex object is often faced There are dramatic changes, but the algorithm that combines them is relatively stable.
When to use: When some basic components will not change, and their combination often changes.
How to solve it: Separate change from change.
Key code: builder: create and provide examples, director: manage the dependencies of the built examples.
Application examples:  1. Go to KFC, burgers, cola, french fries, fried chicken wings, etc. are unchanged, and the combination is constantly changing, resulting in the so-called "set meal". 2. StringBuilder in JAVA.
Advantages:  1. The builder is independent and easy to expand. 2. It is easy to control detailed risks.
Disadvantages:  1. Products must have common ground and limited scope. 2. If the internal changes are complex, there will be many construction classes.
Use scenario:  1. The object to be generated has a complex internal structure. 2. The internal attributes of the objects that need to be generated depend on each other.
Note: The difference from the factory model is that the builder model pays more attention to the order of assembly with parts.
The builder model usually includes the following roles: 

1.  builder (abstract builder) : Give an abstract conclusion to standardize the construction of the various components of the product object. This interface specifies the creation of those parts of complex objects, and does not involve the creation of specific object parts. 
2.  ConcreteBuilder (concrete builder) : implements the Builder interface, for the creation of various parts of complex objects for different business logics. After the construction process is complete, provide an example of the product. 
3.  Director (director) : call the specific builder to create the various parts of the complex object, the director does not involve the information of the specific product, only responsible for ensuring that the parts of the object are created completely or in a certain order. 
4.  Product (product category) : complex objects to be created.

The difference between Builder mode and AbstraceFactory abstract factory mode: 
The Builder builder pattern is very similar to the AbstraceFactory abstract factory pattern. Many people are often unclear. The differences are as follows: 
(1). In the abstract factory pattern, each time a factory object is called, a complete product object is returned, and the user end may decide to assemble these products into a larger and complex product, or may not . Factory items are stateless, I do n’t know which product was built last time, and I do n’t have a future concept, I do n’t know which product to build next time, and I do n’t know that the product I built is in the blueprint of higher-level product structure What position is it. 
(2). The builder mode is different, and the focus of the build mode is on the role of Director. The mentor is stateful. It knows the overall blueprint and knows what parts were given to the builder role last time, this time and next time, so that these parts can be assembled into a larger product. It builds a complex product little by little, and the assembly process of this product takes place inside the role of mentor. The user side of the builder model gets a complete final product. 
In other words, although both the abstract factory pattern and the construction pattern are design patterns, the abstract factory pattern is on a more specific scale, while the construction pattern is on a more macro scale. A system can be composed of a construction mode and an abstract factory mode. The user end calls the factory role of another abstract factory mode indirectly by calling the director role. The factory style returns parts from different product families, while the builder model assembles them. 

Application of builder mode in JDK: 
The append () method of StringBuilder and StringBuffer uses builder mode. 
StringBuilder hands the role of builder to its parent AbstractStringBuilder

Code:
public final class StringBuilder 
   extends AbstractStringBuilder   
   implements java.io.Serializable, CharSequence{
}


Take the append method as an example, the final call is append () of the parent class:
Source code:
public StringBuilder append(String str) {       
 super.append(str);      
  return this;  
  }

public AbstractStringBuilder append(String str) {       
  if (str == null) str = "null";      
  int len = str.length();    
  ensureCapacityInternal(count + len);      
  str.getChars(0, len, value, count);       
  count += len;       
  return this;//返回构建对象   
 }





Published 26 original articles · praised 0 · visits 9934

Guess you like

Origin blog.csdn.net/weixin_38246518/article/details/78756615