AbstractServerパターン
// Abstract Server Pattern
// 抽象化を用いて、依存性を制御する。
class Program
{
static void Main(string[] args)
{
var button1 = new Button(new Light());
button1.Push();
button1.Push();
button1.Push();
var button2 = new Button(new Stereo());
button2.Push();
button2.Push();
button2.Push();
}
}
class Button
{
private ISwitchable _switch;
public Button(ISwitchable sw)
{
this._switch = sw;
}
public void Push()
{
if (this._switch.IsOn) {
this._switch.Off();
} else {
this._switch.On();
}
}
}
interface ISwitchable
{
bool IsOn { get; set; }
void On();
void Off();
}
class Light : ISwitchable
{
#region ISwitchable メンバー
public bool IsOn { get; set; }
public void On()
{
Console.WriteLine("照明をつけます。");
this.IsOn = true;
}
public void Off()
{
Console.WriteLine("照明をけします。");
this.IsOn = false;
}
#endregion
}
class Stereo : ISwitchable
{
#region ISwitchable メンバー
public bool IsOn { get; set; }
public void On()
{
Console.WriteLine("ステレオをつけました。");
this.IsOn = true;
}
public void Off()
{
Console.WriteLine("ステレオをけしました。");
this.IsOn = false;
}
#endregion
}