用设计模式固化你的C#程序(2)
Design Patterns: Solidify Your C# Application Architecture with Design Patterns中文版(上篇)
作者:Samir Bajaj 译者:荣耀 采用这种方式实现所有三个算法后,我就能够采用一种和任何特定算法实现细节毫无耦合的方式来设计客户程序。客户持有一个接口引用,并且不必知道该接口具体实现的任何细节。参见表4代码。
表4
class Primality { private Strategy strategy; public Primality(Strategy s) { strategy = s; } public bool Test(int n) { return strategy.IsPrime(n); } }
最后,我创建了一个Primality类的实例,根据用户输入,以相应算法对其进行初始化。Primality类的Test方法调用相应的算法对象(实现Strategy接口的对象)的IsPrime方法。 用这种方式构造算法族有很多优点,但最大的优点还是客户程序同特定算法实现细节毫无耦合关系。这提高了扩展性—可以开发别的算法并将其无缝插入,只要它们遵从基本接口规范。这样就可以动态变换算法。而且,strategy模式还避免了因为使用条件语句而使客户程序代码变得混乱的可能性。【译注:你理解这句话的含义吗J】
【译注:以下是strategy模式完整示例
C#示例: using System; interface Strategy { bool IsPrime(int n); } class Miller : Strategy { public bool IsPrime(int n) { bool result = false; file://使用Miller法测试n是否为素数,果真,则更新result值 Console.WriteLine("Using Miller's algorithm"); return result; } } class Fermat : Strategy { public bool IsPrime(int n) { bool result = false; file://使用Fermat法测试n是否为素数,果真,则更新result值 Console.WriteLine("Using Fermat's algorithm"); return result; } } class Mersenne : Strategy { public bool IsPrime(int n) { bool result = false; file://使用Mersenne法测试n是否为素数,果真,则更新result值 Console.WriteLine("Using Mersenne's algorithm"); return result; } } class Primality { private Strategy strategy;
public Primality(Strategy s) { strategy = s; } public bool Test(int n) { return strategy.IsPrime(n); } } class Application { public static void Main() { Console.Write("Number to be tested: "); string input = Console.ReadLine(); int n = Int32.Parse(input); Console.Write("Desired algorithm performance: lo, medium, hi? "); input = Console.ReadLine(); char ch = char.Parse(input); Primality prime = null; switch (ch) { case 'l': case 'L': prime = new Primality(new Miller()); break; case 'm': case 'M': prime = new Primality(new Fermat()); break; case 'h': case 'H': prime = new Primality(new Mersenne()); break; } if (prime != null) { bool result = prime.Test(n); } else Console.WriteLine("Bad Choice!"); } } /*以下是某次测试输出结果: Number to be tested:1 Desired algorithm performance: lo, medium, hi? M Using Fermat's algorithm */ C++示例: #include "stdafx.h"; #include class Strategy { public: virtual bool IsPrime(int n) = 0; }; class Miller : public Strategy { public: bool IsPrime(int n) { bool result = false; file://使用Miller法测试n是否为素数,果真,则更新result值 cout<<"Using Miller's algorithm\n"; return result; } }; class Fermat : public Strategy { public: bool IsPrime(int n) { bool result = false; file://使用Fermat法测试n是否为素数,果真,则更新result值 cout<<"Using Fermat's algorithm\n"; return result; } }; class Mersenne : public Strategy { public: bool IsPrime(int n) { bool result = false; file://使用Mersenne法测试n是否为素数,果真,则更新result值 cout<<"Using Mersenne's algorithm\n"; return result; } }; class Primality { private: Strategy* strategy; public: Primality(Strategy* s) { strategy = s; } ~Primality() { if (strategy != NULL) { delete strategy; strategy = NULL; } } bool Test(int n) { return strategy->IsPrime(n); } }; int _tmain(int argc, _TCHAR* argv[]) { cout<<"Number to be tested: "; int n; cin>>n; cout<<"Desired algorithm performance: lo, medium, hi? "; char ch; cin>>ch; Primality* prime = NULL; switch (ch) { case 'l': case 'L': prime = new Primality(new Miller()); break; case 'm': case 'M': prime = new Primality(new Fermat()); break; case 'h': case 'H': prime = new Primality(new Mersenne()); break; } if (prime != NULL) { bool result = prime->Test(n); delete prime; prime = NULL; file://这儿还演示了一个糟糕的设计,你不能够释放Miller或Fermat或Mersenne对象,知道原因吗? } else cout<<"Bad Choice!\n"; return 0; } /*以下是某次测试输出结果: Number to be tested:1 Desired algorithm performance: lo, medium, hi? M Using Fermat's algorithm */ 】
摘自红色黑客联盟(www.) 原文:http://www./kf/200609/12907.html
|