Soft test senior system architect designer (9) composition template - on design patterns and their applications (to be continued)

Table of contents

Knowledge points mastered

Creational

Structural

Behavioral


Knowledge points mastered

  • What are the 3 categories of design patterns?

 

  • What specific design patterns are included in each category

Creational

The creational pattern is an abstraction of the object instantiation process. It encapsulates information such as how objects are created and combined in the system through the interface defined by the abstract class .

 

Creational mode is mainly used to create objects, so that software modules are not related to the creation of objects

The design patterns included are:

  • abstract factory pattern
  • builder mode
  • factory method pattern
  • prototype pattern
  • singleton pattern

Structural

Structural patterns are mainly used to assemble existing classes and objects to obtain a larger structure . Generally, concepts such as encapsulation, proxy, and inheritance are used to combine and encapsulate one or more classes to provide a unified external view or new function.

 

Mainly responsible for dealing with the relationship between classes or objects, effectively organizing classes and objects, and forming a good architecture

The main modes include:

  • adapter pattern
  • bridge mode
  • combination mode
  • decorator pattern
  • appearance mode
  • Flyweight mode
  • Proxy mode

Behavioral

This mode is mainly used for the allocation of responsibilities between objects and the services provided . It not only describes the mode of objects or classes, but also describes the communication mode between them, especially how a group of peer objects cooperate with each other to complete any of them. A task that neither subject can accomplish alone.

 

Mainly deal with the interaction between classes and objects, and describe how to assign responsibilities to classes and objects

The main modes include:

  • chain of responsibility model
  • command mode
  • interpreter mode
  • iterator pattern
  • mediator pattern
  • memo mode
  • Observer pattern
  • state mode
  • strategy pattern
  • template method pattern
  • visitor pattern

example:

Because the traditional structured software design method does not conform to the object-oriented design principles, it cannot well meet the requirements of high cohesion and low coupling, and the modules are too close, which brings many difficulties to software expansion and maintenance. In this case The emergence and wide application of design patterns provide an effective method to solve problems. By using design patterns, developers can use existing design methods to design software with reasonable structure, easy reuse and maintainability. When user needs change, the new needs can be met by modifying a small amount of code or without modifying the original code, which enhances the modifiability and stability of the system and reduces the cost of system development.

Strategy, which is a behavioral type, defines a series of algorithms, encapsulates them one by one, and makes them interchangeable, so that the algorithm can change independently of the user who uses it. In the alarm function of the monitoring module, the front-end interface of each software we monitor can be configured by the user to receive alarm information. For example, by default, DingTalk also has SMS, WeChat, and telephone voice. When the scheduled task is abnormal and meets the requirements of sending an alarm In the case of , the back-end code will obtain the information configured by the user, and call the specified strategy to send the alarm information according to the configuration information. The specific implementation is to first abstract the base class AlarmSender, and the subclass extends the base class DingSender(AlarmSender), class SMSSender (AlarmSender), etc., and define the specific implementation def send(self, info) in the subclass. Assuming that there is a RabbitMQ alarm and the user configures the default nailing method, the code when sending the alarm is to instantiate mq_ins = MQInfo(info='alarm content', way=[DingSender]), way is the specific way to send the alarm, and then call mq_ins.way.send(info) to complete sending the alarm. Using this mode, we found that the way of alerting (that is, the algorithm) can be switched freely, and the class name of the alert can be passed in as a parameter. This is also one of the benefits of relying on abstract class design interfaces, and it also reduces code redundancy. Good scalability, easy porting, and flexible use.

Example 2:

Strategy pattern:

In the system, the function of household payment is designed. There are currently many online payment channels, such as WeChat, Alipay, UnionPay and so on. The algorithms of each payment channel are different. At first, we used multiple conditions to judge, and the algorithm involved in the implementation of each channel’s payment also included heavy condition judgments. After defining it in this way, we found that the code was not concise enough, and it was not conducive to maintenance. After analysis, we chose to use The strategy mode first defines a paystrategy interface as an abstract role, and then defines specific roles such as alipaystrategy, wechatpaystrategy, and unicpaystrategy. These concrete implementation classes encapsulate the algorithm for the payment method, and these classes implement the paystrategy interface. Paycontextstartegy is defined, and this class refers to paystrateg. When the web requests payment, and has the pay_code of the payment method; after the controller receives the request, it uses paycontextstartegy to call specific payment classes such as alipaystategy, wechatpaystategy, and uniompaystrategy. By using the strategy pattern, we realize Free switching of different payment methods, avoiding multiple conditional judgments, using combination instead of inheritance, separates algorithm selection from algorithm implementation, reduces coupling between programs, and has good scalability and maintainability.

Example 3:

Chain of Responsibility Pattern: The Chain of Responsibility pattern is a behavioral design pattern that allows you to send requests along a chain of handlers. After receiving the request, each processor can process the request or pass it to the next processor in the chain.

testing platform: 

Request --- assemble public parameters (environmental information, public configuration) --- replace personalized parameters --- replace context parameters

 Reference: Chain of Responsibility Design Pattern (Chain of Responsibility Pattern)

Guess you like

Origin blog.csdn.net/wodeyijia911/article/details/131405735