程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> 用設計模式固化你的C#程序(2)

用設計模式固化你的C#程序(2)

編輯:C#入門知識

用設計模式固化你的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 Millers algorithm");
return result;
}
}
class Fermat : Strategy
{
public bool IsPrime(int n)
{
bool result = false;
file://使用Fermat法測試n是否為素數,果真,則更新result值
Console.WriteLine("Using Fermats algorithm");
return result;
}
}
class Mersenne : Strategy
{
public bool IsPrime(int n)
{
bool result = false;
file://使用Mersenne法測試n是否為素數,果真,則更新result值
Console.WriteLine("Using Mersennes 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 Fermats 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 Millers algorithm ";
return result;
}
};
class Fermat : public Strategy
{
public:
bool IsPrime(int n)
{
bool result = false;
file://使用Fermat法測試n是否為素數,果真,則更新result值
cout<<"Using Fermats algorithm ";
return result;
}
};
class Mersenne : public Strategy
{
public:
bool IsPrime(int n)
{
bool result = false;
file://使用Mersenne法測試n是否為素數,果真,則更新result值
cout<<"Using Mersennes algorithm ";
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! ";
return 0;
}
/*以下是某次測試輸出結果:
Number to be tested:1
Desired algorithm performance: lo, medium, hi? M
Using Fermats algorithm
*/

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved