設計模式(3)-訪問者模式
模擬不同身份開啟窗體,實現不同的功能:
class Program { static void Main(string[] args) { FORM f = new FORM(); Visitor v1 = new AdminVisitor(); Visitor v2 = new UserVisitor();
f.Accept(v1);
f.button = "aaa";
f.button_OnClick(); } } public class FORM { private Visitor visitor;
public string button { get; set; }
public void Accept(Visitor v) { this.visitor = v; this.visitor.Visit(this); }
public void FORM_OnLoad() { this.visitor.FORM_OnLoad(); }
public void button_OnClick() { this.visitor.button_OnClick(); } } public abstract class Visitor { protected FORM FORM; public void Visit(FORM f) { this.FORM = f; }
public abstract void FORM_OnLoad();
public abstract void button_OnClick(); }
public class UserVisitor : Visitor { public override void FORM_OnLoad() { }
public override void button_OnClick() { Console.WriteLine("UserVisitor:" + base.FORM.button.ToString()); } }
public class AdminVisitor : Visitor {
public override void FORM_OnLoad() { }
public override void button_OnClick() { Console.WriteLine("AdminVisitor:" + base.FORM.button.ToString()); } }