Java Design Patterns

Source: Internet
Author: User

java Design PatternsDesign Pattern Classification

The Java design pattern can be divided into three categories according to the creation pattern, the structure pattern and the behavioral pattern:

    • Create pattern

      Single-instance mode, Factory mode, abstract Factory mode,
      Builder mode, prototype mode

    • Structural mode

      Adapter mode, bridge mode, trim mode,
      Combination mode, appearance mode, enjoy meta mode, proxy mode

    • Behavioral patterns

      Stencil mode, command mode, iterator mode, observer mode,
      Mediator mode, Memo mode, interpreter mode, state mode,
      Policy mode, responsibility chain mode, visitor mode

Single-Case mode
单例模式其核心作用就是保证一个类只有一个实例,并且提供一个访问该实例的全局访问点。常见的单例模式应用场景如下:
    • File systems for Windows Task Manager, Recycle Bin, and operating system
    • Classes that read configuration files in a project
    • Web site counters and application logs (this is generally due to the shared log file is always open, only one instance to operate, no bad append)
    • Design of database connection pool
    • Application objects in servlet programming
    • Each Servlet object in the Servlet programming
    • Each bean in spring is a singleton by default, and the advantage of this is that the spring container can manage

Because the singleton pattern generates only one instance, reducing the overhead of system performance, when an object is produced that requires more resources, such as reading a configuration and generating other dependent objects, it can be resolved by producing a singleton object directly at application startup and then permanently residing in memory.

Common 5 Types of singleton mode implementations:

    • A Hungry man (thread-safe, high-efficiency, but cannot delay loading)
    • Lazy (thread-safe, call efficiency is not high, but can be delayed loading)
    • Dual detection lock (occasionally problematic due to JVM underlying internal model cause, not recommended)
    • Static internal class mode (thread safe, call efficient, but can delay load)
    • Enumeration Singleton (thread safe, call efficient, but cannot delay loading)

A Hungry man type

 PackageSingleton/** * * @project: Pattern * @ClassName: SingletonDemo1 * @Description: This class is ... *   @author : Cage * @date: Apr 01:07:00 * @modifier: Cage * @modifier time:21 APR 01:07:00 * @comments: Single Case A hungry man * @version * */ Public  class SingletonDemo1 {    //class initialization, load this object immediately (without the advantage of delayed loading), when loading classes, natural is thread-safe    Private StaticSingletonDemo1 instance=NewSingletonDemo1 ();Private SingletonDemo1(){    }//No thread synchronization required, high call efficiency     Public StaticSingletonDemo1getinstance(){returnInstance }}

Lazy type

 PackageSingleton/** * * @project: Pattern * @ClassName: SingletonDemo2 * @Description: This class is ... *   @author : Cage * @date: Apr 01:06:14 * @modifier: Cage * @modifier time:21 APR 01:06:14 * @comments: one-off lazy * @version * */ Public  class SingletonDemo2 {    //class initialization, do not initialize this object (delay loading, the real time to create again)    Private StaticSingletonDemo2 instance;Private SingletonDemo2(){}//method synchronization, low call efficiency     Public Static synchronizedSingletonDemo2getinstance(){if(instance==NULL) {instance=NewSingletonDemo2 (); }returnInstance }}

Java Design Patterns

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.