Design - Visitor

using System;
using System.Web;
using System.Collections.Generic;


public partial class DesignPattern_Visitor : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        ObjectStructure o = new ObjectStructure();
        o.Add(new Manager("时"));
        o.Add(new Employee("赵"));
        o.Add(new Employee("陈"));
        o.Add(new Employee("黄"));
        o.Add(new Manager("晓"));
        o.accountWage = new AccountWage2007Year();
        o.Visitor();

    }
}


public interface Person
{
    void Accept(Visitor v);
}

public class Manager : Person
{
    public string Name;
    public int BaseWage = 5000;
    public int ProductBonus = 10000;
    public int ProductManage = 500;
    public Manager(string Name)
    {
        this.Name = Name;
    }
    public void Accept(Visitor v)
    {
        HttpContext.Current.Response.Write("<br>"+this.Name + "项目经理的工资为基础工资+项目提成+项目奖金=" + v.AccountManage(this));
    }
}

public class Employee : Person
{
    public string Name;
    public int BaseWage = 5000;
    public int KaoQing = 100;
    public Employee(string Name)
    {
        this.Name = Name;
    }
    public void Accept(Visitor v)
    {
        HttpContext.Current.Response.Write("<br>"+this.Name + "普通员工的工资为基础工资+考勤=" + v.AccountEmployee(this));
    }
}

public interface Visitor
{
    string AccountEmployee(Employee employee);
    string AccountManage(Manager manager);
}

public class AccountWage2007Year : Visitor// 2007年时候计算工资
{
    public string AccountEmployee(Employee employee)
    {
        int Total = employee.BaseWage + employee.KaoQing + 2007;
        return Total.ToString();
    }
    public string AccountManage(Manager manager)
    {
        int Total = manager.BaseWage + manager.ProductBonus + manager.ProductManage + 2007;
        return Total.ToString();
    }
}

public class ObjectStructure
{
    public List<Person> ListPerson = new List<Person>();

    public Visitor accountWage;
    public void Add(Person p)
    {
        ListPerson.Add(p);
    }
    public void Visitor()
    {
        foreach (Person p in ListPerson)
        {
            p.Accept(accountWage);
        }
    }
}

posted @ 2008-11-10 17:34  游侠_1  阅读(185)  评论(0编辑  收藏  举报