• CSharp设计模式读书笔记(24):访问者模式(学习难度:★★★★☆,使用频率:★☆☆☆☆)


    模式角色与结构:

    示例代码:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace CSharp.DesignPattern.VisitorPattern
    {
        class Program
        {
            static void Main(string[] args)
            {
                ObjectStructure list = new ObjectStructure();
                list.AddEmployee(new FulltimeEmployee("Mike", 2000));
                list.AddEmployee(new FulltimeEmployee("Tony", 3000));
                list.AddEmployee(new ParttimeEmployee("Nancen", 800));
                list.AddEmployee(new ParttimeEmployee("Bibo", 500));
    
                FinanceVisitor fVisitor = new FinanceVisitor();
                HrVisitor hVisitor = new HrVisitor();
    
                list.Accept(fVisitor);
                list.Accept(hVisitor);
            }
        }
    
        class ObjectStructure
        {
            public void Accept(Visitor visitor)
            {
                foreach (IEmployee element in employees)
                {
                    element.Accept(visitor);
                }
            }
    
            public void AddEmployee(IEmployee employee)
            {
                employees.Add(employee);
            }
    
            public void RemoveEmployee(IEmployee employee)
            {
                employees.Remove(employee);
            }
    
            private List<IEmployee> employees = new List<IEmployee>();
        }
    
        class Visitor
        {
            public abstract void Visit(FulltimeEmployee fulltime);
            public abstract void Visit(ParttimeEmployee contract);
        }
    
        class FinanceVisitor : Visitor
        {
            public void Visit(FulltimeEmployee fulltime)
            {
                Console.Write("The wage of fulltime is ..."); 
            }
            public void Visit(ParttimeEmployee contract)
            {
                Console.Write("The wage of parttime is ..."); 
            }
        }
    
        class HrVisitor : Visitor
        {
            public void Visit(FulltimeEmployee fulltime)
            {
                Console.Write("The worktime of fulltime is ...");
                Console.Write("The overtime of fulltime is ...");
                Console.Write("The timeoff of fulltime is ..."); 
            }
            public void Visit(ParttimeEmployee contract)
            {
                Console.Write("The worktime of fulltime is ..."); 
            }
        }
    
        interface IEmployee
        {
            public void Accept(Visitor visitor)
            { }
    
            String Name { set; get; }
            Int32 Wage { set; get; }
        }
    
        class FulltimeEmployee : IEmployee
        {
            public FulltimeEmployee(String name, Int32 wage)
            {
                this._name = name;
                this._wage = wage;
            }
    
            public void Accept(Visitor visitor)
            {
                visitor.Visit(this);
            }
    
            public void OperationFulltime()
            { }
    
            public string Name
            {
                get { return _name; }
                set { _name = value; }
            }
    
            public int Wage
            {
                get { return _wage; }
                set { _wage = value; }
            }
    
            private String _name;
            private Int32 _wage;
        }
    
        class ParttimeEmployee : IEmployee
        {
            public ParttimeEmployee(String name, Int32 wage)
            {
                this._name = name;
                this._wage = wage;
            }
    
            public void Accept(Visitor visitor)
            {
                visitor.Visit(this);
            }
    
            public void OperationParttime()
            { }
    
            public string Name
            {
                get { return _name; }
                set { _name = value; }
            }
    
            public int Wage
            {
                get { return _wage; }
                set { _wage = value; }
            }
    
            private String _name;
            private Int32 _wage;
        }
    }
  • 相关阅读:
    nth-child与nth-of-type
    改变事件绑定的this的问题
    瀑布流的一些CSS实现方式
    事件捕获与冒泡的再探
    为学
    ECharts导出word 图表模糊失真
    垂直对齐:vertical-align:super属性
    Vuex- Action的 { commit } {commit}是什么写法
    修改对象中的属性名
    echarts 角度渐变环形图心得
  • 原文地址:https://www.cnblogs.com/thlzhf/p/3993810.html
Copyright © 2020-2023  润新知