[JAVA] [Gang of Four] Design pattern learning --Mediator

Mediator-Intermediary

** Table of Contents ** As the name implies, this pattern is to eliminate the relationships between objects through the mediator class.

I often see specifications such as the button not being enabled unless the text box has a value. This cannot be achieved unless the textbox object knows the button object (and holds it as an instance variable). With such a simple specification, it may not be a problem if it is directly related, but for more complicated functions, a class must know a number of objects. Eliminate it using the intermediary class.

After all, any pattern results in increased orthogonality.

Purpose

Define an object that encapsulates the interaction of a group of objects. The Mediator pattern helps reduce coupling by preventing objects from explicitly referencing each other. This allows the interaction of objects to be changed independently.

Component

Mediator abstract class ・ ConcreteMediator concrete class ・ Colleague Objects that you want to associate with various objects

Implementation

Implements checkboxes, text boxes, and button objects. The text box cannot be entered unless one of the check boxes is checked. The button cannot be pressed unless the check box is checked and the text box is empty.

Mediator intermediary abstract class

Mediator.kt


package mediator

interface Mediator {

    /**
     *Whether the button can be pressed
     */
    fun buttonPress(): Boolean

    /**
     *Whether text box can be entered
     */
    fun inputTextBox(): Boolean
}

ConcreteMediator Concrete class

ConcreteMediator.kt


package mediator

class ConcreteMediator: Mediator {

    var checkBox: CheckBox? = null
    var textBox: TextBox? = null
    var button: Button? = null

    override fun buttonPress(): Boolean {
        return checkBox?.sex != null && textBox?.value?.isNotEmpty() ?: false
    }

    override fun inputTextBox(): Boolean {
        return checkBox?.sex != null
    }

}

Colleague Objects you want to associate with various objects

Abstract class

Colleague.kt


package mediator

interface Colleague {
    enum class Type {
        CheckBox,
        TextBox,
        Button
    }

    val type: Type
    val mediator: Mediator
}

Concrete class ·Checkbox

CheckBox.kt


package mediator

    class CheckBox(override val mediator: Mediator): Colleague {
    override val type = Colleague.Type.CheckBox
    enum class Sex {
        Male,
        Female
    }

    var sex: Sex? = null

    fun select(sex: Sex) {
        this.sex = sex
    }
}

·Text box

TextBox.kt


package mediator

class TextBox(override val mediator: Mediator): Colleague {
    override val type = Colleague.Type.TextBox
    var value = ""

    fun input(value: String) {
        if (mediator.inputTextBox()) {
            this.value = value
            println("Text box input successful")
        } else {
            println("Text box input failure")
        }
    }
}

·button

Button.kt


package mediator

class Button(override val mediator: Mediator): Colleague {
    override val type = Colleague.Type.Button

    fun press() {
        if (mediator.buttonPress()) {
            println("Button press successful")
        } else {
            println("Button press failure")
        }
    }
}

People who use

Client.kt


package mediator

class Client {
    init {
        val mediator = ConcreteMediator()
        val checkBox = CheckBox(mediator)
        val textBox = TextBox(mediator)
        val button = Button(mediator)

        mediator.checkBox = checkBox
        mediator.textBox = textBox
        mediator.button = button

        //Text box input Failed because the check box is not selected
        textBox.input("Yamada Taro")

        //Checkbox selection
        checkBox.select(CheckBox.Sex.Female)

        //Button press Failure because no text box has been entered
        button.press()

        //Text box input successful!!!
        textBox.input("Hanako Yamada")

        //Button press successful!!!
        button.press()
    }
}

Output result

[out-put]
Text box input failure
Button press failure
Text box input successful
Successful button press

that's all

Recommended Posts

[Gang of Four] Design pattern learning --Mediator
[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 --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 --Factory Method
[Gang of Four] Design pattern learning --Chain of Responsibility
[Gang of Four] Design pattern learning --Template Method
Learn the design pattern "Mediator" in Python
Gang of Four (GoF) Patterns 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
I studied about design patterns (personal memo) Part 6 (Chain of Responsibility pattern, Facade pattern, Mediator pattern)
Learn the design pattern "Chain of Responsibility" in Python
Design Pattern #Factory Method
Deep learning 1 Practice of deep learning
Design Pattern #Template Method
GoF design pattern is just an "introduction of abstraction layer"