Design pattern: An effective thought to solve the problem
Singleton design mode: Ensure that a class can have only one object in memory, such as multiple programs accessing a configuration file, want to multi-program operation is the same configuration file data, then need to guarantee the uniqueness of the profile object.
Idea: 1. What can be done to ensure that the object is unique? Other programs cannot control the number of objects when they are created by new, so no other program can create objects of that class because it cannot be controlled. 2, do not allow other programs to create, you should create an object in this class yourself. 3, this class will create the object provided externally, let other programs get and use.
Step: 1, how to implement do not let other programs create this class object? Privatize the constructors in this class.
For example: Class single
{
Private single ();
}
2. Create an object of this class in this class
such as class single
{
Private single () {};
Private Single S = new single ();
}
3, define a method, the return value is the class type. Let other programs use this method to get the class object. But this method must be public.
Like what:
Class single
{
Private single () {};
Private static single s = new single ();
Public static single getinstance ()
{
return s;
}
}
}
Example:
Package Com_package2; Public class Single { private single () {}; Private Static New Single (); Public Static Single Instance () { return s; }}
PackageCom_package2; Public classSingledemo {/** * @paramargs*/ Public Static voidMain (string[] args) {single S1 = single.instance (); //TODO auto-generated Method Stub }}
Package Com_package2; Public class Single { private single () {}; Private Static New Single (); Public Static Single Instance () { return s; } } //A hungry man type
PackageCom_package2; Public classSingle2 {PrivateSingle2 () {}; Private StaticSingle2 s2 =NULL; Public StaticSingle2 Instance () {if(s2==NULL) S2=NewSingle2 (); returnS2; }}//lazy, also known as a single-case lazy loading mode
Development of the general use of the first mode, a hungry man, because in the case of multi-threaded problems, lazy-type will lead to a series of security risks, but lazy-type is a single example of the important point of view
PackageCom_package2; Public classArraytool {
Private Arraytool () {};//This sentence will not allow other programs to create new objects. Public static int Max (int []arr] { intMax=0; for(inti=1;i<arr.length;i++) { if(arr[i]>Arr[max]) {Max=i; } Else Continue; } returnArr[max]; }Public static int Min (int []arr] { intMin=0; for(inti=0;i<arr.length;i++) { if(arr[i]<Arr[min]) {min=i; } Else Continue; } returnarr[min];}}
PackageCom_package2; Public classArraysdemo {/** * @paramargs*/ Public Static voidMain (string[] args) {int[] a={1,7,4,5,3,3,6}; intMaxs = Arraytool.max (a); intmins = Arraytool.min (a); System.out.println ("maxs=" + "" +maxs); System.out.println ("mins=" + "" +mins); //TODO auto-generated Method Stub }}
Maxs= 7
Mins= 1
In the class above, since none of the two methods have access to the unique variable, all can be static, so it can be called directly with the class name when referencing the methods in the class with the main function.
Single-instance design pattern (lazy, a hungry man type)