Simple explanation in Java programming to implement the single case pattern structure in the design pattern _java

Source: Internet
Author: User

1. Model Introduction

Definition of a pattern

Make sure that there is only one instance of a class, and instantiate it and provide this instance to the entire system.

Usage Scenarios for patterns

Make sure that a class has a scenario with only one object, such as too many resources to consume for creating an object, such as access to resources such as IO and databases.

2. UML Class Diagram

Role Description:
(1) Client: high-level clients.
(2) Singleton: Single Case class.

3. The simple realization of the pattern

public class Singleton {private static Singleton intance; Private Singleton () {} public static Singleton getinstance () {* * * First multithreading came in, encountered a lock, a thread went in, was empty, new object; subsequent line Cheng In, is not empty, does not operate; the last direct return * object is not empty, then more than one thread enters the function, is not empty, does not perform the lock operation, directly returns * * (intance = null) {synchronized (Sing 
        Leton.class) {if (intance = = null) {intance = new Singleton (); 
  }} return intance; } class Singleton1 {//lazy private static Singleton1 intance = new Singleton1 ();/lazy, when the program is running, it's loaded out private Si 
  Ngleton1 () {} public static Singleton1 getinstance () {return intance; 
  } class Singleton2 {//A hungry man private static Singleton2 intance; When private Singleton2 () {} public static Singleton2 getinstance () {//is used to load if (intance = = null) {INTA 
    nce = new Singleton2 (); 
  return intance; 
  } class Singleton3 {//A hungry man thread safety private static Singleton3 intance; Private SingletoN3 () {} public synchronized static Singleton3 getinstance () {//When used to load, lock multithreading calls, all have a lock action if (intance = = Nu 
    ll) {intance = new Singleton3 (); 
  return intance; 
  } class Singleton4 {//A hungry man thread safety private static Singleton4 intance; Only load synchronized (Singleton4.class) {//Singleton4 when Private Singleton4 () {} public static getinstance () {/ 
      /lock efficiency is similar to 3 if (intance = = null) {intance = new Singleton4 (); 
  } return intance; 
 } 
}

4. Advantages and Disadvantages

(1) Advantages:

A. The advantage of a singleton pattern is evident because the singleton pattern has only one instance in memory, reducing memory costs, especially when an object needs to be created and destroyed frequently, and when the performance is not optimized when created or destroyed.
B. Because singleton mode generates only one instance, therefore, the performance cost of the system is reduced, when the generation of an object needs more resources, such as reading configuration and generating other dependent objects, it can be solved by the way of permanent resident memory by the direct generation of a single Instance object when the application starts.
C. Singleton mode avoids multiple occupancy of resources, such as a write file action, which avoids simultaneous write operations on the same resource file because only one instance exists in memory.
D. A single case pattern can set global access points on the system, optimize and share resource access, for example, by designing a single instance class that is responsible for mapping processing for all data tables.

(2) Disadvantages
A. The single example pattern generally does not have the interface, the extension is very difficult, to extend, except modifies the code basically does not have the second way to realize.

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.