zoukankan      html  css  js  c++  java
  • 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;
        }
    }
  • 相关阅读:
    转载:C# 文件下载(WebClient)
    文档生成业务的商业机会
    Java代码编译和执行的整个过程
    (转)TransitionManager
    安装了phpmyadmin
    转:jQuery框架学习第一天:开始认识jQuery
    转:Http post和 Get的本质区别
    vim基本命令汇总
    jQuery框架学习第二天:jQuery中万能的选择器
    IE的hasLayout属性
  • 原文地址:https://www.cnblogs.com/thlzhf/p/3993810.html
Copyright © 2011-2022 走看看