[JAVA] [Gang of Four] Design Pattern Learning --Decorator

Decorator-Decorator

** Table of Contents ** Suddenly, the story goes aside, but when I read books on design patterns and object-oriented programming, I often hear the words ** responsibility ** and ** responsibility ** for objects. This points to what you have to do with the object. There is also the principle of single responsibility, which states that there should be one reason to change classes.

For example, the following classes have two reasons to modify. -** Screen transition ** I want to add processing → Reason for change related to screen -** Subtraction ** Processing added → Reason for change regarding calculation

Screen calculation.kt


interface screen calculation{
fun drawing(): Boolean
fun addition(num1: Int, num2: Int): Int
}

According to the principle of single responsibility, it should be separated as follows.

screen.kt


interface screen{
fun drawing(): Boolean
fun screen transition(): Boolean
}

Calculation.kt


interface calculation{
fun addition(num1: Int, num2: Int): Int
fun subtraction(num2: Int, num2: Int): Int
}

I understand that this principle of single responsibility is also an idea to respond flexibly to changes. That's all for the side road story.

Then, the main subject is ** to dynamically add responsibility ** to the purpose, so it is a pattern that adds functions to objects (instances) at arbitrary timing without using inheritance.

Purpose

Dynamically add responsibility to an object. The Decorator pattern provides a more flexible extension method than subclassing.

Component

· An abstract class that defines an interface that can dynamically add Component responsibilities -ConcreteComponent Component class concrete class An abstract class that defines an interface that adds responsibility to the Decorator Component class -ConcreteDecorator Decorator class concrete class

Implementation

Implement a program that can add various functions at the timing of creating a text view instance.

Component An abstract class that defines an interface that allows you to dynamically add responsibilities

An abstract class that defines an interface that adds responsibility to the Decorator Component class

View component interface

ViewComponent.kt


package decorator

interface ViewComponent {
    fun draw()
}

ConcreteComponent Component class concrete class

Text view

TextView.kt


package decorator

class TextView: ViewComponent {
    override fun draw() {
        print("[Text view]")
    }
}

ConcreteDecorator Decorator class concrete class

Decorator to cast shadows

ShadowDecorator.kt


package decorator

class ShadowDecorator(viewComponent: ViewComponent): ViewComponent {
    val viewComponent = viewComponent

    override fun draw() {
        viewComponent.draw()
        addShadow()
    }

    private fun addShadow() {
        print(": With shadow")
    }
}

Decorator to scroll

ScrollDecorator.kt


package decorator

class ScrollDecorator(viewComponent: ViewComponent): ViewComponent {
    val viewComponent = viewComponent

    override fun draw() {
        viewComponent.draw()
        addScroll()
    }

    private fun addScroll() {
        print(": Scrollable")
    }

}

All the parts are ready. Let's use it.

** User **

ComponentManager.kt


package decorator

class ComponentManager {
    init {
        //Scrollable shaded text view
        val scrollShadowTextView = ScrollDecorator(ShadowDecorator(TextView()))
        //Scrollable text view
        val scrollTextView = ScrollDecorator(TextView())

        //drawing
        scrollShadowTextView.draw()
        print("\n")
        scrollTextView.draw()
    }
}

Output result

When executed, it will be as follows.

[output]
[Text view]: Shadowed: Scrollable
[Text view]: Scrollable

Recommended Posts

[Gang of Four] Design Pattern Learning --Decorator
[Gang of Four] Design pattern learning
[Gang of Four] Design pattern learning --Singleton
[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 --Composite
[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
Design Pattern #Decorator
Learn the design pattern "Decorator" in Python
Gang of Four (GoF) Patterns in Python
Design Pattern #Builder
Design Pattern #Adapter
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
Design Pattern #Factory Method
Deep learning 1 Practice of deep learning
Decorator pattern in Java
Design Pattern #Template Method
GoF design pattern is just an "introduction of abstraction layer"