[Java_ design pattern builder mode]

Scenarios
to generate object chain configuration
benefits: on-demand configuration object, flexibility to modify the default configuration, create objects more intuitive than the constructor.

static void the init public () {
IF (okHttpClient == null) {
OkHttpClient.Builder Builder = new new OkHttpClient.Builder ()
.connectTimeout (5000, TimeUnit.MILLISECONDS)
.connectTimeout (5000, TimeUnit.MILLISECONDS)
.writeTimeout (5000, TimeUnit .MILLISECONDS);
okHttpClient builder.build = ();
}
}
. 1
2
. 3
. 4
. 5
. 6
. 7
. 8
. 9
realization
1. Create audiences Course
2. create the target object internal category [builders]
3. the internal class has the Course member variable of the same name, and to provide a build () method returns the object Course, directly Constructs here.


// internal handling incoming application layer parameters
public static class CourseBuilder {
Private String CourseName;
Private String coursePPT;

// build () returns Course, as the last step in the chain calls the
public Course, Build () {
// need to create a constructor parameter of CourseBudiler
return new new Course, (the this);
}

//
public CourseBuilder buildCourseName(String courseName) {
this.courseName = courseName;
return this;
}

public CourseBuilder buildCoursePPT(String coursePPT) {
this.coursePPT = coursePPT;
return this;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// 应用层.build()后调用该构造方法
public Course(CourseBuilder courseBuilder) {
this.courseName = courseBuilder.courseName;
this.coursePPT = courseBuilder.coursePPT;
}
--------------------- 

Guess you like

Origin www.cnblogs.com/hyhy904/p/10935216.html