2021-09-23 software design mode ^ single example mode

Keywords: Java

1, Software design pattern

1. Concept

Software Design pattern, also known as Design pattern, is a set of repeatedly used, known by most people, and summarized through classification, cataloging and code design experience.

2. Function

Design patterns are used to reuse code, make the code easier to be understood by others, and ensure code reliability and program reusability.

java has 23 design patterns.

2, Singleton mode

Singleton pattern is one of the simplest design patterns in java. It belongs to creation mode. It provides the best way to create objects.

  Singleton mode should ensure that a class has only one instance and provide a way that can be accessed globally.

1. Benefits:

It can save memory and time.

2. Development steps:  

1. Privatize the construction method, or the outside world can use the new object.  

2. Create an object inside the class yourself.

  3. Provide public access methods and return self created objects.

3. Common implementation methods:

1. Lazy style

2. Hungry Han style


1. Thread safety:

Hungry Chinese style is thread safe. When the class is created, a static object has been created for the system to use without changing.

Lazy if synchronized is not added when creating an instance object, access to the object will not be thread safe.

2. Instantiation time:

A lazy man instantiates an object when calling a method, and a hungry man instantiates it when loading a class.

3, Implementation of singleton mode

1. Hungry Han style:

Whether you need to create it or not, it is easy to use.

class HungryBoy{
    //1. Privatization construction method
    private HungryBoy(){
        System.out.println("Private hungry Han type nonparametric structure~");
    //2. Create your own objects
    //The return value of a static method also needs to be static, so static can be called with the return value
    //If it is public static, the outside world can access it without using methods, so add static
    private static HungryBoy hungryBoy = new HungryBoy();
    //3. Provide an exposed method to return the object.
    //This class cannot call methods by creating objects, so add static and call directly with the class name.
    //When a method is called by multiple threads at the same time, thread safety problems may occur and locking is required.
    public static synchronized HungryBoy getHungryBoy(){
        System.out.println("Through public get Method obtained!");
        return hungryBoy;

2. Lazy:

On demand load / deferred load, created only when needed.

class LazyBoy{
    //1. Privatization construction method
    private LazyBoy(){
        System.out.println("Private lazy nonparametric construction~");
    //2. State first:
    private static LazyBoy lazyBoy;
    //3. Provides an exposed method that creates an object return when the method is called
    //When a method is called by multiple threads at the same time, thread safety problems may occur and locking is required.
    public static synchronized LazyBoy getLazyBoy(){
        if (lazyBoy == null) {//Determine whether it has been created
            lazyBoy = new LazyBoy();//The new object was not created before
        return lazyBoy;//Created direct return object

3. Static hungry Han style

Using static code blocks, this method is similar to the first one, and it is also a hungry man mode.

class StaticHungryBoy {
    private StaticHungryBoy(){
        System.out.println("Private static nonparametric construction~");
    private static StaticHungryBoy staticBoy;
        staticBoy = new StaticHungryBoy();
    public static StaticHungryBoy getStaticBoy(){
        System.out.println("Through public get Method obtained!");
        return staticBoy;

4. Static lazy

Using inner classes, similar to the second, is a lazy pattern.

class StaticLazyBoy{
    private StaticLazyBoy(){
        System.out.println("Private lazy static parameterless construction~");
    private static class StaticHungryBoyInnr{
        static StaticLazyBoy staticLazyBoy = new StaticLazyBoy();
    public static StaticLazyBoy getStaticLazyBoy(){
        System.out.println("Through public get Method obtained!");
        return StaticHungryBoyInnr.staticLazyBoy;

It should be noted that:

Using the internal class method, there is no need for explicit synchronous locking operation. Only one instance object can be guaranteed through the special feature that the static members of the class are loaded only once. However, this method is not hungry, but lazy, because when a class is loaded, its internal classes will not be loaded at the same time.

A class is loaded when and only when one of its static members or constructors is called. It can be said that this method is the optimal solution of single case mode. (the code is executed in sequence. Execute the code in the method first, and return to the created object when return. Go to the static internal class.)


Posted by ivki on Thu, 23 Sep 2021 05:07:53 -0700