————— the next day —————

— — — — — —

First, let’s define a Product class:

public class Product { ArrayList<String> parts = new ArrayList<String>(); public void add(String part) { parts.add(part); } public void show() { System.out.println(parts); }}Copy the code

Next, we define the abstract Builder class:

public abstract class Builder {

public abstract void buildPartA();

public abstract void buildPartB();

public abstract Product getResult() ;

}

Then, there is the concrete Builder implementation class:

public class ConcreteBuilder extends Builder { private Product product = new Product(); public Product getResult() { return product; } @override public void buildPartA() {product.add(” build the top part of the product “); } @override public void buildPartB() {product.add(” build the lower part of the product “); }}

Outside of the Builder class, the Director class controls the Builder production process:

public class Director { private Builder builder; public Director(Builder builder) { this.builder = builder; } public void construct() { builder.buildPartA(); builder.buildPartB(); }}

Finally, the client test code:

Builder builder = new ConcreteBuilder();

Director director = new Director(builder);

director.construct();

Product product = builder.getResult();

product.show();

Let’s take a look at the results:

[Build the top half of the product, build the bottom half of the product]

Public OkHttpClient() {this(new Builder()); } // OkHttpClient(Builder Builder) {this.dispatcher = builder.dispatcher; this.proxy = builder.proxy; this.protocols = builder.protocols; /** omit a large section of code */ Boolean isTLS = false; for (ConnectionSpec spec : connectionSpecs) { isTLS = isTLS || spec.isTls(); */ interceptors. Contains (null)) {throw new IllegalStateException(” null interceptor: ” + interceptors); } if ( networkInterceptors.contains(null)) { throw new IllegalStateException(“Null network interceptor: ” + networkInterceptors); }} public static final Class Builder {public Builder() {dispatcher = new dispatcher (); protocols = DEFAULT_PROTOCOLS; proxySelector = ProxySelector.getDefault(); if (proxySelector == null) { proxySelector = new NullProxySelector(); }} Builder(OkHttpClient OkHttpClient) {this.dispatcher = okHttpClient.dispatcher; this.proxy = okHttpClient.proxy; this.protocols = okHttpClient.protocols; / / all new custom interceptors and custom web blocker enclosing interceptors. AddAll (okHttpClient. Interceptors); this .networkInterceptors.addAll(okHttpClient.networkInterceptors); } public Builder proxy(@nullable proxy) {this.proxy = proxy; return this; } public Builder addInterceptor(Interceptor Interceptor) {if (Interceptor == null) throw new IllegalArgumentException(“interceptor == null”); interceptors.add(interceptor); return this; Public OkHttpClient build() {return new OkHttpClient(this); }}

Override public StringBuilder appEnd (CharSequence s) {super.appEnd (s); // return this; } /** Override public StringBuilder reverse() {super.reverse(); // return this; }

Let’s write the test code below:

StringBuilder sb = new StringBuilder(” if “); Sb. Append (” as long as the truth “); sb.reverse(); System.out.println(sb); StringBuilder sb1 = new StringBuilder(” sb1 “); sb1.reverse(); Sb1. Append (” as long as the truth “); System.out.println(sb1);

The test results are as follows:

System.out: the true meaning is mainly caused by self if system.out: the true meaning is caused by self

The difference between Builder mode and simple engineering mode is that the Builder mode has an additional Builder class, which greatly increases the flexibility of creating objects. It is suitable for the following scenarios:

(1) To create an object, multiple same methods are called in different order, resulting in different results

(2) Create an object, which is very complex and has many parameters, many of which have default values

System.out.println(“Hello World”);

What’s the mechanism behind a single line of code?

The principle of Java compilation is to compile Hello. Java into a Hello.class that can be understood by VMS, and then convert it into a Bytecode that can be understood by different hardware devices.

ASM, a well-known bytecode enhancement framework, is a bytecode analysis and manipulation framework that can read and analyze class information, change class behavior, enhance class functions, and even generate new classes when hello. Java is compiled into Hello.class.

Let’s look at the code, and the MV in the code comes from the MethodVisitor interface of the ASM framework.

Out mv.visitFieldinsn (opcodes. GETSTATIC, “Java /lang/System”, “out”, “Ljava/io/PrintStream;” ); // Access the constant pool data “Hello World” mv.visitldcinsn (“Hello World”); // Call the println() method of the PrintStream class and pass the object as a String parameter to mv.visitmethodinsn (opcodes.invokevirtual, “Java/IO /PrintStream”, “println”, “(Ljava/lang/String;) V”, false);