[Strategy with the design mode workout C # series]

zhaozj2021-02-16  51

Name: Strategy Chinese Name: Policy Type: Behavioral Description: Generally used for the implementation of multi-policy solutions. I thought it was very similar to the design idea and Bridge. However, the focus of Stratege is the implementation of multi-policy, and Bridge purposes The system's low coupling, of course, Strategy features it can be implemented. Because they are all basically dependent on the polymorphism features in OOP. It is inheriting the best alternative in many strategies.

// Created on 2003-3-14 at 7:27

Using system; // Member: Strategy concretestrategy conteterface statate {string multi_purpose (string _str);}

Class Concretestrategy1: Strategy {public string Multi_purpose (String_STR) {Return (_STR "Concretestrategy1);}}

Class Concretestrategy2: Strategy {public string Multi_purpose (String _str) {Return (_STR "Concretestrategy2);}}

class context {private strategy _strategy; public context (strategy m_STR) {_strategy = m_STR;} public string Test (string m_STRcontext) {return _strategy.multi_purpose (m_STRcontext);}

}

Class designPattern_Test {public static void main () {int m_inum; context m_con = null; console.writeline ("Which method you will choose: / n" "1.quick but not proflation./n" " 2.Pricition Butt Not quick./n" "1 or 2? / n"); for (;;) {string m_str = console.readLine (); if (m_str == "1" || m_str == "2") { m_inum = int32.parse (m_str); break;} else {console.writeline ("Which method you will choose: / n" "1.quick but not proflation./n" " 2.Pricition But not quick./ n " " 1 or 2? / n ");}}} f (m_inum == 1) m_con = new context (new concretestrategy1 ()); else m_con = new context (new concretestrategy2 ()); if (m_con! = NULL) {Console.WriteLine (M_Con.Test ("I am"));} else {console.writeline ("May Be the is a memory");}}} Review: 1. First It is the original support of the C # interface. We need to know that the implementation of the inheritance class's signature method must be public. See Bridge articles for your opinion. 2. By the way: C # is not supported by the initialization list and the default parameters Behavior. 3. Other implementation mechanism principles with Bridge. Extension: 1. In the design, adjustment of coupling is performed on the actual situation of Strategy and Context information interaction. 2. Many default ConcreteStrategy in Context In the case where the user does not have a special requirement, improve the performance of the probability in the sense of performance. 3. On a specific implementation, you can implement: Template

转载请注明原文地址:https://www.9cbs.com/read-23594.html

New Post(0)