本來園子裡面已經有很多特別好的關於設計模式學習的文章
名詞解釋:
策略:
從策略一詞來看
在他的文章中給GRIDVIEW添加上數據導出成EXCEL
具體策略模式的類圖:
策略模式定義(GOF):
策略模式:主要是定義一系列的算法
具體代碼如下
第一
抽象策略(Strategy)角色
Code
/**//// <summary>
/// 將GridView導出到其他文件的接口
/// </summary>
public interface IExporter
{
/**//// <summary>
/// 輸出編碼
/// </summary>
Encoding ResponseEncoding { get;}
/**//// <summary>
/// 導出文件的名稱
/// </summary>
string ExportFileName { get;}
/**//// <summary>
/// 將GirdView導出為其他格式的文件
/// </summary>
/// <param name=
/// <param name=
void Export(GridViewEx grid);
}
下面為實現了此接口的抽象類
Code
public abstract class Exporter:IExporter
{
private fields#region private fields
Encoding _responseEncoding;
string _exportFileName;
#endregion
my constructors#region my constructors
public Exporter(Encoding encoding
{
this
this
}
#endregion
public property#region public property
/**//// <summary>
/// 輸出編碼
/// </summary>
public Encoding ResponseEncoding
{
get
{
return _responseEncoding;
}
}
/**//// <summary>
/// 導出文件的名稱
/// </summary>
public string ExportFileName
{
get
{
return _exportFileName;
}
}
#endregion
public methods#region public methods
/**//// <summary>
/// 將GirdView導出為其他格式的文件
/// </summary>
/// <param name=
/// <param name=
public abstract void Export(GridViewEx grid);
#endregion
private methods#region private methods
protected string ReplaceHref(string html)
{
string content = Regex
return content;
}
protected virtual void Export(GridViewEx grid
{
//具體代碼略
}
#endregion
}
第二
環境(Context)角色
Code
DropDownList drop = dropList;
if (drop != null)
{
if (drop
{
switch (drop
{
case
{
IExporter exporter = new ExcelExporter(Encoding
ExporterHelper
break;
}
case
{
IExporter exporter = new WordExporter(Encoding
ExporterHelper
break;
}
case
{
IExporter exporter = new PdfExporter(Encoding
ExporterHelper
break;
}
}
}
}
[nextpate]
第三
具體策略(ConcreteStrategy)角色
下面是三個具體操作類
Code
/**//// <summary>
/// Excel導出
/// </summary>
public class ExcelExporter:Exporter
{
private fields#region private fields
static readonly string extension =
static readonly string contentType =
#endregion
my constructors#region my constructors
public ExcelExporter(Encoding encoding
{
}
#endregion
public methods#region public methods
/**//// <summary>
/// 將GirdView導出為其他格式的文件
/// </summary>
/// <param name=
/// <param name=
public override void Export(GridViewEx grid)
{
Export(grid
}
#endregion
PdfExporter:
Code
public class PdfExporter : Exporter
{
static readonly string extension =
static readonly string contentType =
public PdfExporter(Encoding encoding
: base(encoding
{
}
public override void Export(GridViewEx grid)
{
//具體代碼略
}
}
WordExporter:
Code
/**//// <summary>
/// Word導出
/// </summary>
public class WordExporter:Exporter
{
private fields#region private fields
static readonly string extension =
static readonly string contentType =
#endregion
my constructors#region my constructors
public WordExporter(Encoding encoding
: base(encoding
{
}
#endregion
public methods#region public methods
/**//// <summary>
/// 將GirdView導出為其他格式的文件
/// </summary>
/// <param name=
/// <param name=
public override void Export(GridViewEx grid)
{
Export(grid
}
#endregion
}
策略模式優點
算法的使用和算法本身解耦
策略模式缺點
總結
設計模式的有效應用能夠給我們的開發帶來效率
From:http://tw.wingwit.com/Article/program/net/201311/13392.html