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

Facade-Appearance

** Table of Contents ** When a client uses a subsystem, it is a pattern that combines complicated interfaces into only the minimum required interfaces. If you do not want to customize the subsystem, make it easier to use by focusing only on the interface to which the Facade pattern is applied.

The point is to weaken the coupling between the client and the subsystem. It is easy to change the subsystem because the coupling is weak.

Purpose

Give one unified interface to multiple interfaces existing in the subsystem. The Facade pattern defines a high-level interface that facilitates the use of subsystems.

Component

-A class that defines the minimum interface required for Facade clients to use subsystems. -Classes in the subsystem The subsystem to be used

I wonder why only the classes in the subsystem among the components are not properly named ...

Implementation

Just create a new window, but write sample code.

Classes in subsystems Used subsystems

Various printer class interfaces

Printer.kt


package facade

class Printer {
    fun startUp() { print("Start-up") }
    fun shutDown() { print("End") }
    fun setToner(color: String) { print("Toner${color}Set to color.") }
    fun addPaper(num: Int) { print("Paper${num}I will add one.") }
    fun login(name: String) { print("User${name}Has logged in.") }
    fun printOut(num: Int) { print("${num}Print a sheet.") }
}

Since all the client needs to operate the printer is to print, we will prepare a window for that.

A class that defines the minimum interface required for a Facade client to use a subsystem

Limited interface

FacadePrinter.kt


package facade

class FacadePrinter {
    private val printer = Printer()

    fun printOut(num: Int) {
        printer.printOut(num)
    }
}

client

Client.kt


package facade

class Client {
    init {
        val printer = FacadePrinter()
        printer.printOut(15)

        //No other method available
        // printer.startUp()
        // pritner.shutDown()
    }
}

Output result

The result is not so big

[output]
Print 15 sheets.

Somehow, I feel that my perception is wrong. Please point out in the comments.

Recommended Posts

[Gang of Four] Design pattern learning --Facade
[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 --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 #Facade
Gang of Four (GoF) Patterns in Python
Learn the design pattern "Facade" with Python
Design Pattern #Builder
Design Pattern #Adapter
Pattern recognition learning in video Part 1 Field of Pattern Recognition
Design Pattern #Observer
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
Facade pattern in Java
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"