【设计模式】之 Visitor 观察者模式

本文深入探讨了访问者模式的概念、实现方式及其在员工管理中的应用,通过具体实例展示了如何使用访问者模式来调整员工的收入和休假天数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace DesignFactory.Visitor
{
    /// <summary>
    /// 访问者模式
    /// </summary>
    class VisitorPattern
    {
    }

   abstract class Element
   {
   abstract public void Accept(Visitor visitor);
   }

    abstract class Visitor
    {
        abstract public void Visit(Element element);       
    }

    class Employee : Element
    {
        public Employee(string name, double income, int vacationDays)
        {
            this.name = name;
            this.income = income;
            this.vacationDays = vacationDays;
        }

        public override void Accept(Visitor visitor)
        {
            visitor.Visit(this);
        }


        private string name;

        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        private double income;

        public double Income
        {
            get { return income; }
            set { income = value; }
        }


        private int vacationDays;

        public int VacationDays
        {
            get { return vacationDays; }
            set { vacationDays = value; }
        }
        
        


    }

    class IncomeVisitor : Visitor
    {
        public override void Visit(Element element)
        {
            Employee employee = (Employee)element;

            employee.Income *= 1.10;
            Console.WriteLine("{0}'s new income:{1:C}", employee.Name, employee.Income);
        }
    }

    class VacationVisitor : Visitor
    {
        public override void Visit(Element element)
        {
            Employee employee = (Employee)element;

            employee.VacationDays +=3;
            Console.WriteLine("{0}'s new vacation days:{1:C}", employee.Name, employee.VacationDays);
        }
    }

    class Employees
    {
        private ArrayList employees = new ArrayList();
        public void Attach(Employee employee)
        {
            employees.Add(employee);
        }

        public void Detach(Employee employee)
        {
            employees.Remove(employee);
        }

        public void Accept(Visitor visitor)
        {
            foreach (Employee e in employees)
            {
                e.Accept(visitor);
            }
        }
    }


    public class VisitorApp
    {
        public static void Main(string[] args)
        {
            Employees e = new Employees();
            e.Attach(new Employee("Hank", 25000.0, 14));
            e.Attach(new Employee("Elly", 35000.0, 16));
            e.Attach(new Employee("Dick", 45000.0, 21));


            IncomeVisitor v1 = new IncomeVisitor();
            VacationVisitor v2 = new VacationVisitor();


            e.Accept(v1);
            e.Accept(v2);
        }
    }



}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值