[設計模式練習]範本方法模式
將文件處理的輸入參數利用範本方法設計
延遲到子類別再處理
類別圖
物件檔定義
using System.Collections.Generic;
using System.Text;
namespace DesignModelTest.範本方法
{
/// <summary>
/// 範本方法:將實作延遲到子類別
/// </summary>
class TextFileTransform
{
public void DisplayInputFile()
{
Console.WriteLine("輸入檔案名稱\t" + InputFileName());
}
public void DisplayUsedFormat()
{
Console.WriteLine("所採用的格式\t" + FormatName());
}
public void DisplayOutputFile()
{
Console.WriteLine("輸出檔案名稱\t" + OutputFileName());
}
protected virtual string InputFileName()
{
return string.Empty;
}
protected virtual string FormatName()
{
return string.Empty;
}
protected virtual string OutputFileName()
{
return string.Empty;
}
}
class Format1 : TextFileTransform
{
protected override string InputFileName()
{
return "輸入檔一";
}
protected override string FormatName()
{
return "格式一";
}
protected override string OutputFileName()
{
return "輸出檔一";
}
}
class Format2 : TextFileTransform
{
protected override string InputFileName()
{
return "輸入檔二";
}
protected override string FormatName()
{
return "格式二";
}
protected override string OutputFileName()
{
return "輸出檔二";
}
}
}
用戶端程式碼
DesignModelTest.範本方法.TextFileTransform t1 = new DesignModelTest.範本方法.Format1();
DesignModelTest.範本方法.TextFileTransform t2 = new DesignModelTest.範本方法.Format2();
t1.DisplayInputFile();
t2.DisplayInputFile();
t1.DisplayUsedFormat();
t2.DisplayUsedFormat();
t1.DisplayOutputFile();
t2.DisplayOutputFile();
Console.Read();
#endregion
輸出結果