zoukankan      html  css  js  c++  java
  • 访问者模式

    代码
    using System;
    using System.Collections;
    using System.Collections.Generic;

    abstract class Action
    {
        
    public abstract void GetManConclusion(Man man);
        
    public abstract void GetWomanConclusion(Woman woman);
    }

    class Success:Action
    {
        
    public override void GetManConclusion(Man man)
        {
            Console.WriteLine(
    "{0}{1}时,背后多半有一个伟大的女人。", man.GetType().Name, this.GetType().Name);
        }
        
        
    public override void GetWomanConclusion(Woman woman)
        {
            Console.WriteLine(
    "{0}{1}时,背后多半有一个伟大的女人。", woman.GetType().Name, this.GetType().Name);
        }
    }



    abstract class Person
    {
        
    public abstract void Accept(Action action);
    }

    class Woman:Person
    {
        
    public override void Accept(Action action)
        {
            action.GetWomanConclusion(
    this);
        }
    }
    class Man:Person
    {
        
    public override void Accept(Action action)
        {
            action.GetManConclusion(
    this);
        }
    }

    class ObjectStructure
    {
        
    private IList<Person> elements=new List<Person>();
        
        
    public void Add(Person element)
        {
            elements.Add(element);
        }
        
        
    public void Detach(Person element)
        {
            elements.Remove(element);
        }
        
        
    public void Display(Action voisitor)
        {
            
    foreach(Person e in elements)
            {
                e.Accept(voisitor);
            }
        }
    }



    public class MyClass
    {
        
    public static void Main()
        {
            ObjectStructure o
    =new ObjectStructure();
            o.Add(
    new Man());
            
            Success v1
    =new Success();
            o.Display(v1);
            
            Console.ReadLine();
        }
    }


  • 相关阅读:
    OpenLDAP备份和恢复
    OpenLDAP搭建部署
    Python正则表达式
    ansible学习
    Jenkins学习
    docker学习2
    让阿里告诉你, iOS开发者为什么要学 Flutter !
    用UIKit和UIView在视图上执行iOS动画
    iOS开发如何面对疫情过后的面试高峰期 !
    如何写好一个UITableView
  • 原文地址:https://www.cnblogs.com/mikechang/p/1712634.html
Copyright © 2011-2022 走看看