Adapter pattern

This commit is contained in:
kim
2021-01-08 17:02:49 +09:00
parent 39a39bdbde
commit 18e8ebcf3c
6 changed files with 68 additions and 1 deletions

View File

@@ -0,0 +1,14 @@
Adapter
- 연관성 없는 두 객체를 묶어서 사용
- 어떤 메소드를 사용하기위해 로직을 변경해야 하는 경우 사용
요구사항
- 두 수에 대한 다음 연산을 수행하는 객체를 만든다
- 수의 두 배의 수를 반환 : twiceOf(Float) : Float
- 수의 반(1/2)의 수를 반환 : halfOf(Float) : Float
- 구현 객체의 이름은 'Adapter'
- Math 클래스에서 두 배와 절반을 구하는 함수는 이미 구현되어 있다.
- 파라미터로 double 타입을 받는다.
- Math 클래스에 새롭게 두 배를 구할 수 있는 메소드 추가
- 절반을 구하는 기능에서 로그를 찍는 기능을 추가

View File

@@ -0,0 +1,7 @@
package Adapter;
public interface Adapter {
Float twiceOf(Float f);
Float halfOf(Float f);
}

View File

@@ -0,0 +1,16 @@
package Adapter;
public class AdapterImpl implements Adapter{
@Override
public Float twiceOf(Float f) {
// return (float) Math.twoTime(f.doubleValue());
return Math.doubled(f.doubleValue()).floatValue();
}
@Override
public Float halfOf(Float f) {
System.out.println("half 메소드 호출");
return (float) Math.half(f.doubleValue());
}
}

View File

@@ -0,0 +1,10 @@
package Adapter;
public class Main {
public static void main(String[] args) {
Adapter adapter = new AdapterImpl();
System.out.println(adapter.halfOf(100f));
System.out.println(adapter.twiceOf(10f));
}
}

View File

@@ -0,0 +1,20 @@
package Adapter;
public class Math {
// 두배
public static double twoTime(double num) {
return num * 2;
}
// 절반
public static double half(double num ) {
return num / 2;
}
// 강화된 알고리즘
public static Double doubled(Double d) {
return d * 2;
}
}