Swift-组合模式

作者: FlyElephant | 来源:发表于2017-06-07 16:25 被阅读174次

    组合模式能够使容器与内容具有一致性,创造出递归结构,将对象组合成树形结构以表示“部分-整体”的层次结构.

    组合模式.png

    Leaf(树叶):表示叶节点对象,叶子节点没有子节点,不能放入其他对象.

    Composite(复合物): 可以存储Leaf对象,也可以存储Composite对象,相当于容器.

    Component : 组合中的对象声明接口, 定义Leaf与Compostite的公共行为接口.

    现实生活中最符合组合模式结构的莫过于文件系统,文件类似于Leaf,文件夹类似于Composite.

    核心代码:

    class Entry {
        
        var parent:Directory?
        var name:String?
        
        convenience init(name:String,parent:Directory?) {
            self.init()
            self.name = name
            self.parent = parent
        }
        
        func addEntry(entry:Entry) {}
        
        func deleteEntry(entry:Entry) {}
        
        func printnEntry() {}
        
        func printEntry(prefix:String) {}
        
    }
    
    class File:Entry {
        
        
        override func printnEntry() {
            print("目录:\(String(describing: self.parent!.name!))/\(String(describing: self.name!))")
        }
        
        override func printEntry(prefix: String) {
             print("目录:\(prefix)/\(String(describing: self.parent!.name!))/\(String(describing: self.name!))")
        }
    }
    
    class Directory: Entry {
        
        var files = [File]()
        var dirs =  [Directory]()
        
        override func addEntry(entry: Entry) {
            if entry is File {
                files.append(entry as! File)
            } else {
                dirs.append(entry as! Directory)
            }
        }
        
         override func deleteEntry(entry:Entry) {
            
            if entry is File {
                for i in 0..<files.count {
                    if entry.name == files[i].name {
                        files.remove(at: i)
                    }
                }
            } else {
                for i in 0..<dirs.count {
                    if entry.name == dirs[i].name {
                        dirs.remove(at: i)
                    }
                }
            }
        }
        
        override func printnEntry() {
            
            for file in files {
                file.printnEntry()
            }
            
            for dir in dirs {
                for file in dir.files {
                    file.printEntry(prefix: self.name!)
                }
            }
        }
        
    }
    

    测试代码:

           let rootDir:Directory = Directory(name: "root", parent: nil)
            
            let file:File = File(name: "study.html", parent: rootDir)
            let file2:File = File(name: "readme.txt", parent: rootDir)
            
            let studyDir:Directory = Directory(name: "study", parent: rootDir)
            let file3:File = File(name: "composite.swift", parent: studyDir)
            let file4:File = File(name: "study.project", parent: studyDir)
            
            rootDir.addEntry(entry: file)
            rootDir.addEntry(entry: file2)
            rootDir.addEntry(entry: studyDir)
            
            studyDir.addEntry(entry: file3)
            studyDir.addEntry(entry: file4)
        
            rootDir.printnEntry()
            
            print("FlyElephant---删除文件夹")
            rootDir.deleteEntry(entry:studyDir)
            rootDir.printnEntry()
            
            print("FlyElephant---删除文件")
            rootDir.deleteEntry(entry:file2)
            rootDir.printnEntry()
    
    FlyElephant.png

    组合模式定义由 Leaf 对象和 Composite 对象组成的类结构,调用简单,添加或删除子部件变得很容易.

    相关文章

      网友评论

        本文标题:Swift-组合模式

        本文链接:https://www.haomeiwen.com/subject/dbemfxtx.html