Feel the code is awkward, turn back to the Java 23 design patterns.
Today, well, it's so late, sleepy. Let's get a simple singleton pattern.
Single-case mode: Commonly known as single dog
Package Singleton;
public class SingleTon {
Private static Final class Singletonbuilder {
private static SingleTon SingleTon = new SingleTon ();
}
Private SingleTon () {
}
public static SingleTon getinstance () {
return Singletonbuilder.singleton;
}
}
In fact, what kind of lazy, a hungry man, crazy mode, such as no tube, as long as this kind of is enough.
It's a pattern like Singleton. Three single call.
The Chinese explanation is monotonous, the popular saying is that only someone can use something.
Give a scene, Humanwang, Wangwife.
Lao Wang, and Lao Wang's wife.
Lao Wang's wife has a technology called MKL, but this technology can only be used by the old king,
First look at Lao Wang:
Package SingleCall;
public class Humanwang {
The slogan, is it the princess?
Private Boolean iswf = false;
Prevent Lao Wang's son and the like (subclass rewrite) impersonate
Public Final Boolean iswf () {
return iswf;
}
Get the Princess,
Private Wangwife Getwangwife () {
Wangwife Vivian;
iswf = true;//is the princess
Vivian = Wangwife.getwangwife (this);
iswf = false;//After getting the princess, the others are not.
return Vivian;
}
public static void Main (string[] args) {
Humanwang hw=new Humanwang ();
Wangwife Vivian =hw.getwangwife ();
VIVIAN.MKL ();
}
}
In the eyes of Lao Wang wife, commonly known as the princess:
Package SingleCall;
public class Wangwife {
The body is his own, can not be casually to show people
Private Wangwife () {
}
Of course, anyone can try to get the body of the princess, but the princess only to the Lao Wang a person, if someone with modified byte code way strong XX,
This can only call the police.
public static Wangwife Getwangwife (Humanwang hw) {
Wangwife Vivian;
There is no one
if (HW = = null) {
Vivian = null;
} else {
The correct slogan is incorrect
if (hw.iswf ()) {
Vivian = new Wangwife ();
} else {
Vivian = null;
}
}
return Vivian;
}
Princess of Technology.
public void Mkl () {
SYSTEM.OUT.PRINTLN ("Pa pa");
}
}
This is the only old king to obtain the princess, and then the princess to display technology.
PS PostScript:
Do not say my code to write dirty, we use the object-oriented thinking.
Java 23 Design patterns of the single dog and the Story of the Old king next door