** Table of Contents **
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.
-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
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 relationship
Orhas-a relationship
It 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