[設計模式練習]轉接器模式
將程式設計人員分成資深和新進人員
有天公司來了外籍的工程師在外駐點
要讓他設計符合公司規範的產品
利用轉接器設計告知
類別圖
物件檔定義
using System.Collections.Generic;
using System.Text;
namespace DesignModelTest.轉接器
{
/// <summary>
/// 抽象類別:IT工作者
/// </summary>
abstract class ITWorker
{
protected string name;
public ITWorker(string name)
{
this.name = name;
}
public abstract void Design();//設計
public abstract void Maintain();//維護
}
/// <summary>
/// 資深人員
/// </summary>
class OldITWorker : ITWorker
{
public OldITWorker(string name) : base(name) { }
public override void Design()
{
Console.WriteLine("資深人員 {0} 有經驗的設計 ",name);
}
public override void Maintain()
{
Console.WriteLine("資深人員 {0} 有經驗的維護 ", name);
}
}
/// <summary>
/// 新進人員
/// </summary>
class NewITWorker : ITWorker
{
public NewITWorker(string name) : base(name) { }
public override void Design()
{
Console.WriteLine("新進人員 {0} 無經驗的設計 ", name);
}
public override void Maintain()
{
Console.WriteLine("新進人員 {0} 無經驗的維護 ", name);
}
}
/// <summary>
/// 外籍人員 : 不認識IT工作者類別(ITWorker)
/// </summary>
class ForeignITWorker
{
public string name;
public void Design()
{
Console.WriteLine("外籍人員 {0} 設計 ", name);
}
public void Maintain()
{
Console.WriteLine("外籍人員 {0} 維護 ", name);
}
}
/// <summary>
/// 轉接器 : 負責轉接的介面
/// </summary>
class Translator : ITWorker
{
ForeignITWorker foreign = new ForeignITWorker();
public Translator(string name)
: base(name)
{
foreign.name = name;
}
public override void Design()
{
foreign.Design();
}
public override void Maintain()
{
foreign.Maintain();
}
}
}
用戶端程式碼
DesignModelTest.轉接器.ITWorker old = new DesignModelTest.轉接器.OldITWorker("老鳥");
old.Design();
old.Maintain();
DesignModelTest.轉接器.ITWorker newone = new DesignModelTest.轉接器.NewITWorker("小菜");
newone.Design();
newone.Maintain();
//透過轉接器來執行物件內的動作即使彼此不認識
DesignModelTest.轉接器.ITWorker foreign = new DesignModelTest.轉接器.Translator("老外");
foreign.Design();
foreign.Maintain();
Console.Read();
#endregion
輸出結果