What are design patterns?

Design Patterns represent best practices and are often adopted by experienced object-oriented software developers. Design pattern is a solution to the common problems faced by software developers during software development.

Design pattern classification :(3 categories, 23 kinds)

Creative patterns: singleton, Abstract Factory, Prototype, Builder, Factory.

Structural mode: adapter mode, bridge mode, decorative mode, composite mode, appearance mode, share mode, proxy mode.

Behavioral patterns: template method pattern, command pattern, visitor pattern, iterator pattern, observer pattern, mediator pattern, memo pattern, interpreter pattern, state pattern, policy pattern, chain of responsibility pattern.

I. Singleton mode:


Take a method so that there is only one object instance of a class in the entire software system. And the class provides only one method to get an instance of its object (a static method).

Type of implementation:

1) Hungry and Hungry:

This is the easiest way to create a singleton class, but one drawback is that the client program may not use the instance that was created.

2) Static initialization:

The static block initialization implementation is similar to hanhan-mode initialization, but instances of classes are created in static code blocks and exceptions are handled.

3) Lazy loading:

Lazy loading means creating instances of the class when needed, rather than creating them in the first place.

4) Thread-safe singleton:

public class ThreadSafeSingleton { private static ThreadSafeSingleton instance = null; Private synchronized ThreadSafeSingleton(){} public static synchronized ThreadSafeSingleton getInstance(){ if(instance == null){ instance = new ThreadSafeSingleton(); } return instance; }}Copy the code

5) Enumerate singletons

This method is simple and convenient.

public enum  EnumSingleton {
    INSTANCE;

    public static void doSomething(){
        //do something
    }
}
Copy the code

Features:

1) A singleton class can have only one instance.

2) The singleton class must create its own unique instance.

3) The singleton class must provide this instance to all other objects.

Ii. Factory Mode:


Factory models include:

1) Simple factory model

Also known as the static factory method pattern, it belongs to the class-creation pattern. In the simple factory pattern, you can return instances of different classes depending on the parameters. The simple factory pattern specifically defines a class that is responsible for creating instances of other classes, which usually have a common parent class.

  • The work of creating instances is separated from the work of using instances, so that users do not have to care about how class objects are created, so as to achieve decoupling.
  • Putting the instantiation work in the factory makes the code easier to maintain. More object oriented & interface oriented programming rather than implementation oriented programming.

2) Factory method mode

The factory approach is to provide a factory class for each product and create different product instances from different factory instances. The main solution is to solve the problem that the simple factory model violates the “open – close principle”.

Advantages:

  • More in line with the open-closed principle, when a new product is added, only the corresponding specific product class and the corresponding factory subclass can be added
  • In accordance with the single responsibility principle, each specific factory class is only responsible for creating the corresponding product

3) Abstract factory pattern

The abstract factory pattern allows you to use an abstract interface to create a set of related products without knowing or caring about what the actual product is, so it can be decoupled from the concrete product.

Advantages:

  • Reduce coupling. Abstract factory mode delays the creation of concrete products to the subclass of concrete factories. In this way, the creation of objects is encapsulated, which can reduce the dependence between the client and the concrete product class, so that the coupling degree of the system is low, which is more conducive to the later maintenance and expansion.

  • It’s more open and closed. When you add a product class, you only need to add the corresponding concrete product class and the corresponding factory subclass.

Six principles of design patterns:

  • Single responsibility principle:

    The single responsibility principle represents the functional dependencies between the components of a module. In layman’s terms, a class has only one responsibility.

    Objective:

    1) Reduce class complexity

    2) Improve system maintainability

    3) Reduce risk overflow during modification

  • Richter’s substitution principle:

    All references to a base class must be able to transparently use its subclass objects, and subclasses cannot break the functionality of the parent class by extending its functionality.

  • Interface isolation rules:

    The dependency of one class on another should be based on the smallest interface.

  • Dependency inversion principle:

    Dependencies: Member variables, method parameters, and return values depend on abstractions, not concrete ones. A high-level module should not depend on a low-level module; both should depend on its abstraction.

  • Demeter principle:

    One object should have minimal knowledge of other objects.

  • Open and close principle:

    Open to extension, closed to modification, by extending existing software systems, can provide new functions, modification closed, to ensure stability and continuity.