- 表示一個作用於某物件結構中的各元素之操作。它使你可以在不改變各元素之類別的前提下,定義作用於這些元素的新操作。
- 適用於資料結構相對穩定的系統
- 它把資料結構和作用於結構上的操作之間的耦合解脫開,使得操作集合可以相對自由地演化。
- 其目的是要把處理從資料結構分離出來。
- 其缺點係不易增加新的資料結構

using System;
using System.Collections.Generic;
namespace DPExample
{
// 狀態
abstract class Action
{
protected string name;
protected string Name
{
get { return name; }
set { name = value; }
}
// 得到男人結論或反應
public abstract void GetManConclusion(Man concreteElementA);
// 得到女人結論或反應
public abstract void GetWomanConclusion(Woman concreteElementB);
}
// 人
abstract class Person
{
protected string name;
protected string Name
{
get { return name; }
set { name = value; }
}
// 接受
public abstract void Accept(Action visitor);
}
// 具體狀態
class Success : Action
{
public override void GetManConclusion (Man concreteElementA)
{
Console.WriteLine ("{0}{1}時,背後多半有一個偉大的女人。", concreteElementA.GetType().Name, this.GetType().Name);
}
public override void GetWomanConclusion(Woman concreteElementB)
{
Console.WriteLine ("{0}{1}時,背後多半有一個不成功的女人。", concreteElementB.GetType().Name, this.GetType().Name);
}
}
class Failing : Action
{
public override void GetManConclusion (Man concreteElementA)
{
Console.WriteLine ("{0}{1}時,悶頭喝酒,誰也不用勸。", concreteElementA.GetType().Name, this.GetType().Name);
}
public override void GetWomanConclusion(Woman concreteElementB)
{
Console.WriteLine ("{0}{1}時,眼淚汪汪,誰也勸不了。", concreteElementB.GetType().Name, this.GetType().Name);
}
}
class Amativeness : Action
{
public override void GetManConclusion (Man concreteElementA)
{
Console.WriteLine ("{0}{1}時,凡事不懂也要裝懂。", concreteElementA.GetType().Name, this.GetType().Name);
}
public override void GetWomanConclusion(Woman concreteElementB)
{
Console.WriteLine ("{0}{1}時,遇事懂也裝作不懂。", concreteElementB.GetType().Name, this.GetType().Name);
}
}
class Marriage : Action
{
public override void GetManConclusion(Man concreteElementA)
{
Console.WriteLine ("{0}{1}時,感慨道:戀愛遊戲終結時,'有妻徒刑' 遙無期。", concreteElementA.GetType().Name, this.GetType().Name);
}
public override void GetWomanConclusion(Woman concreteElementB)
{
Console.WriteLine ("{0}{1}時,欣慰曰:愛情長跑路漫漫,婚姻保險保平安。", concreteElementB.GetType().Name, this.GetType().Name);
}
}
// 男人
class Man : Person
{
public override void Accept (Action visitor)
{
visitor.GetManConclusion(this);
}
}
// 女人
class Woman : Person
{
public override void Accept (Action visitor)
{
visitor.GetWomanConclusion(this);
}
}
// 物件結構
class ObjectStructure
{
private IList elements = new List();
public void Attach (Person element)
{
elements.Add (element);
}
public void Detach (Person element)
{
elements.Remove (element);
}
public void Display (Action visitor)
{
foreach (Person e in elements)
e.Accept (visitor);
}
}
}
using System;
namespace DPExample
{
public class Program
{
static void Main()
{
ObjectStructure o = new ObjectStructure();
o.Attach (new Man());
o.Attach (new Woman());
Success v1 = new Success();
o.Display (v1);
Failing v2 = new Failing();
o.Display (v2);
Amativeness v3 = new Amativeness();
o.Display (v3);
Marriage v4 = new Marriage();
o.Display (v4);
}
}
}
留言
張貼留言