Java design pattern creational design pattern

Creation Design Patterns :

Abstract Factory :

  1. Purpose: Provides an interface to create a series of related or interdependent objects without specifying a concrete class.

  2. Scenes:

    1. A system is created independently of its product.
    2. A system is configured by one of several product families.
    3. Emphasizes the design of a series of related product objects for joint use.
    4. Provide a library of product classes, but only expose their interfaces rather than implementations
  3. Structure Diagram: Create a series of related objects

    image-20210530093622283

  4. Known applications: Collection, Map in JDK

  5. Factory Method :

    1. Purpose: Defines an interface for creating objects and lets subclasses decide which class to instantiate.

    2. Scenes:

      1. A class does not know the class of the objects it creates.
      2. A class expects its subclasses to specify the objects it creates.
      3. It is mostly used in the framework code, oriented to interface programming.
    3. Structure Diagram: Create an Object

      image-20210530093747954

    4. Known Applications:

      1. Blocking queue of ThreadPoolExecutor in JDK
      2. AbstractExecutorService.submit method, returns different Futures in ScheduledThreadPoolExecutor

Builder :

  1. Purpose: To separate the construction of a complex object from its representation, so that the same construction process can create different representations.

  2. Scenes:

    1. When an algorithm for creating a complex object should be independent of what that object is made of and how they are assembled.
    2. When the construction process must allow different representations of the constructed object.
  3. Structure diagram: defines a series of steps to create a class of products

    image-20210530100747814

  4. known application

    1. StringBuilder in JDK
    2. BeanDefinitionBuilder in Spring

singleton pattern

  1. Purpose: Relates to a single class that is responsible for creating its own objects while ensuring that only a single object is created. This class provides a way to access its only object, directly, without instantiating an object of the class.

  2. Scenes:

    1. A production unique serial number is required.
    2. The counter in the WEB does not need to be added to the database every time it is refreshed, but is cached first with a singleton.
    3. An object created needs to consume too many resources, such as I/O connection to the database.
  3. Structure diagram:

    UML diagram of singleton pattern

prototype mode

  1. Purpose: Implements a prototype interface that is used to create a clone of the current object. This mode is used when the cost of directly creating the object is relatively large

  2. Scenes:

    1. Resource optimization scenarios.
    2. Class initialization needs to digest a lot of resources, including data, hardware resources, etc.
    3. Scenarios with performance and security requirements.
    4. To generate an object through new requires very tedious data preparation or access rights, you can use the prototype pattern.
    5. Scenarios with multiple modifiers for one object.
  3. Note: Instead of constructing a new object by instantiating a class, the prototype pattern creates a new object by copying an existing object. Shallow copy implements Cloneable, rewrite, deep copy is to read binary stream by implementing Serializable.

  4. Structure diagram: clone() method

    UML diagram of the prototype pattern

Guess you like

Origin blog.csdn.net/a1774381324/article/details/120796853