Java 23 Design patterns of the single dog and the Story of the Old king next door

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.