Examples of the responsibility chain mode in JS design mode, and details of js design mode instances

Source: Internet
Author: User

Examples of the responsibility chain mode in JS design mode, and details of js design mode instances

This article describes the responsibility chain mode of JS design patterns. We will share this with you for your reference. The details are as follows:

Responsibility Chain Design Model:

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 that sends this request does not know which object on the chain will eventually process this request, which allows the system to dynamically reorganize and assign responsibilities without affecting the client.

The roles involved in the responsibility chain mode are as follows:

● Abstract Handler role: defines an interface for processing requests. If necessary, the interface can define a method to set and return references to the next house. This role is usually implemented by a Java Abstract class or Java interface. The aggregate relationship of the Handler class in gives reference to the sub-class. The abstract method handleRequest () standardizes the operations of the sub-class to process requests.

● ConcreteHandler role: after receiving a request, the handler can choose to process the request or send the request to the next user. Because the specific Handler holds a reference to the next home, if necessary, the specific handler can access the next home.

In JavaScript (before ES6), there is no extends inheritance concept strictly. Therefore, the following code does not simulate abstract classes and only implements specific processing classes.

Use Cases: in a shopping mall, an activity was held on May Day, so the following discount schemes were made for book products based on the purchase amount,

1. 199 off for full purchase
2. 399 off for a full price purchase
3. If you have purchased more than 599 Yuan, you will be entitled to a discount;

Advantages of the responsibility chain:

The request sender only needs to know the first node in the chain, which weakens the strong relationship between the sender and a group of recipients. If you do not use the responsible chain mode, the client must know the discount information for each level based on the current price, and finally know that the discount for that level is the discount for the current price.

Function BookHandler () {this. calcPrice = function (price) {if (199> price) {console. log ("Original price:" + price);} else {this. successor. calcPrice (price) ;}} this. setSuccessor = function (_ successor) {this. successor = _ successor;} function BookCalc9Handler (_ successor) {this. calcPrice = function (price) {if (199 <= price & price <399) {console. log ("Original price:" + price + "; after a 0.9 discount:" + (price *);} else {this. successor. calcPrice (price) ;}} this. setSuccessor = function (_ successor) {this. successor = _ successor;} function BookCalc8Handler () {this. calcPrice = function (price) {if (399 <= price & price <599) {console. log ("Original price:" + price + "; after 0.8:" + (price *);} else {this. successor. calcPrice (price)} this. setSuccessor = function (_ successor) {this. successor = _ successor;} function BookCalc7Handler () {this. calcPrice = function (price) {if (price> = 599) {console. log ("Original price:" + price + "; 0.7 off:" + (price *);} else {this. successor. calcPrice (price)} this. setSuccessor = function (_ successor) {this. successor = _ successor ;}}

Client:

var price = 400;var bookHandler = new BookHandler();var bookCalc9Handler = new BookCalc9Handler();var bookCalc8Handler = new BookCalc8Handler();var bookCalc7Handler = new BookCalc7Handler();bookHandler.setSuccessor(bookCalc9Handler);bookCalc9Handler.setSuccessor(bookCalc8Handler);bookCalc8Handler.setSuccessor(bookCalc7Handler);bookHandler.calcPrice(price);

Console. log:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.