C#设计模式之---组合模式

组合模式(Composite Pattern)

组合模式(Composite Pattern)可以清楚地定义分层次的复杂对象,表示对象的全部或部分层次,使得增加新部件也更容易,它让客户忽略了层次的不同性,而它的结构又是动态的,提供了对象管理的灵活接口。组合模式对于树结构的控制有着神奇的功效,用户对单个对象和组合对象的使用具有一致性。组合模式解耦了客户程序与复杂元素内部结构,从而使客户程序可以像处理简单元素一样来处理复杂元素。也是一种结构型模式。

复制代码
using System;
using System.Text;
using System.Collections.Generic;
namespace ConsoleApplication
{
    //一般每个接口或类都写在单独的.cs文件中
    //本示例为了执行查看方便才写在一起  
   public abstract class FileObject
    {
        public string Name;
        public List<FileObject> filelist = new List<FileObject>();//组合
        public FileObject(string name)
        {
            this.Name = name;
        }
        //打印文件名
        public abstract void PrintName();
        public virtual void Add(FileObject fileobject)
        {
           Console.WriteLine("Add方法未重写");
        }
        public virtual void Remove(FileObject fileobject)
        {
           Console.WriteLine("Add方法未重写");
        }
    }
    public class txtFile:FileObject
    {
        public txtFile(string name)
            : base(name)
        { }
        public override void PrintName()
        {
            Console.WriteLine("文件名:" + base.Name); 
        }
    }
    public class docFile:FileObject
    {
        public docFile(string name):base(name)
        { }
        public override void PrintName()
        {
            Console.WriteLine("文件名:" + base.Name); 
        }
    }
    public class Folder:FileObject
    {
        public Folder(string name):base(name)
        { }
        public override void Add(FileObject fileobject)
        {
            filelist.Add(fileobject);
            Console.WriteLine(base.Name + "中添加了" + fileobject.Name);
        }
        public override void Remove(FileObject fileobject)
        {
            if (filelist.Remove(fileobject))
            {
                Console.WriteLine(base.Name + "中删除了" + fileobject.Name);
            }
            else
            {
                Console.WriteLine("删除失败!");
            }
        }
        public override void PrintName()
        {
            Console.WriteLine("文件名:" + base.Name); 
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            FileObject txt1 = new txtFile("txt1");
            FileObject txt2 = new txtFile("txt2");
            FileObject doc1 = new docFile("doc1");
            FileObject doc2 = new docFile("doc2");
            FileObject folder1 = new Folder("folder1");
            FileObject folder2 = new Folder("folder2");
            txt1.Add(txt2);
            doc1.Add(doc2);
            txt1.Remove(doc1);
            folder1.Add(txt1);
            folder1.Add(doc2);
            folder2.Add(folder1);
            folder2.Add(doc1);
            folder1.Remove(txt1);
            //打印folder1目录下的文件
            Console.WriteLine("-------------");
            foreach (FileObject file in folder1.filelist)
            {
                Console.WriteLine(file.Name);
            }
            //打印folder2目录下的文件
            Console.WriteLine("-------------");
            foreach (FileObject file in folder2.filelist)
            {
                Console.WriteLine(file.Name);
            }
            Console.ReadKey();
        }
    }
}
相关推荐
Zane19944 天前
一个 new 就能创建对象,为什么还要拆出单例、工厂、建造者、原型四种模式?
设计模式
怕浪猫5 天前
一行命令复刻爆款视频,我把 Hypit 从安装跑到了出片
人工智能·设计模式·程序员
Zane19945 天前
函数式编程里的函数,其实不是你天天写的那个函数——三大编程范式的边界在哪
设计模式
Zane19946 天前
策略模式现在该不该上?一次讲清楚过度设计和设计不足怎么找平衡
设计模式
她说..6 天前
常见设计模式-模板方法模式
java·spring·设计模式·springboot
xiaofeiyang1507 天前
第六章 · 桥接 — 三支毛笔,画出九种颜色
设计模式
Shadow(⊙o⊙)7 天前
OTOL设计模式 One Thread One Loop
服务器·网络·设计模式
sarasuki8 天前
如何让LLM 能在半夜偷偷打开网易云呢?
人工智能·设计模式·agent
小王师傅668 天前
【设计模式】装饰模式(四):框架源码实战——从 Java I/O 到 Spring 到 MyBatis
java·设计模式
执明wa8 天前
Android RecyclerView 多类型, 多种 Item
android·xml·开发语言·设计模式·android studio