Responsibility Chain Mode of JAVA design pattern; responsibility of java Design Pattern
What we bring to you today is the JAVA design pattern.
Someone may ask: What is JAVA? What is the design pattern?
Brave teacher's favorite sentence: JAVA is an internal skill, and the design mode is a combat skill. If only internal skill is used, the good internal skill is only a brute force. Only combat skill is used, and it becomes a flower fist embroidery leg.
How to grasp the two?
A sentence specific analysis.
One of the common JAVA design patterns today: responsibility chain (responsibility chain)
First of all, we simply understand our partner's responsibility chain.
Responsibility Chain mode [Chain of Responsibility]:
The responsibility chain mode is an object behavior mode. In the responsibility chain mode, many objects are connected by each object's reference to its next home to form a chain.
Requests are transmitted on this chain until an object on the chain decides to process this request. The client sending this request does not know which object on the chain will eventually process this request,
This allows the system to dynamically reorganize and assign responsibilities without affecting the client. (Concept interpretation comes from: JAVA and pattern by Dr. Yan Hong)
Many partners may be very vague about this concept. To put it simply, when A business object needs to complete task a B C D... When you wait for multiple tasks, the solution is to hand over the task to A first, and then teach B after completion.
Responsibility chain: task processing object
When a task is assigned to the responsibility chain, this advantage will occur.
Of course, there is also an unfavorable situation: when processing multiple tasks, the object is transmitted in the chain mode. Different implementation methods will affect the processing response speed.
Our simple task has understood the concept of the responsibility chain. Next we will Code together and feel the art of code.
We implement a simple responsibility chain based on the JAVA language.
Scenario: design a simulated mall discount. The shopping guide can only offer a discount of less than 100 yuan to the discount manager..
Objectives:
1. design an abstract processing class named AbstractHandlerChain
Requirement: 1. Hold the reference of the current object
2. Get the current object
Third, Common Business Processing Methods
4. Next object transfer
2. Two business processing objects inherit the AbstractHandlerChain
Project Structure
Design HandlerChainSales)
All operators () inherit the handling responsibility chain.
Manager Design
Object creation Factory
Result:
Simple Structure
Summary: What have you learned today? Today, I will introduce you to our responsibility chain model. The actual operation scenarios are very extensive, such as filter interception, servlet, Servlet Engine Startup, tomcat server, springmvc spring and other frameworks have applications. (Shanghai shangxuetao java training: original, please pay more attention to it. Some articles related to java development technologies are published one after another.)
If you want to use it flexibly, You need to experience it in the project practice. In the future, we will introduce this practical drill in the project, which is conducive to the use of the responsibility chain model of the security checksum and Variant set up by annotation configuration and responsibility chain.
Try it!