[設計模式練習]代理模式
直接切到主題=.=
類別圖
物件檔
using System.Collections.Generic;
using System.Text;
namespace DesignModelTest.代理
{
/// <summary>
/// 存取介面:付款項目
/// </summary>
interface ITransaction
{
void PayWater();
void PayGas();
void PayElectron();
}
/// <summary>
/// 接受者:學校
/// </summary>
class School
{
private string _name;
public string name
{
get { return _name;}
set { _name = value;}
}
public School(string name)
{
this._name = name;
}
}
/// <summary>
/// 委託人:學生
/// </summary>
class Student : ITransaction
{
School school;
public Student(School sc1)
{
this.school = sc1;
}
public void PayWater()
{
Console.WriteLine("付水費給" + school.name );
}
public void PayGas()
{
Console.WriteLine("付電費給" + school.name);
}
public void PayElectron()
{
Console.WriteLine("付瓦斯費給" + school.name);
}
}
/// <summary>
/// 代理人
/// </summary>
class Proxy : ITransaction
{
Student student;
private string _name;
public Proxy(School sc1,string name)
{
this.student = new Student(sc1);
this._name = name;
}
public void PayWater()
{
Console.WriteLine(this._name);
student.PayWater();
}
public void PayGas()
{
Console.WriteLine(this._name);
student.PayGas();
}
public void PayElectron()
{
Console.WriteLine(this._name);
student.PayElectron();
}
}
}
用戶端程式
DesignModelTest.代理.School school = new DesignModelTest.代理.School("學校一");
DesignModelTest.代理.Proxy proxy1 = new DesignModelTest.代理.Proxy(school, "代理機構一");
//請代理單位處理
proxy1.PayWater();
proxy1.PayGas();
proxy1.PayElectron();
Console.Read();
#endregion
輸出結果