C # design pattern-Strategy ),

Source: Internet
Author: User

C # design pattern-Strategy ),

The purpose of the rule mode is to encapsulate each algorithm into an independent class with a common interface for mutual replacement. The policy mode allows the algorithm to change without affecting the client.

Policy mode is the packaging of algorithms. It separates the responsibility for using algorithms from the algorithms themselves and delegates them to different objects for management. Rule mode typically packs a series of algorithms into a series of policy classes and serves as a subclass of an abstract policy class. In one sentence, it is: "prepare a set of algorithms and encapsulate each algorithm so that they can be exchanged. "

The Mode involves three roles:

1. Context Role: Hold a reference to the Strategy class.

2. Abstract Policy (ICommunication) Role: This is an abstract role, usually implemented by an interface or abstract class. This role provides all the interfaces required for specific policy classes.

3. Specific Policy (Serial, Lan) Roles: encapsulate related algorithms or behaviors.

Let's look at the example:

Using System; using System. collections. generic; using System. linq; using System. text; using System. timers; namespace Demo {public interface ICommunication {bool Send (object data);} public class Serial: ICommunication {public bool Send (object data) {Console. writeLine ("an algorithm for sending data via serial port"); return true ;}} public class Lan: ICommunication {public bool Send (object data) {Console. writeLine ("algorithm for sending data through the network port"); return true ;}} public class Context {private ICommunication _ communication; public void SetStrategy (ICommunication communication) // pass the specific policy {this. _ communication = communication;} public bool Send (object data) {return this. _ communication. send (data) ;}} class Program {static void Main (string [] args) {Console. writeLine ("Enter the communication type: Lan, Serial"); string input = Console. readLine (); object data = new object (); Context ct = new Context (); if (input. equals ("Lan") // determine the specific communication algorithm {ct. setStrategy (new Lan ();} else {ct. setStrategy (new Serial ();} ct. send (data); Console. readKey ();}}}

Running result:

From the above example, we can see that Strategy is similar to the Factory mode, but the Factory changes the object during creation, while the Strategy Mode can switch freely.

The Strategy mode can be used to encapsulate poor but easy-to-implement solutions, or better but hard-to-implement solutions. You can first implement a poor but easy-to-implement solution, and then use this poor solution for testing. Using this mode, you can implement a better solution in the future without having to change the algorithm calling solution.

Disadvantages of the Policy mode include:

1. The client must know all the policy classes and decide which one to use. This means that the client must understand the differences between these algorithms so that appropriate algorithm classes can be selected in a timely manner. In other words, the policy mode is only applicable when the client knows all the algorithms or actions.

2. The rule mode causes many policy classes. Sometimes you can save the environment-dependent status to the client and design the policy class to be shared so that the policy class instance can be used by different clients. In other words, you can use the metadata mode to reduce the number of objects.

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.