Common design principles

Software development process

  • Requirements analysis documents, outline design documents, detailed design documents, coding and testing, installation and debugging, maintenance and upgrading

Common design principles

  • The Open Close Principle is Open for extension and closed for modification, in order to make the program extensible, easy to maintain and upgrade.
  • Anywhere a base class can appear, subclasses must, use polymorphism.
  • The Dependence Inversion Principle relies more on abstract classes or interfaces than concrete implementation classes, and is mandatory and normative for subclasses
  • Interface Segregation Principle uses small interfaces rather than large interfaces as much as possible to avoid Interface pollution and reduce coupling degree between classes.
  • The Demeter Principle is that an entity should interact with other entities as little as possible, so that the functional modules of the system are relatively independent. High cohesion, low coupling.
  • The Composite Reuse Principle uses Composite/aggregate rather than inheritance as much as possible.

Common design patterns

The basic concept

  • Design pattern is a set of repeatedly used, most people know, cataloged code Design experience summary.
  • A design pattern is a set piece for a set situation.

The basic classification

  • Creative patterns – singleton design patterns, factory method patterns, Abstract factory patterns,…
  • Structural mode – Decorator mode, proxy mode…
  • Behavioral patterns – template design patterns,…

Detailed explanation of design patterns

Singleton design pattern

  • Singleton design mode is mainly divided into: hungry and lazy, lazy need to multi-thread synchronization processing.

Common Factory model

The basic concept

  • The common factory method pattern is to create a factory class that creates instances of different implementation classes that implement the same interface.

The class diagram structure

Main drawback

  • In the normal factory method pattern, if the string passed is wrong, the object cannot be created correctly, and null-pointer exceptions may occur.

Multiple factory method patterns

The class diagram structure

Main drawback

  • In multiple factory method patterns, an object of the factory class needs to be created before a production method in the factory class can be invoked in order to create an object correctly.

Static factory method pattern

The class diagram structure

Practical significance

  • The factory method pattern is suitable for: When a large number of products need to be created and have common interfaces, the factory method pattern can be used for creation.

Main drawback

  • One problem with the factory method pattern is that the creation of classes depends on the factory class, which means that if you want to extend the program to produce new products, you have to modify the factory class code, which violates the open closed principle.

Abstract Factory pattern

The class diagram structure

Decorator mode

The basic concept

  • The decorator pattern dynamically adds new functionality to an object, requiring that the decorator object and the decorator object implement the same interface and that the decorator object hold an instance of the decorator object.

The class diagram structure

Practical significance

  • An extension of a class’s functionality can be implemented.
  • Functions can be added dynamically and undone dynamically (inheritance does not work).
  • Disadvantages: Too many similar objects, not easy to row.

The proxy pattern

The basic concept

  • The proxy mode is to find a proxy class to perform some operations on the original object.
  • For example, when we rent a house, we find an intermediary, and if we need a lawyer for a lawsuit, the intermediary and the lawyer are our agents here.

The class diagram structure

Practical significance

  • If you need to improve the original method, you can use a proxy class to call the original method and control the results, this way is the proxy mode.
  • The proxy mode can be used to divide functions more clearly, facilitating later maintenance.

Comparison of the proxy and decorator patterns

  • While the decorator pattern typically passes the original object as a parameter to the decorator’s constructor, the proxy pattern typically creates an object of the proxy-class within a proxy class.
  • The decorator pattern focuses on dynamically adding methods to an object, whereas the proxy pattern focuses on controlling access to the object.

Template method pattern

The basic concept

  • The template method pattern mainly refers to the encapsulation of a fixed process in an abstract class. The specific steps in the process can be implemented differently by different subclasses, and the fixed process can produce different results through the abstract class.

The class diagram structure

Practical significance

  • Multiple subclasses are common and the logic is basically the same content extracted to achieve code reuse.
  • Different subclasses achieve different effects to form polymorphism, which helps in later maintenance.