** 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.
Dynamically add responsibility to an object. The Decorator pattern provides a more flexible extension method than subclassing.
· 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
Implement a program that can add various functions at the timing of creating a text view instance.
View component interface
ViewComponent.kt
package decorator
interface ViewComponent {
fun draw()
}
Text view
TextView.kt
package decorator
class TextView: ViewComponent {
override fun draw() {
print("[Text view]")
}
}
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()
}
}
When executed, it will be as follows.
[output]
[Text view]: Shadowed: Scrollable
[Text view]: Scrollable
Recommended Posts