Singleton _ case study and explanation of Single-Column Mode

Source: Internet
Author: User
Tags throwable

Today, I will talk about the importance and how to understand the design pattern as the technical personnel of our development and design. To be precise, the design pattern is a conceptual act, it is an abstraction of thing and entity. It involves not only a specific domain, but also a design behavior for the existence of the entire ecosystem, so here I can only explain the actual situations I need in the programming process and give examples for analysis, in my previous development process, I often ignored the existence of the design model. However, today I have to pay attention to all its possible charm, because it is very simple, it makes it easier for me to maintain and expand the management of the programs we designed with more regularity and logic, today is my first time writing design patterns on my blog. Besides, I did some J2EE work in the early stage of the android project, so here I will explain in detail the most common single-column mode. However, before I explain it, I must first know the basic concepts of the Single-Column mode: that is, a class has only one instance and provides a global entry point for access to the corresponding instance. OK. I will paste the code I tested directly below:

Package com. JSD. pattern;
/**
* Single-column mode explanation
* @ Author jiangshide
*
*/
Public class Singleton {

Public static void main (string [] ARGs ){
System. Out. println ("---------------- array Bubble Sorting in the early design mode of a single column --------------");
String STR = "jiangshide ";
// Sort binary search first
Int arr [] = new int [] {332,234 };
Int Index = 9;
// Test singletonbeforeinstanctiated
Singletonbeforeinstantiated SBI = singletonbeforeinstantiated. getinstance ();
Int [] result = SBI. singletonbeforeinstantiatedmethod1 (STR, arr );
System. Out. println ("Before sorting Arrays :");
For (INT x = 0; x <arr. length; X ++ ){
System. Out. Print (ARR [x] + "/t ");
}
System. Out. println ();
System. Out. println ("after sorting the array :");
For (INT I = 0; I <result. length; I ++ ){
System. Out. println (I + ":" + result [I] + "/t ");
}
System. Out. println --------------");

System. Out. println ("---------------- single-column direct lock design mode to achieve binary lookup --------------");
Singletondirectlyunlock SDU = singletondirectlyunlock. getinstance ();
Int result_index = 0;
If (SDU! = NULL ){
Result_index = SDU. getindex (STR, result, index );
System. Out. println ("the number to be searched is:" + index + ", and its array subscript is:" + result_index );
} Else {
System. Out. println ("instantiation is null! ");
}
System. Out. println ("------------- the above implementation finds ---------------");

System. Out. println ("------------------ Single Column double lock design mode to implement any number countdown ---------------------------------");
Singletondoubledirectlyunlock sddu = singletondoubledirectlyunlock. getinstance ();
If (sddu! = NULL ){
Sddu. getnumber (STR, result_index );
}
}
}

/**
* Single Column mode: Early instantiation ~ That is, the hungry Chinese Model
*/
Class singletonbeforeinstantiated {
// Class self-static private instantiation
Private Static singletonbeforeinstantiated instance = new singletonbeforeinstantiated ();
// Private the constructor
Private singletonbeforeinstantiated (){
System. Out. println ("singletonbeforeinstantiated:" + singletonbeforeinstantiated. Class );
}
// Set a public access interface for this class as the method for obtaining the instantiation of the net static state.
Public static singletonbeforeinstantiated getinstance (){
System. Out. println ("Single Column pattern _ early instantiation design: Get access entry point :");
Return instance;
}
/**
* A method to be accessed in the single-column mode: Process sorting
*/
Int [] singletonbeforeinstantiatedmethod1 (string STR, int [] ARR ){
If (STR! = NULL ){
System. Out. println ("Sort flag character:" + Str );
For (INT I = 0; I <arr. length; I ++ ){
For (Int J = arr. Length-1; j> I; j --){
If (ARR [J-1]> arr [J]) {
Int temp = arr [J-1];
Arr [J-1] = arr [J];
Arr [J] = temp;
}
}
}
Return arr;
}
Return NULL;
}
}

/**
* Single-column mode: Direct lock design ~ Full mode: note that synchronization is not performed at the if judgment. Therefore, multiple instances may be generated in a multi-threaded environment.

* If synchronization control is added, the get instance must be synchronized every time. This has a serious impact on the performance and is not recommended for use.
*/
Class singletondirectlyunlock {
Private Static singletondirectlyunlock instance = NULL;
Private singletondirectlyunlock (){
System. Out. println ("singletondirectlyunlock:" + singletondirectlyunlock. Class );
}
Public static synchronized singletondirectlyunlock getinstance (){
System. Out. println ("Single Column mode _ direct lock instantiation design: Get access entry point :");
If (instance = NULL ){
Instance = new singletondirectlyunlock ();
}
Return instance;
}
/**
* Method to access a single-column mode: Use the binary lookup Method
* @ Return
*/
Public int getindex (string STR, int [] arr, int index ){
If (STR! = NULL ){
System. Out. println ("binary search identifier:" + Str );
Int low = 0;
Int Height = arr. length;
Int meddile;
While (low <= height ){
Meddile = (low + height)/2;
If (Index = arr [meddile]) {
Return meddile;
}
If (index> arr [meddile]) {
Low = meddile + 1;
}
If (index <arr [meddile]) {
Height = meddile-1;
}
}
}
Return-1;
}
 
// Exception Handling
Private throwable getthrows (){
Return new throwable ("Single-Column instances are null! ");
}
}

/**
* Single-column mode: dual lock mode: This solves the problems that may exist in the previous two cases. After jdk1.5, the dual lock check is effective.
*/
Class singletondoubledirectlyunlock {
Private Static singletondoubledirectlyunlock instance = NULL;
Private singletondoubledirectlyunlock (){
System. Out. println ("singletondirectlyunlock:" + singletondirectlyunlock. Class );
}
Public static singletondoubledirectlyunlock getinstance (){
If (instance = NULL ){
Synchronized (singletondoubledirectlyunlock. Class ){

If (instance = NULL ){
Instance = new singletondoubledirectlyunlock ();

}
}
}
Return instance;
}
/**
* Method to access a single-column mode: implement any number countdown display Effect
*/
Public int getnumber (string STR, int number ){
If (STR! = NULL ){
Boolean flag = true;
System. Out. println ("Countdown to implement the search flag:" + Str );
System. Out. Print ("the remaining number is :");
JSD: While (FLAG ){
Number --;
Try {
Thread. Sleep (1000 );
System. Out. Print (number );
If (number <1 ){
Break JSD;
}
} Catch (interruptedexception e ){
E. printstacktrace ();
}
}
Return number;
}
Return new INTEGER ("the flag is blank! ");
}
}

 

The following is the UML diagram:

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.