** Table of Contents ** ** Assemble the class with a wooden structure. It seems to be a pattern consisting of objects called composite and leaf, as in **.
Part-Assemble objects into a tree to represent the entire hierarchy. The Composite pattern allows clients to treat individual objects and their composites uniformly.
· Component An abstract class of roots, nodes and leaves ・ Leaf leaf ・ Composite root or node ・ Client user
When you hear the word "tree structure", you can think of a directory tree. Therefore, implement a program that can manage and output the directory tree.
The directory is composite and the file is leaf.
directory and file interface
Element.kt
package composite
interface Element {
    enum class ElementType(val type: String) {
        DIRECTORY("Direcotry"),
        FILE("File")
    }
    fun getType(): ElementType
    fun getName(): String
}
directory abstract class
Have an elementList so that you can manage the Elements under your control.
AbstractDirectory.kt
package composite
abstract class AbstractDirectory: Element {
    var elementList: MutableList<Element> = mutableListOf()
    abstract fun addElement(element: Element)
}
directory concrete class
Directory.kt
package composite
class Directory(private val name: String): AbstractDirectory() {
    override fun getType(): Element.ElementType {
        return Element.ElementType.DIRECTORY
    }
    override fun addElement(element: Element) {
        elementList.add(element)
    }
    override fun getName(): String {
        return name
    }
}
file concrete class
File.kt
package composite
class File(private val name: String): Element {
    override fun getType(): Element.ElementType {
        return Element.ElementType.FILE
    }
    override fun getName(): String {
        return name
    }
}
The person who manages the directory tree
DirectoryManager.kt
package composite
class DirectoryManager {
    init {
        //Root folder
        val rootDirectory = Directory("/")
        rootDirectory.addElement(File("sample.config"))
        rootDirectory.addElement(File("gof.env"))
        //Folder for iOS apps
        val iosDirectory = Directory("iOS")
        val iosAppDirectory = Directory("GofiOSApp")
        iosAppDirectory.addElement(File("xcode.project"))
        val iosAppSourceDirectory = Directory("Source")
        iosAppSourceDirectory.addElement(File("hoge.swift"))
        iosAppSourceDirectory.addElement(File("fuga.swift"))
        iosDirectory.addElement(iosAppDirectory)
        iosAppDirectory.addElement(iosAppSourceDirectory)
        rootDirectory.addElement(iosDirectory)
        //Folder for Android apps
        val androidDirectory = Directory("AndroidOS")
        val androidAppDirectory = Directory("GofAndroidApp")
        androidAppDirectory.addElement(File("AndroidManifest.xml"))
        val androidAppSourceDirectory = Directory("Source")
        androidAppSourceDirectory.addElement(File("moge.kt"))
        androidAppSourceDirectory.addElement(File("unbaba.kt"))
        androidDirectory.addElement(androidAppDirectory)
        androidAppDirectory.addElement(androidAppSourceDirectory)
        rootDirectory.addElement(androidDirectory)
        show(rootDirectory, 0)
    }
    private fun show(element: Element, indent: Int) {
        if (element is Directory) {
            println("${"----".repeat(indent)}【${element.getType().type}】${element.getName()}")
            element.elementList.forEach {
                show(it, indent + 1)
            }
        } else {
            println("${"----".repeat(indent)}【${element.getType().type}】${element.getName()}")
        }
    }
}
This completes the directory tree using the composite pattern.
[output]
【Direcotry】/
----【File】sample.config
----【File】gof.env
----【Direcotry】iOS
--------【Direcotry】GofiOSApp
------------【File】xcode.project
------------【Direcotry】Source
----------------【File】hoge.swift
----------------【File】fuga.swift
----【Direcotry】AndroidOS
--------【Direcotry】GofAndroidApp
------------【File】AndroidManifest.xml
------------【Direcotry】Source
----------------【File】moge.kt
----------------【File】unbaba.kt
If you want to duplicate the / iOS / GofIosApp / Source folder to the same hierarchy, add ```iosAppDirectory.addElement (iosAppSourceDirectory) `` `.
[output]
【Direcotry】/
----【File】sample.config
----【File】gof.env
----【Direcotry】iOS
--------【Direcotry】GofiOSApp
------------【File】xcode.project
------------【Direcotry】Source
----------------【File】hoge.swift
----------------【File】fuga.swift
------------【Direcotry】Source
----------------【File】hoge.swift
----------------【File】fuga.swift
Abbreviation
You can manipulate the directory freely.
Recommended Posts