[GO] [Gang of Four] Design pattern learning --Composite

Composite-Composite

** 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 **.

Purpose

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

· Component An abstract class of roots, nodes and leaves ・ Leaf leaf ・ Composite root or node ・ Client user

Implementation

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.

Component An abstract class of roots, nodes and leaves

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
}

Composite Root or node

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
    }
}

Leaf leaf

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
    }
}

Client user

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 result

[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

[Gang of Four] Design pattern learning --Composite
[Gang of Four] Design pattern learning
[Gang of Four] Design pattern learning --Singleton
[Gang of Four] Design Pattern Learning --Decorator
[Gang of Four] Design pattern learning --Visitor
[Gang of Four] Design pattern learning --Mediator
[Gang of Four] Design pattern learning --Iterator
[Gang of Four] Design pattern learning --Facade
[Gang of Four] Design pattern learning --Prototype
[Gang of Four] Design pattern learning --Memento
[Gang of Four] Design pattern learning --State
[Gang of Four] Design pattern learning --Interpreter
[Gang of Four] Design pattern learning --Builder
[Gang of Four] Design pattern learning --Bridge
[Gang of Four] Design pattern learning --Proxy
[Gang of Four] Design pattern learning --Strategy
[Gang of Four] Design pattern learning --Adapter
[Gang of Four] Design pattern learning --Observer
[Gang of Four] Design pattern learning --Command
[Gang of Four] Design pattern learning --Fly Weight
[Gang of Four] Design pattern learning --Abstract Factory
[Gang of Four] Design pattern learning --Factory Method
[Gang of Four] Design pattern learning --Chain of Responsibility
[Gang of Four] Design pattern learning --Template Method
Gang of Four (GoF) Patterns in Python
Learn the design pattern "Composite" in Python
Design Pattern #Builder
Design Pattern #Adapter
Design Pattern #Decorator
Pattern recognition learning in video Part 1 Field of Pattern Recognition
Design Pattern #Observer
Design Pattern #Facade
Design Pattern #Strategy
Design Pattern #Singleton
Design Pattern #Proxy
Learn the design pattern "Chain of Responsibility" in Python
Deep learning 1 Practice of deep learning
Design Pattern #Template Method
GoF design pattern is just an "introduction of abstraction layer"