The 20190412 phase
Design Patterns - How to Understand Responsibility Chain Patterns?
Definition: Make multiple objects have the opportunity to process requests, thus avoiding the coupling between the sender and the receiver of requests. Connect these objects into a chain and pass the request along the chain until one object processes it.
That is to say, after the request, starting with the first object, the object receiving the request in the chain either handles it in person or forwards it to the next candidate in the chain. The object submitting the request does not know exactly which object will handle it -- that is, the request has an implicit receiver. According to the runtime, any candidate can respond to the corresponding request. The number of candidates is arbitrary. You can decide which candidates are involved in the chain at runtime.
Not clear yet? ok, a small example of life, crowded buses in the early rush hours, often crowded up but not see the conductor, we often pass ticket money to the conductor through the hands of others, this relationship can be seen as a chain of responsibility, our ticket money is finally delivered to the conductor through the hands of more than one person.
Code scenario:
Suppose we are developing an e-commerce website and a commodity is in the process of booking activities. The rules of the activities are as follows.
500 yuan deposit will receive 200 concessions
200 yuan deposit will receive 100 concessions
No scheduled user can only buy ordinary
Suppose our back end returns the following fields
Order Type [1,2,3] 500,200, ordinary purchase
Conventional implementation:
const order = function(orderType){ if(orderType===1){ // Suppose we have other needs if(....){ ..... } return console.log(500 Yuan Deposit User) } if(orderType===2){ // Suppose we have other needs if(....){ ..... } return console.log(200 Yuan Deposit User) } if(orderType===3){ // Suppose we have other needs if(....){ ..... } return console.log(User Ordinary Purchase) } } order(1) // 500 yuan deposit user
Although we get the expected results, it's not a good piece of code, and the order function changes frequently as the business changes.
Now let's rewrite it with the responsibility chain model
const Chain = function(fn){ this.fn = fn; this.successor = null; } Chain.prototype.setNextSuccessor = function(successor){ Specify the next node in the chain return this.successor = successor; } Chain.prototype.passRequest = function(){ //Pass a request to a lower node var ret = this.fn.apply(this,arguments); if(ret === false){ // If ret represents the chain for false, it has to go down. return this.successor && this.successor.passRequest.apply(this.successor,arguments) } } // Nodes packaged as responsibility chains var chainOrder500 = new Chain(order500); var chainOrder200 = new Chain(order200); // Then specify the order of nodes in the chain chainOrder500.setNextSuccessor(chainOrder200) // Finally, the request is passed to the first node chainOrder500.passRequest(1) // 500 yuan deposit user
Implementing Responsibility Chain with AOP(Aspect Oriented Programming)
It is simple and ingenious to implement responsibility chain with AOP, but this way of overlapping functions also overlaps the scope of functions. If the chain is too long, it will also affect performance.
Function.prototype.after(fn){ var _this = this; return function(){ var ret = _this.apply(this,arguments); if(ret === false){ return fn.apply(this.arguments); } return ret } } var order = order500.after(order200).after(orderNormal); order(2) // 200 deposit users
summary
Responsibility chain model can help us manage code well, reduce the coupling between the requesting object and the receiving object. The order of nodes in the responsibility chain is changeable, and we can decide which nodes are included in the chain in operation.
On JS Daily Question
JS Question of the Day can be seen as a voice answering community
Use fragmentation time every day to complete the day's test in 60 seconds of voice form
The group leader pushed the reference answers for the day at 0:00 the next day.
- Notes are not limited to the completion of the day's task, but more to check omissions and fill gaps, and other students in the study group excellent ideas for answering questions.