博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
composite-theory-ns-withShare.cs
阅读量:6184 次
发布时间:2019-06-21

本文共 2619 字,大约阅读时间需要 8 分钟。

  using System;
  using System.Collections.Generic;
  using System.IO;
  using PrototypePattern;
  namespace CompositePattern {
 
    // The Interface                                                                                                                 
    public interface IComponent <T> {
      void Add(IComponent <T> c);
      IComponent <T> Remove(T s);
      string Display(int depth);
      IComponent <T> Find(T s);
      IComponent <T> Share (T s,IComponent <T> home);
      string Name {get; set;}
    }
    // The Composite
    [Serializable()]
    public class Composite <T> : IPrototype <IComponent <T>>, IComponent  <T> {
      List  <IComponent <T>> list;
      public string Name {get; set;}
    
      public Composite (string name)  {
        Name = name;
        list = new List <IComponent <T>> ();
      }
      public void Add(IComponent  <T> c) {
        list.Add(c);
      }
      // Finds the item from a particular point in the structure
      // and returns the composite from which it was removed
      // If not found, return the point as given
      public IComponent <T> Remove(T s) {
        holder = this;
        IComponent <T> p = holder.Find(s);
        if (holder!=null) {
          (holder as Composite<T>).list.Remove(p);
          return holder;
        } else
           return this;
      }
    
      IComponent <T> holder=null;
    
      // Recursively looks for an item
      // Returns its reference or else null
      public IComponent <T>  Find (T s) {
        holder = this;
        if (Name.Equals(s)) return this;
        IComponent <T> found=null;
        foreach (IComponent <T> c in list)  {
          found = c.Find(s);
          if (found!=null)
            break;
        }
        return found;
      }
      
      public IComponent <T> Share (T set, IComponent <T> toHere) {
        IPrototype <IComponent <T>> prototype = this.Find(set) as IPrototype <IComponent <T>>;
        IComponent <T> copy = prototype.DeepCopy() as IComponent<T>;
        toHere.Add(copy);
        return toHere;
      }
        
      // Displays items in a format indicating their level in the composite structure
      public string Display(int depth) {
        String s = new String('-', depth) + "Set "+ Name +  " length :" + list.Count + "\n";
        foreach (IComponent <T> component in list) {
          s += component.Display(depth + 2);
        }
        return s;
      }
    }
    // The Component
    [Serializable()]
    public class Component <T> : IPrototype <IComponent<T>>, IComponent <T> {
      public string Name {get; set;}
      
      public Component (string name)  {
        Name = name;
      }
      public void Add(IComponent <T> c) {
        Console.WriteLine("Cannot add to an item");
      }
      public IComponent <T>  Remove(T s) {
        Console.WriteLine("Cannot remove directly");
        return this;
      }
      public string Display(int depth) {
        return new String('-', depth) + Name+"\n";
      }
    
      public IComponent <T>  Find (T s) {
        if (s.Equals(Name)) return this;
        else
          return null;
      }
      
      public IComponent <T> Share (T set, IComponent  <T>toHere) {
        IPrototype <IComponent <T>> prototype = this.Find(set) as IPrototype <IComponent <T>>;
        IComponent <T> copy = prototype.Clone() as IComponent<T>;
        toHere.Add(copy);
        return toHere;
      }
    }
  }

转载地址:http://atsda.baihongyu.com/

你可能感兴趣的文章
RGW Cache类解析
查看>>
Linux tar 加密解密
查看>>
【springMVC基础】spring获取bean的几种方法
查看>>
Hadoop学习笔记14:./bin/hadoop namenode -format之源码解析
查看>>
SSH两种登陆认证剖析(搜狗面试)
查看>>
解决SyntaxError: invalid syntax
查看>>
创建android程序会出现两个布局文件
查看>>
从plist文件中读取数据
查看>>
MySQL 5.7中添加用户,新建数据库,用户授权,删除用户,修改密码
查看>>
配置rstatd使用LoadRunner监控Linux系统资源
查看>>
python 基于Digngo 开发web程序和处理get、post请求
查看>>
XenServer的架构之Xenopsd组件架构与运行机制
查看>>
Splunk大数据日志分析系统安装、使用
查看>>
Linux下golang+bee环境安装
查看>>
PV、UV、独立 IP的统计
查看>>
Linux系统启动流程和特殊变量及脚本编程(3)
查看>>
10420 - List of Conquests
查看>>
python自动轨迹绘制
查看>>
基于system Center 2012–VMM(virtral machine manager)在线扩容Centos硬盘。
查看>>
matlab-高数 subs 求导数后求值
查看>>