我学会了,组合模式

简介: 组合模式属于结构型模式,这个类型的设计模式总结出了 类、对象组合后的经典结构,将类、对象的结构和使用解耦了,花式的去借用对象。

前言

组合模式属于结构型模式,这个类型的设计模式总结出了 类、对象组合后的经典结构,将类、对象的结构和使用解耦了,花式的去借用对象。

组合模式

使用场景:当你遇到 局部到整体的情况时,比如文件目录与子文件、子目录这样的有层次的树结构,就可以使用组合模式了。

理解:类、对象的结构和使用解耦,通过嵌套的结构让对象与对象之间拥有了层次关系,这种层次关系也支持动态增加或者减少。正常情况下应该使用一个class来实现,其实只需要一个FileNode即可。但是我将FIle和Dir分开了,我觉得需要拆分一下,这样就可以拥有各自互不干扰的行为,但是就目前来看,也造成了冗余代码的产生,不过你应该也发现了 这种结构有缺陷,比如File不应该有子文件,但是我们还是让它拥有了addChild方法,虽然我们直接将它抛异常。

namespace struct_mode_06 {

    interface IFile {

        list: Array<IFile>
        parent?: IFile

        fileName: string
        fileSize: string
        fileType: string
        isDir: boolean

        getPath(): string
        getCount(): number

        // 从下往上
        setParent(parent: IFile): IFile
        
        // 从下往上
        addChild(children: IFile): IFile

    }

    class File implements IFile {
        list: IFile[];
        fileName: string;
        fileSize: string;
        fileType: string;
        isDir: boolean;
        parent?: IFile;

        constructor(fileName: string, fileSize: string, fileType: string, parent?: IFile) {
            this.fileName = fileName
            this.fileSize = fileSize
            this.fileType = fileType
            this.isDir = false

            this.list = new Array()
            if (parent) {
                this.parent = parent
            }
        }

        setParent(parent: IFile): IFile {

            if (!parent.list.includes(this)) {
                parent.list.push(this)
                this.parent = parent
            } else {
                throw new Error("已经有父亲了");
            }

            return this
        }

        addChild(children: IFile): IFile {
            throw new Error("无法追加");
        }

        getPath(): string {
            let parent = this.parent
            let pathStack = [this.fileName]
            while (parent) {
                pathStack.unshift(parent.fileName)
                parent = parent.parent
            }

            return pathStack.join('/');
        }

        getCount(): number {
            return 1
        }
    }

    class Dir implements IFile {
        list: IFile[];
        fileName: string;
        fileSize: string;
        fileType: string;
        isDir: boolean;
        parent?: IFile;

        constructor(fileName: string, fileSize: string, fileType: string, parent?: IFile) {
            this.fileName = fileName
            this.fileSize = fileSize
            this.fileType = fileType
            this.isDir = true

            this.list = new Array()

            if (parent) {
                this.parent = parent
            }
        }
        setParent(parent: IFile): IFile {

            if (!parent.list.includes(this)) {
                parent.list.push(this)
                this.parent = parent
            } else {
                throw new Error("已经有父亲了");
            }

            return this
        }
        addChild(children: IFile): IFile {
            if (!this.list.includes(children)) {
                this.list.push(children)
                children.parent = this
            } else {
                throw new Error("已经有儿子了");
            }

            return this
        }

        getPath(): string {
            let parent = this.parent
            let pathStack = [this.fileName]
            while (parent) {
                pathStack.unshift(parent.fileName)
                parent = parent.parent
            }

            return pathStack.join('/');
        }

        getCount(): number {
            let list = this.list

            const result = getSubCount(list, list.length)

            function getSubCount(ls: Array<IFile>, count: number) {
                ls.filter(item => item.isDir).forEach(item => {
                    count += getSubCount(item.list, item.list.length)
                })

                return count
            }

            return result
        }
    }

    const dir = new Dir('DIR', '0KB', '')
    const dir2 = new Dir('DIR--2', '0KB', '')
    const dir3 = new Dir('DIR---3', '0KB', '')
    const file = new File('test.rar', '9KB', 'rar')
    const file2 = new File('test2.rar', '29KB', 'rar')
    const file3 = new File('test3.rar', '19KB', 'rar')

    dir.addChild(file)
    dir.addChild(dir2)
    dir2.addChild(file2)
    dir2.addChild(dir3)
    dir3.addChild(file3)

    console.log(file3.getPath(), dir.getCount())
}
目录
相关文章
|
Hadoop
使用ambari快速部署Hadoop集群
Ambari 自身也是一个分布式架构的软件,主要由两部分组成:Ambari Server 和 Ambari Agent。我们可以通过 Ambari Server 通知 Ambari Agent 安装对应的软件;甚至连Ambari Agent我们都可以在Web界面上来进行安装和部署。
3608 0
使用ambari快速部署Hadoop集群
|
存储 Shell Linux
【Shell 命令集合 磁盘维护 】Linux 创建DOS文件系统 mkdosfs命令使用指南
【Shell 命令集合 磁盘维护 】Linux 创建DOS文件系统 mkdosfs命令使用指南
198 2
|
Python
【Python 训练营】N_13 遍历字符串
【Python 训练营】N_13 遍历字符串
84 2
|
XML JSON 缓存
虾皮shopee根据ID取商品详情 API 返回值说明
-- 请求示例 url 默认请求参数已经URL编码处理 curl -i &quot;https://api-gw.onebound.cn/shopee/item_get/?key=&lt;您自己的apiKey&gt;&secret=&lt;您自己的apiSecret&gt;&num_iid=264070136/5637247041&country=.com.my&quot;
|
存储 算法 安全
指针数组
指针数组
98 0
|
传感器 开发框架 运维
物联网碎片化的一些思考
提到物联网遇到的挑战,都会谈到物联网碎片化。
504 17
物联网碎片化的一些思考
|
消息中间件 存储 资源调度
操作系统总结
操作系统总结
195 0
|
JavaScript 前端开发 索引
工作中必会使用的javascript方法
20多个javascript方法,让你js开发事半公办倍
南通:支持中小企业共渡难关,崇川区出台相关扶持政策
南通市崇川区近期发布了关于应对新型冠状病毒感染的肺炎疫情,给予中小微企业相关扶持政策的意见
南通:支持中小企业共渡难关,崇川区出台相关扶持政策