JavaScript implementation Policy mode

Source: Internet
Author: User

Before you begin, share the words you see today about design patterns: separating the unchanging parts from the changes is the theme of each design pattern.

Please feel the essence of this sentence on your own, and think about what the learning Design model can bring to our programming, and welcome you to comment (Chui) on the article (shui) and share your valuable insights. Well, back to the point today, JavaScript implements the policy model.

What is a policy mode

Define a series of algorithms, encapsulate them one by one, and make them convert to each other. Speaking of words is: I want to travel, then I can choose a different method (strategy) to achieve my purpose-tourism. For example, fly, walk, even a small yellow car ofo, this depends on how you choose. If you want to hurry, then choose to fly, if you have a dream, then choose to walk, no matter what you choose, finally you can achieve the same purpose-travel.

Strategy Mode thought

Joining the above example and opening the first sentence, in the strategic mode, the unchanging is our purpose-travel, and the changing part is the way we go (by plane, walk, Ofo), so we need to separate the two parts to achieve decoupling. That is the purpose of the strategy model. In order to compare the benefits of the strategy model, I first completed the above example in a general way. Please look at the code:

 varTravel =function(transportation, destination) {if(Transportation = = ' plane ') {Console.log ("Flying to" + Destination + "yes"); }        if(Transportation = = ' foot ') {Console.log ("Walking to" + Destination + "La"); }        if(Transportation = = ' Ofo ') {Console.log ("Ride the Yellow car to" + Destination + "); }} Travel ("Foot", "Beijing");//Console printing: Walking to BeijingTravel ("Ofo", "Paradise");//Console printing: Riding a yellow car to heaven

We have now completed the tourist code, looks good oh. But with all due respect, this is a spicy chicken code. Hehe, specifically for the following reasons:

1. The travel function contains many if statements that need to overwrite all logical branches

2, travel function lacks elasticity, if now more a kind of vehicle small blue car, we must revise travel function source code, this unscientific! True is a violation of the open-closed principle.

Now that we use the policy model to optimize this code, take a look at the following code:

   varStrategies ={plane:function(destination) {Console.log ("Flying to" + Destination + "yes"); }, Foot:function(destination) {Console.log ("Walking to" + Destination + "La"); }, Ofo:function(destination) {Console.log ("Ride the Yellow car to" + Destination + ")        }    }    varTravel =function(transportation, destination) {Strategies[transportation] (destination); } Travel ("Foot", "Beijing"); Travel ("Ofo", "heaven");

The results of the implementation are as follows:

We can see that we have completed our demand through the strategy mode, and if we have more transportation, whether it is a small yellow car or a small black car, we can only use the policy object strategies to add a new configuration, instead of changing our travel function.

Once again, the first sentence of the opening: separating the unchanging parts from the changes is the subject of each design pattern.

In combination with our strategic approach, the unchanging part of the strategy model is the goal (the previous tour), and the changing part is the process (transportation and destination). Through the policy model we completely decouple the two parts, and our process (strategies) changes and does not affect our purpose (travel). I think this is the benefit of the strategy model.

  

    

JavaScript implementation 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.