Design pattern--Single case mode (i) lazy and a hungry man-type

Source: Internet
Author: User
Tags stub
This paper is the second article of design pattern learning notes, which is mainly about the single case mode. This includes lazy, a hungry man, registration, and lazy-type improvements, as well as
an instance of reading propertoes configuration files. It is expected to be divided into three sections. This is the first section, first analysis of the simplest of the lazy and a hungry man-style. The

single case pattern is a simpler one in the design pattern. For situations where a class has only one instance, such as a window manager, a print buffer pool, and a file system,
they are examples of prototypes. Typically, the types of those objects are accessed by different objects across a software system, so a global access pointer is required
, which is known as the application of a single case pattern. Of course it is only when you are sure that you no longer need any more than one instance. 
The single case pattern is intended to be of concern in the preceding paragraph. By using a single case pattern you can: first 


                     , to ensure that only one instance of a class is built 
                     two, provides a global access pointer to the object 
                     three, without affecting the singleton class of the case to allow more than one instance of the

Classic single case mode has three kinds, lazy type, a hungry man type and Registration type.

lazy features are delayed loading, such as configuration files, using the lazy method, as the name implies, lazy, very lazy, the configuration file instance
will not load until it is used ...

A hungry man is a feature of the load, if the lazy is "time for space", then the A hungry man is "space for Time", because the beginning of the creation of an instance, so each use after the direct return of the good.


Let's look at the code first:


Lazy Type:

Lazy single case mode public
class Mysingleton {

	//Set static variable
	private static Mysingleton Mysingleton = null;

	Private Mysingleton () {
		//privatisation constructor
		System.out.println ("--> lazy single case mode starts calling constructor");
	}
	
	Open a public method to determine whether an instance already exists, has a return, does not create a new one after returning to the publicly
	static Mysingleton getinstance () {
		System.out.println ("--> Lazy Singleton mode begins invoking the public method return instance ");
		if (Mysingleton = = null) {
			System.out.println ("The instance of the--> lazy constructor is not currently created");
			Mysingleton = new Mysingleton ();
		} else{
			System.out.println ("instance of--> lazy constructor has been created");
		}
		System.out.println ("--> method call end, return single example");
		Return Mysingleton
	}
}
Look at the client's test code:

public class Client {
	
	/**
	 * Lazy single case mode
	 * Mysingleton
	 /public
	static void Myprint () {
		System.out.println ("-----------------lazy single case Mode----------------");
		System.out.println ("First Instance (lazy type)");
		Mysingleton S1 = mysingleton.getinstance ();
		System.out.println ("Second instance (lazy type)");
		Mysingleton s2 = mysingleton.getinstance ();
		if (s1==s2) {
			System.out.println (">>>>>s1,s2 is the same instance (lazy) <<<<<");
		System.out.println ();
	}
        /**
	 * @param args
	 *
	/public static void main (string[] args) {
		//TODO auto-generated method stub
  
   //Lazy Type
		myprint ();
		A hungry man type
		//myprint2 ();
		Lazy type Improvement
		//MYPRINT2A ();
		Registration type
		//myprint3 ();

	}


  

The output results are:

-----------------Lazy single case mode----------------
First instance (lazy type)
--> Lazy Singleton mode start calling public method return instance
An instance of the--> lazy constructor is not currently created
--> Lazy single Case mode start call constructor
--> method call End, return single example
Second instance (lazy type)
--> Lazy Singleton mode start calling public method return instance
An instance of the--> lazy constructor has been created
--> method call End, return single example
>>>>>s1,s2 for the same instance (lazy) <<<<<


As you can see, there is no instance when the public method is invoked for the first time, so we create an instance and then visit it, because there is already a good instance already created, so we go straight back.



A Hungry man type:

A hungry man singleton mode public
class MySingleton2 {

	//Set static variable, directly create instance
	private static MySingleton2 Mysingleton = new MySingleton2 ();

	Private MySingleton2 () {
		//privatisation constructor
		System.out.println ("--> A Hungry man single-case mode starts calling constructor");
	
	Open a public method to determine whether an instance already exists, has a return, does not create a new one after returning to the publicly
	static MySingleton2 getinstance () {
		System.out.println ("--> A hungry man-style singleton mode begins invoking the public method return instance ");
		Return Mysingleton
	}
}

Look at the client's test code:

	/**
	 * A hungry man single case mode
	 * MySingleton2/
	 public
	static void Myprint2 () {
		System.out.println ("------------ -----A Hungry man type single case mode----------------");
		System.out.println ("First Instance (a Hungry man type)");
		MySingleton2 S1 = mysingleton2.getinstance ();
		System.out.println ("second instance (a Hungry man type)");
		MySingleton2 s2 = mysingleton2.getinstance ();
		if (s1==s2) {
			System.out.println (">>>>>s1,s2 is the same instance (a hungry man) <<<<<");
		System.out.println ();
	}
	/**
	 * @param args
	 *
	/public static void main (string[] args) {
		//TODO auto-generated method stub
  
   //Lazy Type
		//myprint ();
		A hungry man type
		myprint2 ();
		Lazy type Improvement
		//MYPRINT2A ();
		Registration type
		//myprint3 ();

	}
  


The output results are:

-----------------a hungry man mode----------------of single case
First Instance (a Hungry man type)
--> a hungry man-style single case mode to start calling constructors
--> a hungry man singleton mode to start calling the public method to return an instance
Second instance (a Hungry man type)
--> a hungry man singleton mode to start calling the public method to return an instance
>>>>>s1,s2 for the same instance (a Hungry man type) <<<<<


To sum up, both the constructors and common methods of the two schemes are static (static), and both the instance and the public method are private (private). However, the A hungry man type does not need to be created each time it is invoked, returning directly to the created instance. This saves time, but takes up space, and the instance itself is static and will always be in memory. The lazy type is to judge, in the use of time to load, will affect the speed of the program. Most crucially, in the case of concurrency, the lazy type is unsafe. If two threads, we call them thread 1 and thread 2, call the GetInstance () method at the same time, if thread 1 advanced into the If block and then thread 2 controls, then two instances are created.


RELATED links:

design Pattern--Single case mode (i) lazy and a hungry man-type design Pattern--Single case mode (ii) registration type Design Pattern--Single case mode (III.) Improved lazy type design mode--single case mode (iv) Single case mode instance configuration Properties



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.