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

Adapter --Person to adapt [thing]

** Table of Contents **

Purpose

Convert an interface of one class to another interface that the client wants. The Adapter pattern allows you to combine classes that are incompatible with each other in the interface.

Component

-Define the interface of the class after the target combination. ・ Client user class ・ Adaptee Class you want to combine -Class created by combining the Adapter Adaptee class

Implementation

When I read the purpose and thought, "I can do that," it was like having a class inherit multiple classes and putting together various classes. The sample code of the GoF book is basically written in C ++, so it seems that it cannot be realized with kotlin (original java). Even though it is the first pattern related to the structure.

After a lot of research, there were some pages that realized (or are you trying to) the adapter pattern in java. is-a relationshipOrhas-a relationshipIt can be realized by two things. It was written on every page, but is it really an adapter pattern ...? I think.

For example, the `TextView class`, `Rect class` and `ColorBackground class` are included in the standard toolkit, but the system you are about to create can have rounded corners. I'm aware that this pattern is used when you need a text view that allows you to change the background color !!!

If it can be realized with java, the following class can be created.

ColorRectTextView.java



public class ColorRectTextView extends TextView, Rect, ColorBackground {
    //Method to round the corners and change the background color
}

However, java does not allow multiple inheritance. If this is realized by is-a relation `` `or `` has-a relation

IsAColorRectTextView.java


public class IsAColorRectTextView extends TextView implements RectInterface, ColorBackgroundInterface {
    //Implement the methods defined by RectInterface and ColorBackgroundInterface
}

Shape like

HasAColorRectTextView.java


public class HasAColorRectTextView extends TextView {
    private Rect rect;
    private ColorBackground colorBackGround;

    public HasAColorRectTextView(Rect rect, ColorBackground colorBackGround) {
        this.rect = rect;
        this.colorBackGround = colorBackGround;
    }

    public void rectMethod() {
        rect.rectMethod()
    }

    public void colorBackGroundMethod() {
        colorBackGround.colorBackGroundMethod()
    }
}

It looks like a mere inheritance and delegation.

Recommended Posts

[Gang of Four] Design pattern learning --Adapter
[Gang of Four] Design pattern learning
[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 --Facade
[Gang of Four] Design pattern learning --Composite
[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 --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 "Adapter" in Python
Design Pattern #Builder
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 wrote a design pattern in kotlin Adapter edition
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"