Design Patterns --- 004 Strategy mode (self reprint teacher Alley- alley)

First, the concept of design patterns

  Design patterns are highlighted structural design codes and problem-solving ideas

 

Second, the strategy pattern

  Strategy Mode: All the solutions we should be set up in advance, do not judge according to what is happening today, the different problems and different solutions correspondence.
 
  The greatest advantage of the policy is to model problems and solutions do the treatment mappings, so you do not need to judge each
  

  In simple terms we normally Monday, Tuesday, Wednesday morning shift need to get up at 7 every Thursday, 10:00 on Friday Intermediate to get up every day, we would have booked using a mobile phone alarm clock early, the phone will be based on the current Some date to determine the alarm sounded, this is the simplest strategy mode

 

Third, the case

 

Copy the code
LoL = {var 
    Hero: { 
        "blind monk": function () { 
        
        }, 
       "Yasuo": function () { 

        }, 
        "Triticum": function () { 

         } 
    }, 
    Create: function (type) { 
        // ? What is here to fill 
    } 
} 

LoL.create ( "blind monk");



Think about how we get the hero of this object type by type?
 
Our first thought is to switch judgment, but the correct answer is to return new this.hero [type] ();
 
 
This reduces the determination logic of the code, the code efficiency will improve
Copy the code

 

 

 

Guess you like

Origin www.cnblogs.com/mp-0518/p/11440557.html