Java uses responsibility chain mode to deal with students’ leave
This paper illustrates how Java uses the responsibility chain model to deal with students' leave. Share with you for your reference, as follows:
I Schema definition
In the responsibility chain model, many objects are connected by each object's reference to its next family to form a chain. Client application requests are passed on the chain until an object on the chain decides to process the request. The client issuing the request does not know which object in the chain will eventually process the request, which enables the system to dynamically reorganize the chain and assign responsibilities without affecting the client.
(1) Abstract handler role: defines an interface to handle requests. If necessary, the interface can define a method to set and return the reference of the next home. This role is usually implemented by a Java abstract class or Java interface.
(2) Specific processing role: after receiving the request, the specific processor can choose to process the request or send the request to the next home.
II Model example
1 mode analysis
We borrow the first mock exam to explain this pattern.
2 static class diagram of responsibility chain mode
3 code example
3.1 student interface I IStudent
3.2 students realize one student
3.3 abstract processing interface ihandler
3.4 abstracthandler
3.5 squad leader - squadleaderhandler
3.6 teacher handler
3.7 headmaster - schoolmasterhandler
3.8 appearance class I processhandler
3.9 client
4 operation results
III Design principle of this mode
1 "open close" principle
2. Principle of single responsibility
IV Use occasion
(1) There are multiple objects processing the same request. It is uncertain which object will process it. Only at run time can it be determined which object will process it. (2) When a message has multiple recipients and the receiving object is ambiguous. You only need to send a message to one of the objects, which will be processed internally. (3) Multiple processing objects of the same message may increase or decrease dynamically and need to be specified dynamically.
V Static class diagram of responsibility chain pattern
For more Java related content, interested readers can view the special topics of this site: Java data structure and algorithm tutorial, summary of Java DOM node operation skills, summary of java file and directory operation skills, and summary of Java cache operation skills