Article 1: Creation Mode of Design Mode
Article 2: structural model of Design Model
In this section, we focus on the behavior pattern in the gof design pattern, which is used to divide responsibilities and algorithm abstractions between different objects. The behavior pattern involves not only classes and objects, it also involves how to associate classes with objects.
Behavior patterns include: chain of responsibility, command, interperter, iterator, mediator, and memento), observer, state, strategy, template, and visitor ). We will mainly discuss some of these models, and we will add more in the future.
1)Chain of responsibility)If a business is completed, many steps are required. However, if these operations are completely encapsulated into a class or method, they violate the principle of single responsibility. In this case, we can consider using the responsibility chain model. The corresponding UML diagram is as follows:
We can create many handler implementation classes and combine these handler "strings" by setting successor. So how to trigger all handler? This is similar to decorator. We can call successor. handlerrequest. In this way, the user only needs to care about the first handler, instead of the other handler.
2)Command)The command mode separates issuing and executing commands. When we execute a service, the client only needs to construct a request without worrying about the specific details of the Service implementation, that is, the construction of requests and business implementation are independent. The corresponding UML diagram is as follows:
We can see that when the client needs to execute a service, it needs to construct an invoker object, which is responsible for sending requests and generating a command object. At the same time, we can see that there is a aggreger object which is used to implement specific services. In concretecommand, We will reference this object to complete specific services.
3)Observer (observer)In our system, there is a business a, and multiple other businesses need to pay attention to business A. When its status changes, other businesses need to perform corresponding operations, in this case, we can use the observer mode. The observer mode is also called the subscription mode. It defines a "topic" (Business A), an abstract "subscriber", and many specific "subscriber" (other services ), in "topic", all "subscriber" references are retained, and "subscriber" can be added or deleted. When the status of "topic" changes, it proactively "notifies" all "subscribers" so that "subscribers" can perform corresponding operations. The corresponding UML diagram is as follows:
We can see that multiple subscriber references are retained in concretesubject. In the yysubscriber method, it calls the update method of each subscriber in sequence to update the "subscriber" status.
4)Visitor (visitor)When we have an object set, the element types in the set are different, but the types are relatively fixed. For example, there are only three different types, but there may be 30 elements. If we want to perform some operation on all elements in the Set, from the interface perspective, because the types are inconsistent, it is difficult to use a unified interface to traverse and operate the collection elements. In this case, we can consider using the visitor mode, which will get an element and separate the operations on the element. The corresponding UML diagram is as follows:
Here we assume that the set contains only two different types. objectbuilder is the set mentioned above, which contains multiple different ielement elements, the Core Implementation of the business is in visitora and visitorb. For the accept method of element1, it only calls visitor. visitelement1 method.
5)Template)Inheritance is the core of object-oriented, And the template method is the perfect embodiment of inheritance. For a business, we can design the method skeleton based on the general process and process it in an abstract way in an unclear or ambiguous place, then, different sub-services are used to create different sub-classes and implement those abstract methods in the sub-classes. The corresponding UML diagram is as follows: