Java/Android design pattern
There are many design modes in Android, which can effectively improve the development efficiency, code efficiency and project innovation;
1
Singleton pattern: The most common design pattern in Java/Android:
private static volatile ApiClient mApiClient;
public static ApiClient getInstance(Context context) {
if (mApiClient == null) {
synchronized (ApiClient.class) {
mApiClient = newApiClient(context); }}return mApiClient;
}Copy the code
Note a few points: 1. Private constructs, other classes cannot new objects; 2. Static modifies singletons. 3. Volatile modifiers singleton: Each time a singleton is accessed, it must be read from shared memory. If the value of the member variable changes, it is forced to put the changed value into the shared memory to ensure the uniqueness of the value. 4. Synchronized We need to add synchronous execution when we create class instantiation methods, because it is possible for multiple programs to execute the class instantiation method at the same time if there is a large amount of concurrency in the project. Synchronized (singlet.class) is null because a thread enters the code, and if another thread is waiting, at that time, the previous thread creates an instance, and the synchronized (singlet.class) is not null. Another thread acquires the lock and enters the synchronization code. The instance already exists and there is no need to create it again, so null is still required.
2.
Builder model
public class Person {
private String name;
private int age;
private double height;
private double weight;
privatePerson(Builder builder) {
this.name=builder.name;
this.age=builder.age;
this.height=builder.height;
this.weight=builder.weight;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
public double getWeight() {
return weight;
}
public void setWeight(double weight) {
this.weight = weight;
}
static class Builder{
private String name;
private int age;
private double height;
private double weight;
public Builder name(String name){
this.name=name;
return this;
}
public Builder age(int age){
this.age=age;
return this;
}
public Builder height(double height){
this.height=height;
return this;
}
public Builder weight(double weight){
this.weight=weight;
return this;
}
public Person build() {return new Person(this); }}Copy the code
As can be seen from the above code, such as a bean class such as Person, which has a lot of parameters, if you selectively add or subtract parameters to create a bunch of constructs obviously not work, so create an internal class Builder inside person to use selective initialization parameters — > Builder provides after initialization Builder as a parameter creation Building the person object
Person.Builder builder=new Person.Builder(a);
Person person=builder
.name("Zhang")
.age(18)
.height(100)
.weight(100)
.build(a);Copy the code
The above code is already clear. Android development retrofit and Dialog all use builder mode; //eg:retrofit:
mRetrofit = new Retrofit.Builder(a).baseUrl(Constant.BASE// Set the baseurl parameter.client(mOkHttpClient)// Set the OkHttpClient instance.addConverterFactory(GsonConverterFactory.create() // Gson Conversion Plant.build(a);Copy the code