[design mode] JavaScript's strategy mode

Source: Internet
Author: User
Tags what interface

Definition: Encapsulates a series of algorithms so that they can be replaced by each other, and this pattern uses an algorithm independent of the changes in the customer using it.

  Description: The strategy mode, is a pattern of the organization algorithm, the core is not the algorithm, but the organization of a series of algorithms, and how to use it; the role of strategy mode is to realize the unpredictable behavior, in the face of such a change, we have to think about how to use the program good dimension and extension, And make the customer very good use of the method of the algorithm;  strategy mode use to pay attention to its "change" side, the strategy mode is to solve the change problem.   such as the price of shopping malls or promotional issues, if you do not use the model, it is possible to simply "all" the situation with if else like "hard-coded" open write together, or pass a parameter, a little bit of internal logic code, preferably written together in a class inside;  p such as:   function Price (PersonType, price) {   //Big customer 50 percent     if (PersonType = = ' VIP ') {  &nbsp ;     Return price * 0.5;   }     else if (PersonType = = ' old ') {//Customer 30 percent         Return price * 0.3;   } else {        return price;//All other full price    }}&N BSP; We look at the above as a class, if we want to extend a price method, we have to add a new else if, or modify an algorithm logic, to have an if or else if the modification, this is a single class modification, and this situation will often change this class, This violates one of the principles of design patterns: the principle of;  and opening up, and these algorithms, which may be replaced frequently in the presence of different customers, and fixed parameters can not meet the requirements;  to solve this problem, we could use the strategy mode, the strategy is to pay attention to the change, Different behavior of the implementation of each package together, such as to achieve an animal, walking, shouting, eating a variety of performance, you can walk, call, eat, you can define three interface abstraction, and then their respective implementation to achieve three interfaces;    said before, The policy pattern is to make the algorithm independent of the use of the customer's changes, so in the policy mode, is generally defined in the clientSelect the algorithm used, pass in the corresponding parameters, and then return the results according to the algorithm;     this is somewhat similar to the simple Factory mode, this addition layer Context is for customers to use more simple call, do not have to handle some logic, let the team development, more consistent Context can also be made into a base class, under which there can be multiple implementations of subclass inheritance;  context and client can be changed to:    inheriting subclasses simply by referencing their own implementation interface algorithm, so that the customer can be more simple to call, Also do not care what kinds of animals use what interface, is how to achieve the;  instance source code    here to the above market price means to achieve:  1. The implementation of several algorithms, JavaScript is a weak type, no interface this thing, is directly written;  VIP customer  function Vipprice () {    this.discount = 0.5;}  vipprice.prototype.getprice = function (price) {return price * THIS.DISCOUNT;}   Old customers:   function Oldprice () {    this.discount = 0.3;}  oldprice.prototype.getprice = function (price) {    return price * THIS.DISCOUNT;}   General Customer   function Price () {    this.discount = 1;}  price.prototype.getprice = function (price) {    return price;}  2. Context:   function context () {    this.name = ';    this.strategy = null;    THIS.PRI CE = 0;}  context.prototype.set = function (NAMe, strategy, price) {    this.name = name;    This.strategy = strategy;    This.price = pri CE;}  context.prototype.getresult = function () {    Console.log (this.name + ' checkout price: ' + this.stragegy.getPrice (This.price));}  3. Client use;   //context var contextual = new context ();  //VIP client var VIP = new Vipprice () Context.set (' VIP client ', VIP, 200); Context.getprice ();  //old customer var = new Oldprice (); Context.set (' Old client ', Lao, $); Context.getprice ();  //General Customer/ /...  So even if the client and the algorithm to decouple, and make the modification and expansion can be independent, not shadow to the use of the client or other algorithms;  other description   Strategy mode is a very practical and commonly used design mode, common use for shopping malls prices, promotions and other scenes, He's a lot like the factory model. Upgrade version;  strategy mode related mode: Factory mode, combined mode;

[design mode] JavaScript's policy mode

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.