Single case mode
The following is an example of using a single example pattern in Java
public class businessbll{
private static BUSINESSBLL BUSSINESSBLL = null;
private context;
Public Businessbll {
This.context = context;
}
public static BUSINESSBLL GETBUSINESSBLL {
if (BUSINESSBLL = = null) {
BUSINESSBLL = new BUSINESSBLL (context);
}
Return BUSINESSBLL
}
}
The following constructor is the key to a single example
public static BUSINESSBLL GETBUSINESSBLL {
if (BUSINESSBLL = = null) {
BUSINESSBLL = new BUSINESSBLL (context);
}
return BUSINESSBLL;
}
A single case pattern is implemented in C + + as follows
Simply put, the other part uses the business logic layer only by copying the original object
How to copy.
The cdalfile here is class, Pobjfile is the object of the class, as a parameter to the function init, in the INIT function to modify the object in the single case mode
This is a single example in C + +, which is a design pattern,
As far as the business logic layer is concerned, only the business logic Layer object can be instantiated once, simply by being a new one,
In single case mode
Single case mode code public class DD {public static int icount = 0;
Static class ABC {public static ABC m_a; The public static ABC Initabc () {//2. Custom function Initabc encapsulates the constructor ABC () so that the constructor can only be in the//init
The ABC () is invoked, and through the IF statement, the constructor ABC () can only be invoked once if (m_a = = null) {m_a = new ABC ();
return m_a; Private ABC ()//1. A null constructor with no parameters and private type, guaranteed not to call {}/* Test side arbitrarily
Method: Print M_a Value public boolean getm_a () {System.out.println (m_a);
return true;
} */}/** * @param args/public static void main (string[] args) {Abc.m_a = null;//constructor is private or protected, you cannot construct an object with the new method.Can be abc.initabc when public ();
ABC test1 = ABC.INITABC ()//When the Initabc method is invoked, the ABC () is skipped, and the M_a value is the value//test1.getm_a () at the time of the first construction.
ABC Test2;
Test2 = ABC.INITABC ()//When the Initabc method is invoked, the ABC () is skipped, and the M_a value is the value//test2.getm_a () at the time of the first construction.
System.out.println ("Nihao"); }
}