The intent of the policy pattern is to encapsulate each algorithm in a separate class with a common interface for a set of algorithms, so that they can be replaced with each other. The policy pattern allows the algorithm to change without affecting the client.
The strategy pattern is the wrapper over the algorithm, which divides the responsibility of the algorithm and the algorithm itself, delegating it to different object management. The strategy pattern typically wraps a series of algorithms into a set of policy classes as subclasses of an abstract policy class. In a word, it is: "Prepare a set of algorithms and encapsulate each algorithm so that they can be interchanged." "
A pattern involves three roles:
1. Environment (context) Role: holds a reference to a strategy class.
2. Abstract policy (icommunication) Role: This is an abstract role, usually implemented by an interface or abstract class. This role gives the interfaces required for all the specific policy classes.
3. Specific strategy (Serial, Lan) role: wrapping the relevant algorithm or behavior.
See examples directly:
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingsystem.timers;namespacedemo{ Public Interfaceicommunication {BOOLSend (Objectdata); } Public classserial:icommunication { Public BOOLSend (Objectdata) {Console.WriteLine ("an algorithm for sending a data through a serial port"); return true; } } Public classlan:icommunication { Public BOOLSend (Objectdata) {Console.WriteLine ("an algorithm for sending a data through a network port"); return true; } } Public classContext {Privateicommunication _communication; Public voidSetstrategy (icommunication communication)//delivery of specific policies { This. _communication =communication; } Public BOOLSend (Objectdata) { return This. _communication. Send (data); } } classProgram {Static voidMain (string[] args) {Console.WriteLine ("Please enter the type of communication: Lan, Serial"); stringinput =Console.ReadLine (); Objectdata =New Object(); Context CT=NewContext (); if(Input. Equals ("Lan"))//the choice of the client to determine the specific communication algorithm{Ct. Setstrategy (NewLan ()); } Else{Ct. Setstrategy (NewSerial ()); } Ct. Send (data); Console.readkey (); } }}
Operation Result:
As can be seen from the above example, strategy is similar to Factory mode, but factory changes the object when it is created, and the strategy mode is free to switch.
The strategy mode can be used to encapsulate poor but easy-to-implement solutions, or to encapsulate a better but difficult solution. A poor but easy-to-implement solution can be implemented first, and then tested with this poor scheme, which enables better scenarios at a later stage without changing the scheme of the calling algorithm.
The disadvantages of the strategy model are:
1. The client must know all of the policy classes and decide for itself which policy class to use. 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 model causes a lot of policy classes. 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.
C # design mode-strategist mode (strategy)