Policy Mode C #

Source: Internet
Author: User

The policy pattern defines a series of algorithms, encapsulates each algorithm, and allows them to be replaced with each other. The policy pattern makes the algorithm independent of the customers who use it.

Abstract Policy Roles: A policy class, usually implemented by an interface or abstract class. Specific policy Roles: The related algorithms and behaviors are packaged. Environmental Roles: Holds a reference to a policy class that is eventually called to the client. Context(scenario): 1. You need to use the algorithm provided by Concretestrategy. 2, internal maintenance of a strategy instance. 3, is responsible for the dynamic set run time strategy concrete implementation algorithm. 4, responsible for the interaction and data transfer with strategy. Strategy(Abstract Policy Class): 1, defines a public interface, a variety of different algorithms to implement this interface in different ways, the context uses this interface to invoke different algorithms, generally using an interface or abstract class implementation. Concretestrategy(Specific policy Class): 2, the implementation of the strategy definition of the interface, provide specific algorithm implementation.
/// <summary>    ///Abstract Policy Roles/// </summary>     Public InterfaceIStrategy {voiddosomething (); }    /// <summary>    ///specific policy role 1/// </summary>     Public classConcretestrategy1:istrategy { Public voiddosomething () {Console.WriteLine ("Strategy 1"); }    }    /// <summary>    ///specific policy role 2/// </summary>     Public classConcretestrategy2:istrategy { Public voiddosomething () {Console.WriteLine ("Strategy 2"); }    }    /// <summary>    ///Encapsulating Roles/// </summary>     Public classContent {Privateistrategy strategy;  PublicContent (IStrategy strategy) { This. Strategy =strategy; }         Public voiddoanything () { This. Strategy.        DoSomething (); }    }
Static void Main (string[] args)        {            new Content (new  ConcreteStrategy2 ());            Content. Doanything ();             New Content (new  ConcreteStrategy1 ());            Content. Doanything ();        }
Application Scenarios: 1, multiple classes only differ in performance behavior, you can use the Strategy mode, at run time to dynamically select the specific behavior to execute. 2. Different strategies (algorithms) need to be used in different situations, or strategies may be implemented in other ways in the future. 3, the customer hides the specific strategy (algorithm) Implementation details, each other completely independent. Advantages: 1. Policy mode provides a way to manage related algorithm families. The hierarchy structure of a policy class defines an algorithm or a family of behaviors. Proper use of inheritance can transfer common code to the parent class, thus avoiding duplicate code. 2. The policy mode provides a way to replace the inheritance relationship. Inheritance can handle multiple algorithms or behaviors. If the policy mode is not used, then the environment class using the algorithm or behavior may have some subclasses, each of which provides a different algorithm or behavior. However, the user of the algorithm or behavior is mixed with the algorithm or the behavior itself. The logic that determines which algorithm to use or which behavior to take is mixed with the logic of the algorithm or behavior, so that it is impossible to evolve independently. Inheritance makes it impossible to dynamically change an algorithm or behavior. 3. Use the policy mode to avoid using multiple conditional transfer statements. Multiple transfer statements are difficult to maintain, and it mixes the logic of which algorithm or behavior is taken with the logic of the algorithm or the action, all in a multiple transfer statement, more primitive and backward than the method of using inheritance. Disadvantages: 1. The client must know all the policy classes and decide which policy class to use at its own discretion. This means that the client must understand the differences between these algorithms in order to select the appropriate algorithm classes at the right time. In other words, the policy pattern applies only to situations where the client knows all the algorithms or behaviors. 2, the policy mode causes a lot of policy classes, each specific policy class will produce a new class. It is sometimes possible to design a policy class to be shareable by saving the environment-dependent state to the client, so that the policy class instance can be used by different clients. In other words, you can use the enjoy meta mode to reduce the number of objects.

Policy Mode C #

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.