[JAVA] I wrote a design pattern in kotlin Adapter edition

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.

What is 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.

Two types of Adapter pattern implementation

  1. Pattern with inheritance in class
  2. Pattern with delegation on instance Two types are introduced, but this time I would like to implement a pattern using inheritance in the first class.

Pattern with inheritance in class

Era class

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
}

Print interface

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
}

PrintEra class

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")
    }
}

Main class

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

Class diagram

image.png

Impressions

reference

It was very easy to read and understand by referring to the following.

Introduction to Kotlin for Java Programmers

Recommended Posts

I wrote a design pattern in kotlin Adapter edition
I wrote a design pattern in kotlin Factory edition
I wrote a design pattern in kotlin Builder edition
I wrote a design pattern in kotlin Singleton edition
I wrote a design pattern in kotlin, Iterator edition
I wrote a design pattern in kotlin Template edition
I wrote a design pattern in kotlin Prototype
Design Pattern #Adapter
Learn the design pattern "Adapter" in Python
I wrote a class in Python3 and Java
I wrote a Japanese parser in Japanese using pyparsing.
I wrote python in Japanese
I wrote a script to get a popular site in Japan
I wrote a script that splits the image in two
I get a UnicodeDecodeError in mecab-python3
I get a KeyError in pyclustering.xmeans
I wrote a function to load a Git extension script in Python
I wrote Fizz Buzz in Python
I wrote Gray Scale in Pytorch
I wrote a script to extract a web page link in Python
I wrote the queue in Python
I participated in AtCoder (ABC169 edition)
I wrote the stack in Python
I wrote a code to convert quaternions to z-y-x Euler angles in Python
[Python] I forcibly wrote a short Perlin noise generation function in Numpy.
I wrote FizzBuzz in python using a support vector machine (library LIVSVM).
Learn the design pattern "Prototype" in Python
Learn the design pattern "Builder" in Python
I want to print in a comprehension
Learn the design pattern "Flyweight" in Python
I made a payroll program in Python!
Learn the design pattern "Observer" in Python
Learn the design pattern "Memento" in Python
Learn the design pattern "Command" in Python
Learn the design pattern "Visitor" in Python
Learn the design pattern "Bridge" in Python
Learn the design pattern "Mediator" in Python
Learn the design pattern "Decorator" in Python
I wrote the selection sort in C
Learn the design pattern "Iterator" in Python
Learn the design pattern "Strategy" in Python
Learn the design pattern "Composite" in Python
Learn the design pattern "State" in Python
I wrote Project Euler 1 in one liner.
I started Node.js in a virtual environment
I created a password tool in Python.
I wrote the sliding wing in creation.
[Gang of Four] Design pattern learning --Adapter
I wrote a graph like R glmnet in Python for sparse modeling in Lasso
[Fundamental Information Technology Engineer Examination] I wrote a linear search algorithm in Python.
I wrote a PyPI module that extends the parameter style in Python's sqlite3 module
When I get a chromedriver error in Selenium
Learn the design pattern "Abstract Factory" in Python
Delete data in a pattern with Redis Cluster
Learn the design pattern "Template Method" in Python
I want to create a window in Python
I tried playing a typing game in Python
Learn the design pattern "Factory Method" in Python
I wrote "Introduction to Effect Verification" in Python
[Memo] I tried a pivot table in Python
I wrote a script to upload a WordPress plugin