“This is the 19th day of my participation in the August Gwen Challenge.
preface
At the advanced end of the interview, there are often some questions about design patterns, and the answers are not good. Coincides with the activities of more challenges in August, I plan to spend a month to clarify the knowledge points about design patterns, to increase the confidence of my interview.
define
The chain of responsibility pattern is defined as avoiding coupling between the sender and receiver of the request by giving multiple objects the opportunity to process the request, connecting the objects into a chain, and passing the request along the chain until one object processes it.
Let’s take an example from life
During the morning rush hour, taking the bus is often a hassle, and often you can’t get in through the front door, only through the back door. Before the bus or conductor, from the back of the people who get on the bus will often put 1 dollar forward, has been handed to the salesman’s hand.
The above is a chain of responsibility model. The biggest advantage of the chain of responsibility model can be seen from the example above. The request sender only needs to know the first node in the chain (the passenger only needs to hand the money to the first passenger standing in front), thus weakening the strong connection between the sender and a set of receivers. Without the chain of responsibility model, passengers on the bus would have to figure out who the conductor was before handing him the coin.
How to implement the chain of responsibility pattern
The chain of responsibility pattern lies in delivery, as illustrated by an example.
Code is used to implement a requirement, for example, in a market, to launch a new product, as long as the user, if you pay 1000 yuan deposit, buy can get 500 yuan coupon, if pay 500 yuan deposit, buy can get 200 yuan coupon, if scheduled for failing to pay the deposit, no coupons, when buying And it depends on the availability of stock.
The order function is used to fulfill the above requirements, with orderType representing the type of the order, pay representing whether the deposit is paid, and stock representing the inventory.
const order = (orderType, pay, Stock) => {if (orderType === 1) {if (pay === true) {console.log(' orderType === 1 '); } else {// No deposit paid, downgrade to normal purchase mode if (stock > 0) {// There is stock console.log(' Normal purchase, no coupon ') for normal purchase; } else {console.log(' phones out of stock '); }} else if (orderType === 2) {// Pay === true {console.log(' orderType === 2 '); } else {if (stock > 0) {console.log(' normal purchase, no coupon '); } else {console.log(' phones out of stock '); }} else if (orderType === 3) {if (stock > 0) {console.log(' normal purchase, no coupon '); } else {console.log(' phones out of stock '); }}}; order(1, true, 500); // Output: 1000 yuan deposit pre-order, get 100 couponsCopy the code
Although the above code has realized the requirements, the order function is very large, and the types of subsequent orders are increasing, so the order function will have a lot of if and else judgments, which will become more and more difficult to maintain.
The following is a transformation of the responsibility chain mode. The order with 1000 yuan deposit, 500 yuan deposit and no deposit is divided into three different functions to deal with.
// const orderType = (orderType, pay, Stock) => {if (orderType === 1 && pay === = true) {console.log('1000 yuan deposit order, get 500 coupons '); } else { order500(orderType, pay, stock); // Pass the request to the pre-paid 1000 deposit order}}; // Const order500 = (orderType, pay, stock) =>{if (orderType === 2 && pay === = true) {console.log('500 元 Get 200 coupons '); } else { orderNormal(orderType, pay, stock); // Pass the request to a normal order}}; // Const orderNormal = (orderType, pay, stock) =>{if (stock > 0) {console.log(' orderType, no coupon '); } else {console.log(' phones out of stock '); }}; order1000(1, true, 500); Order1000 (1, false, 500); // Output: normal purchase, no coupon order1000(2, true, 500); Order1000 (3, false, 500); // Output: normal purchase, no coupon order1000(3, false, 0); // Output: Not enough phones in stockCopy the code
Above, the original order function is divided into three small functions that do not affect each other, and a chain is formed and connected in series, which is the simplest chain mode of responsibility.
Of course, in the chain of responsibilities pattern implemented above, the transfer of responsibilities is very rigid and coupled to the business code, such as calling order500 functions from order500 functions. If the 500 down payment is changed to the 200 down payment sales plan, is it necessary to modify the Order500 function, which violates the open-closed principle? So no, we need to optimize. Otherwise it is like a chain of interlocking knots that must be broken before a node can be added, removed, or moved. You need to implement a flexible and detachable chain of responsibility node.