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;
        }
    }
  • 相关阅读:
    转载JGTM' 2004[MVP]有关AOP的三篇精彩文章
    新增Skin
    发表文章的要求
    自定义UserControl的属性为什么不能在设计时显示在属性窗口中
    .Text学习笔记(一)
    访问类的private或internal成员[转载]
    博客园对发表文章的一些要求
    博客园成立了管理团队
    推荐一篇介绍.NET MetaData的文章
    让大家久等了:终于完成了AOP尝鲜系列之第三部[JGTM'2004 [MVP]文章转载]
  • 原文地址:https://www.cnblogs.com/thlzhf/p/3993810.html
Copyright © 2011-2022 走看看