10, Introduction of JAVA design pattern

Keywords: Java

1. Introduction to design mode

There are 23 design patterns in Java. The essence is the practical application of object-oriented design principles. It is a full understanding of class encapsulation, inheritance and polymorphism, as well as class association and composition.

Of course, the software design pattern is only a guide. In the actual software development, we must choose according to the specific needs.

1. For simple programs, it may be easier to write a simple algorithm than to introduce a design pattern.

2. However, for large-scale project development or framework design, it is obviously better to organize the code with design patterns.

1.1 single case design pattern concept

The singleton pattern can be said to be the most used by most developers in practice. The common bean created by Spring by default is the singleton pattern.

Singleton mode has many advantages, such as saving system memory space and controlling the use of resources.

The most important thing of singleton mode is to ensure that there is only one object.

Simply put, ensure that there is only one object of a class in memory.

RunTime is a typical singleton design. Let's take a look through the analysis of the RunTime class.

1.2 source code analysis

/**
 * Every Java application has a single instance of class
 * <code>Runtime</code> that allows the application to interface with
 * the environment in which the application is running. The current
 * runtime can be obtained from the <code>getRuntime</code> method.
 * <p>
 * An application cannot create its own instance of this class.
 *
 * @author  unascribed
 * @see    java.lang.Runtime#getRuntime()
 * @since   JDK1.0
 */
RunTime.java
package java.lang;
public class Runtime {
    //1. Create a static, globally unique object
    private static Runtime currentRuntime = new Runtime();

    //2. Private construction method,
    /** Don't let anyone else instantiate this class */
    private Runtime() {}

    //3. Get instances through custom static methods
    public static Runtime getRuntime() {
        return currentRuntime;
    }
}

1.3 hungry Han style

Objective: to control the number of external objects. Only one object can be created

Development steps:

1. Privatization construction method

2. Create the object inside the class

3. Provide a public get() to the outside world and return a prepared object

//Test singleton design pattern 
public class TestSingle {
    public static void main(String[] args) {
       Single s = Single.get();
       Single s1 = Single.get();
     
       //How many times does get() use the same object in memory
       System.out.println(s);//Single@15db9742
       System.out.println(s1);//Single@15db9742
    }
}

class Single{
//  1. Privatize the construction method and prevent the outside world from directly creating new
    private Single() {}
   
//  2. Inside the class, create the object
    //Static: static can only be called
    static private  Single s = new Single();
   
//  3. Provide a public get() to the outside world and return a prepared object

//static is used to access methods directly through class names instead of objects 
    static public Single get(){
       //Note: static can only be called
       return s;
    }
}

1.4 lazy

class Single{
//  1. Privatize the construction method and prevent the outside world from directly creating new
    private Single() {}
 
//  2. Inside the class, create the object
    //Static: static can only be called
    static private  Single s = null;
   
//  3. Provide a public get() to the outside world and return a prepared object
//static is used to access methods directly through class names instead of objects 
synchronized  static public Single get(){
       //Note: static can only be called
       if(s==null){
           s = new Single();//There will be security problems, so lock it
       }
       return s;
    }
}

2. Opening and closing principle of software design OCP

Open function extension, close source code modification. etc.

The full English name of the Open Close Principle is Open Close Principle, abbreviated as OCP. It is the most basic design principle in the Java world. It guides us how to build a stable and flexible system.

The definition of the opening and closing principle is that the objects (classes, modules, functions, etc.) in the software should be open for extension, but closed for modification.

The principle of opening and closing is a design pattern. With the idea of object-oriented programming, it came into being.

Open means that it can be extended on the basis of source code, such as inheritance, interface, abstract class, etc. In JAVA, the reason why inheritance is used is to extend its functions on the premise that the class library can be called directly. The application does not need to understand the internal logic of the encapsulated class.

Closed: the original code cannot be modified. So as not to affect other existing functions and cause functional paralysis.

This article is just an introduction. There are other design patterns and design principles that need to be studied

Posted by J@ystick_FI on Fri, 10 Sep 2021 02:42:34 -0700