JAVA Design Mode

Source: Internet
Author: User

JAVA Design Mode
JAVA Design ModeDesign Pattern Classification

JAVA design patterns can be divided into three categories based on creation patterns, structural patterns, and behavior patterns:

  • Creation Mode

    Singleton mode, factory mode, abstract factory mode,
    Builder mode and prototype mode

  • Structural Mode

    Adapter mode, Bridge Mode, decoration mode,
    Combination Mode, appearance mode, metadata mode, and proxy Mode

  • Behavior mode

    Template mode, command mode, iterator mode, observer mode,
    Intermediary mode, memorandum mode, interpreter mode, status mode,
    Rule mode, responsibility chain mode, and visitor Mode

Singleton Mode
Singleton mode ensures that a class has only one instance and provides a global access point to the instance. Common Singleton scenarios are as follows:
  • Windows Job Manager, recycle bin, and Operating System File System
  • Class for reading configuration files in the project
  • Website counters and application logs (this is generally because the shared log files are always open and can only be operated by one instance. Otherwise, it is difficult to append logs)
  • Database Connection Pool Design
  • Application Object in Servlet Programming
  • Each Servlet object in Servlet Programming
  • Each bean in Spring is a singleton by default. The advantage of doing so is that the spring container can manage

Because the singleton mode only generates one instance, the system performance overhead is reduced. When an object requires a large amount of resources, such as reading configurations and generating other dependent objects, you can solve this problem by directly generating a singleton object at application startup, and then permanently resident in the memory.

Common implementation methods of five Singleton modes:

  • Ele. Me (thread security, high call efficiency, but not delayed loading)
  • Lazy (thread security, low call efficiency, but can be loaded at a delay)
  • Double detection locking (occasional problems may occur due to the underlying JVM internal model, which is not recommended)
  • Static internal class mode (thread security, high call efficiency, but can be loaded with delay)
  • Enumeration Singleton (thread-safe, high call efficiency, but not delayed loading)

Hungry Chinese Style

Package singleton;/***** @ project: pattern * @ ClassName: SingletonDemo1 * @ Description: This class is... * @ author: cage * @ date: 21 Apr 2015 01:07:00 * @ modifier: cage * @ modifier time: 21 Apr 2015 01:07:00 * @ comments: singletonDemo1 in a single example @ version **/public class SingletonDemo1 {// This object is loaded immediately when the class is initialized (without the advantage of delayed loading). When the class is loaded, naturally, the thread-safe private static SingletonDemo1 instance = new SingletonDemo1 (); private SingletonDemo1 () {}// no thread synchronization is required, and the call efficiency is high. public static SingletonDemo1 getInstance () {return instance ;}}

Lazy

Package singleton;/***** @ project: pattern * @ ClassName: SingletonDemo2 * @ Description: This class is... * @ author: cage * @ date: 21 Apr 2015 01:06:14 * @ modifier: cage * @ modifier time: 21 Apr 2015 01:06:14 * @ comments: singletonDemo2 in a single example * @ version **/public class SingletonDemo2 {// when the class is initialized, this object is not initialized (delayed loading, created when actually used) private static SingletonDemo2 instance; private SingletonDemo2 () {}// method synchronization, low call efficiency public static synchronized SingletonDemo2 getInstance () {if (instance = null) {instance = new SingletonDemo2 ();} return instance ;}}

Static internal class (also a lazy loading method)

Package singleton;/***** @ project: pattern * @ ClassName: SingletonDemo3 * @ Description: This class is... * @ author: cage * @ date: 21 Apr 2015 23:49:44 * @ modifier: cage * @ modifier time: 21 Apr 2015 23:49:44 * @ comments: the static internal class implements the singleton mode, thread security, high call efficiency, and latency loading * @ version **/public class SingletonDemo3 {private static class SingletonClassInstance {private static final SingletonDemo3 instance = new SingletonDemo3 ();} private SingletonDemo3 () {} private static SingletonDemo3 getInstance () {return SingletonClassInstance. instance ;}}

If the external class does not have the static attribute, the object will not be loaded immediately as it is in the hungry Chinese style.
The static internal class is loaded only when getInstance () is called. When the class is loaded, it is thread-safe. The instance type is static final, ensuring that only such a case exists in the memory, it can only be assigned once, thus ensuring thread security and merging the advantages of concurrent and efficient calls and delayed loading.

Enumeration Method

Package singleton;/***** @ project: pattern * @ ClassName: SingletonDemo4 * @ Description: This class is... * @ author: cage * @ date: 22 Apr 2015 00:08:32 * @ modifier: cage * @ modifier time: 22 Apr 2015 00:08:32 * @ comments: Enumeration type implementation Singleton, no lazy loading of * @ version **/public enum SingletonDemo4 {// This enumeration element is itself a singleton object INSTANCE; public void singletonOperation (){}}

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.