To learn the concept of Interface and the reusability of objects, which are important in object orientation ["Introduction to design patterns learned in Java language"](https://www.amazon.co.jp/%E5%A2%97% E8% A3% 9C% E6% 94% B9% E8% A8% 82% E7% 89% 88Java% E8% A8% 80% E8% AA% 9E% E3% 81% A7% E5% AD% A6% E3% 81% B6% E3% 83% 87% E3% 82% B6% E3% 82% A4% E3% 83% B3% E3% 83% 91% E3% 82% BF% E3% 83% BC% E3% 83% B3% E5% 85% A5% E9% 96% 80-% E7% B5% 90% E5% 9F% 8E-% E6% B5% A9 / dp / 4797327030 / ref = sr_1_1? __mk_ja_JP =% E3% 82% AB % E3% 82% BF% E3% 82% AB% E3% 83% 8A & keywords = java% E8% A8% 80% E8% AA% 9E% E3% 81% A7% E5% AD% A6% E3% 81% B6 % E3% 83% 87% E3% 82% B6% E3% 82% A4% E3% 83% B3% E3% 83% 91% E3% 82% BF% E3% 83% BC% E3% 83% B3% E5 I learned about% 85% A5% E9% 96% 80 & qid = 1559563427 & s = gateway & sr = 8-1) and decided to write in Java and then in kotlin. This time I will write about Adapter.
It is a design pattern that bridges the gap between what is already provided and what is needed, and is also called the Wrapper pattern. A pattern of roles that wraps the original class and meets the required specifications.
This class is assumed to be the originally provided class. When inheriting in kotlin, class is treated as java final, so it is necessary to open and inherit, so specify open before class definition reference: https://www.atmarkit.co.jp/ait/articles/1804/24/news008.html
Era.java
class Era{
private int year;
public Era(int year) {
this.year = year;
}
public int yearToReiwa() {
return year - 2018;
}
public int yearToHeisei() {
return year - 1988;
}
}
Era.kt
open class Era(private val year: Int){
fun yearToReiwa(): Int = this.year - 2018
fun yearToHeisei(): Int = this.year - 1988
}
This is an interface that meets the requirements required as a specification. In kolin, use Unit when specifying void in Java
Print.java
interface Print{
public abstract void printAsReiwa();
public abstract void printAsHeisei();
}
Print.kt
interface Print{
fun printAsReiwa(): Unit
fun printAsHeisei(): Unit
}
This class will be an Adapter and will wrap the Era class to meet the required requirements. The point is that the required specifications can be implemented without changing the implementation of the Era class. When inheriting, specify like ": Era", and when calling the inherited constructor, you can call it with ": Era (year)"
PrintEra.java
class PrintEra extends Era implements Print{
public PrintEra(int year) {
super(year);
}
public void printAsHeisei() {
System.out.println("Heisei" + yearToHeisei() + "Year");
}
public void printAsReiwa() {
if(yearToReiwa() == 1) System.out.println("First year of Reiwa");
else System.out.println("Reiwa" + yearToReiwa() + "Year");
}
}
PrintEra.kt
class PrintEra(private val year: Int) :Era(year), Print{
override fun printAsHeisei(): Unit = println("Heisei" + yearToHeisei() + "Year")
override fun printAsReiwa() {
if (yearToReiwa() == 1) println("First year of Reiwa") else println("Reiwa" + yearToReiwa() + "Year")
}
}
This is the Main class that actually operates the above class.
AdapterSample.java
public class AdapterSample {
public static void main(String[] args) {
Print p = new PrintEra(2019);
p.printAsHeisei();
p.printAsReiwa();
}
}
AdapterSample.kt
fun main(args: Array<String>){
val p: Print = PrintEra(2019)
p.printAsHeisei()
p.printAsReiwa()
}
Execution result
2019
First year of Reiwa
It was very easy to read and understand by referring to the following.
Introduction to Kotlin for Java Programmers
Recommended Posts