abstract class Person
{
public abstract void Accept ( Action visitor) ;
}
abstract class Action
{
public abstract void GetManConclusion ( Man concreteElementA) ;
public abstract void GetWomanConclusion ( Woman concreteElementB) ;
}
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 Amativeness : Action
{
public override void GetManConclusion ( Man concreteElementA)
{
Console. WriteLine ( "男人戀愛時,不懂也要裝懂" ) ;
}
public override void GetWomanConclusion ( Woman concreteElementB)
{
Console. WriteLine ( "女人戀愛時,懂也要裝不懂" ) ;
}
}
class Failing : Action
{
public override void GetManConclusion ( Man concreteElementA)
{
Console. WriteLine ( "男人失敗時,悶頭喝酒,誰也不用勸" ) ;
}
public override void GetWomanConclusion ( Woman concreteElementB)
{
Console. WriteLine ( "女人失敗時,眼淚汪汪,誰也勸不動" ) ;
}
}
class Success : Action
{
public override void GetManConclusion ( Man concreteElementA)
{
Console. WriteLine ( "男人成功時,背後多半有一個偉大的女人" ) ;
}
public override void GetWomanConclusion ( Woman concreteElementB)
{
Console. WriteLine ( "女人成功時,背後多半有一個不成功的男人" ) ;
}
}
class ObjectStructure
{
private IList< Person> elements = new List < Person > ( ) ;
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) ;
}
}
}
class Program
{
static void Main ( string [ ] args)
{
ObjectStructure structure = new ObjectStructure ( ) ;
structure. Attach ( new Man ( ) ) ;
structure. Attach ( new Woman ( ) ) ;
structure. Display ( new Failing ( ) ) ;
structure. Display ( new Amativeness ( ) ) ;
structure. Display ( new Success ( ) ) ;
}
}