User-friendly design Model - Strategy Pattern

Foreword

  Today we look at the strategy pattern [Stragety Pattern [behavioral] ], this model is quite well understood. Strategies how to understand it? Generally it refers to: 1. can achieve the set goals of the program; 2. action methods of struggle and development of the situation and formulated; 3. artistic struggle, to pay attention to ways and means. Overall it is a collection of methods for different purposes. Here to talk about the strategy pattern how to understand it? Simply put, it is to conduct a class or its replacement algorithm can be changed at runtime.

Strategy Mode Introduction

First, the reason

  In software systems, some algorithms use objects or behavior may change frequently, if the algorithms of these changes are written to object, then the object will become more complex and difficult to understand. So how do we different algorithms at runtime dynamically modify the object of it? This is to use the strategy pattern.

Second, the intent

  Define a series of algorithms , encapsulate them one by one, and makes them interchangeable.

Third, the case of FIG.

 

Fourth, the strategy pattern code examples

  See chart above cases and cases like drawing on a state model is somewhat similar. They are contained in three parts. Specific differences we see in detail the back.

Environmental roles: holding a Strategy contains abstract policy references.

Abstract strategy: defining a common method, by specific strategies to achieve different algorithms.

Specific strategies: strategies to achieve the abstract interface methods.

  Here we see such a case, to buy things at a supermarket. Finally, when the settlement will be asked if there is membership, there are several settlement mechanism. Ordinary users full cost. Regular play 95 fold. Gold hit 9 fold. Diamond hit 8 fold. We look at how to achieve this functionality:

 

namespace Stragety_Pattern 
{ 
    class StragetyPattern 
    { 
    } 
    #region    Abstract Strategy ==================
     ///  <Summary> 
    /// abstract policy interfaces
     ///  </ Summary> 
    public  interface IStragetyPattern 
    { 
        ///  <Summary> 
        /// billing Interface
         ///  </ Summary> 
        void settlement ( decimal   Money); 
    } 
    #endregion 

    #region    specific strategies ================ =======
     ///  <Summary> 
    /// no membership calculated
     ///  </ Summary>
    public  class OrdinaryStragety: IStragetyPattern 
    { 
        public  void Settlement ( decimal   Money) 
        { 
            Console.WriteLine ($ " . Not a member, should not be discounted settlement payment Money} { " ); 
        } 
    } 

    ///  <the Summary> 
    ///   Member computing embodiment
     ///  </ Summary> 
    public  class MemberStragety: IStragetyPattern 
    { 
        public  void settlement ( decimal   Money) 
        { 
            Console.WriteLine ($ " ordinary members 95 hit off billing payables Money * 0.9M} {. "); 
        } 
    } 

    ///  <Summary> 
    /// Gold calculated
     ///  </ Summary> 
    public  class GoldMemberStragety: IStragetyPattern 
    { 
        public  void Settlement ( decimal   Money) 
        { 
            Console.WriteLine ($ " Gold, 10% off The settlement payable Money * 0.95M} { " ); 
        } 
    } 

    ///  <Summary> 
    /// diamond calculated
     ///  </ Summary> 
    public  class DiamondGoldMemberStragety: IStragetyPattern 
    { 
        public  voidSettlement ( decimal   Money) 
        { 
            Console.WriteLine ($ " Diamond Member, hit 8-fold settlement payables Money * 0.8M} {. " ); 
        } 
    } 
    #Endregion 

    #region    environmental role
     public  class ContextStragety 
    { 
        Private IStragetyPattern _stragety;
         public ContextStragety ( stragety IStragetyPattern) 
        { 
            _stragety = stragety; 
        } 

        ///  <Summary> 
        /// call billing method
         ///  </ Summary> 
        ///  <param name = "Money"> </ param>
        public void GetSettlement(decimal  Money) 
        {
            _stragety.Settlement( Money);
        } 
    }
    #endregion
}

 

namespace Stragety_Pattern 
{ 
    class Program 
    { 
        static  void the Main ( String [] args) 
        { 
            decimal the Account = 190.99M ;
             /// members of calculating 
            ContextStragety stragety = new new ContextStragety ( new new MemberStragety ()); 
            stragety.GetSettlement (the Account); 

            /// Normal billing 
             stragety = new new ContextStragety ( new new OrdinaryStragety ()); 
            stragety.GetSettlement (the Account); 
        } 
    } 
}

  Here we focused on the time of the final settlement amount is calculated can be interchangeable. Because in specific policy, we regard the changes in the algorithm encapsulated.

 Usage scenarios and the advantages and disadvantages

First, the use of scenarios

1, if there are some classes in the system, the difference between them lies in its conduct of words. You can use the Strategy pattern allows an object to select an action in a number of dynamic behavior.

2, a system need to choose between a variety of algorithms.

3. If an object has a lot of behavior, you can simplify the multiple conditions that select statement. Avoid difficult maintenance problems.

Second, the advantage

1, can freely switch between a policy class, as a method of abstract class policy in the policy is implemented. So you can freely switch.

2, easy to expand, our new strategy in time before the code is basically no need to modify.

3, for multiple conditions are selected to optimize simplified statement

Third, shortcomings

1, with the increase in the policy, the policy class will increase.

2, the client must know all the policy classes and to decide which strategy to use.

to sum up

  Here the strategy pattern to introduce finished. Strategy mode is mainly for a series of algorithms. And packaged them all together. You can switch freely between them. These algorithms make changes independent of the client changes. That is the change between the various acts were encapsulated. Then when we call can be switched call freedom.

  Here we have a somewhat similar case mentioned in the policy map mode and state mode at the outset. Here we focus on the analysis of the differences between some of the tactics pattern mode and status bar:

1, the role of the environment in different tasks, policy mode environment to have a role in the commission role is responsible for calling the algorithm passed in accordance with the policy. But state mode environment role in behavior is not only responsible for calling this method, it is also responsible for record status changes, class collaboration with the specific state. Completion status after switching behavior of the switch.

2, the strategy pattern the main problem is to reduce the impact of changes in internal algorithm to the outside. Guaranteeing the free handover algorithm. State mode main solution is to change the state causes a change in behavior, a change in state of the object, from the outside it seems as if behavior change.

3, Strategy Mode is a method of packaging. Here a package algorithm may be meaningful object, or may be logically meaningless fragment. Here, for example, encapsulated encryption algorithm. Various encryption algorithms, can freely switch. Algorithm must be parallel. State model is to ask some of the status bar changes with changes with behavior. Required to have state and behavior.


Even injured, but also raised his head and said with a smile, nice weather today.

   C # Design Patterns series directory

   Welcome to scan the next Fanger Wei code, and I set foot on the road to pass through the design patterns it!

  

Guess you like

Origin www.cnblogs.com/hulizhong/p/11678188.html