Common design patterns in software design

The following are common design patterns and application scenarios:

  1. Factory Pattern: used to create objects, hiding the creation details of specific objects. The client only needs to obtain the object through the factory interface. Application scenarios include: when you need to generate different types of objects based on different parameters; when you need to follow the "opening and closing principle", that is, when adding new types, you only need to add a new factory class.

  2. Singleton Pattern: Used to ensure that a class can only have one instance and provide a global access point. It is typically used when you need to share resources, control access to resources, or only need a single instance.

  3. Observer Pattern: Defines a one-to-many dependency relationship. When the state of an object changes, all its dependencies will be notified and automatically updated. Application scenarios include: when it is necessary to achieve loose coupling between objects but does not want to use object-oriented inheritance; when it is necessary to implement an event handling mechanism.

  4. Adapter Pattern: Converts the interface of a class into another interface expected by the client, so that classes that originally cannot work due to incompatible interfaces can work together. Application scenarios include: when you need to reuse some classes, but the interfaces of these classes are incompatible with your code; when you need to create a reusable class that can work with unrelated or unforeseen classes.

  5. Strategy Pattern: Defines a family of algorithms and makes them interchangeable with each other. It separates the use and implementation of algorithms, and clients can flexibly choose different strategies to accomplish the same task. Application scenarios include: when you need to define some algorithms and want to dynamically select one of the algorithms at runtime; when an object has multiple behaviors and may use different behaviors in different scenarios.

  6. Decorator Pattern: Dynamically attach responsibilities to objects and extend the functionality of the object. It does this by creating wrapper objects that maintain interface consistency. Application scenarios include: when you need to enhance the functionality of an object and do not want to change the original class structure; when you need to add multiple optional functions to an object, choose different combinations of functions in different situations.

  7. Iterator Pattern: Provides a way to sequentially access individual elements in an aggregate object without exposing its internal implementation. It separates the traversal algorithm from the data structure, allowing the traversal method to be changed independently. Application scenarios include: when complex traversal of aggregate objects is required; when it is necessary to provide a general traversal method without exposing the internal representation of aggregate objects.

  8. State Pattern: Allows an object to change its behavior when its internal state changes, making the object appear as if it has modified its class. It encapsulates different states into independent classes, making the object's behavior easier to manage, expand and maintain when the state changes. Application scenarios include: when the behavior of an object depends on its state and the state may change; when different responses need to be made based on different states.

Replenish

The Interpreter Pattern is a behavioral design pattern that defines a grammatical representation of a language and an interpreter to interpret expressions in the language. This mode is mainly used to solve certain types of problems, such as compilers, parsers, etc.

In the interpreter mode, it usually includes the following roles:

  1. AbstractExpression: Defines an abstract interface for interpreting expressions in context.

  2. Terminal Expression: implements the abstract expression interface, represents the terminal symbols in the grammar, and performs specific interpretation operations.

  3. Non-terminal expression (Non-terminal Expression): implements the abstract expression interface, represents the non-terminal symbols in the grammar, and implements the interpretation operation of multiple terminal symbols through recursive calls.

  4. Context: Contains global information needed by the interpreter for interpretation operations.

Use the Interpreter pattern to build a simple language interpreter that converts complex grammatical rules into objects that are easy to understand and manipulate. The grammatical rules of the language can be flexibly changed and extended by adding new interpreters or modifying existing ones.

In summary, the interpreter pattern implements the function of interpreting and executing a specific language by defining grammatical rules and corresponding interpreters. It can be used to deal with language parsing and execution problems in some specific fields, making the change and expansion of grammatical rules simpler and more flexible.

Guess you like

Origin blog.csdn.net/weixin_42797483/article/details/133081394