어댑터 패턴(Adapter pattern)은 클래스의 인터페이스를 사용자가 기대하는 다른 인터페이스로 변환하는 패턴으로, 호환성이 없는 인터페이스 때문에 함께 동작할 수 없는 클래스들이 함께 작동하도록 해준다.
알고리즘을 요구사항에 맞춰 사용할 수 있다.
디버깅하기 편하다.
ex) 배열 함수를 가져왔는데 리스트로 써야될때 사용 가능
기본설계

target Method
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MathTest {
// 두배
public static double twoiTime(double num)
{
return num * 2;
}
// 절반
public static double half(double num)
{
return num / 2;
}
public static double doubled(double d) { return d * 2; }
}
| cs |
Interface
1
2
3
4
5
|
public interface Adapter
{
float TwiceOf(float f);
float HalfOf(float f);
}
| cs |
Interface Impliment
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AdapterImpl : MonoBehaviour, Adapter
{
public float TwiceOf(float f)
{
Debug.Log("함수호출");
return (float) MathTest.twoiTime(f);
}
public float HalfOf(float f)
{
Debug.Log("함수호출");
return (float)MathTest.half((double)f);
}
}
| cs |
Start
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class startTest : MonoBehaviour {
// Use this for initialization
void Start () {
Adapter adapter = new AdapterImpl();
Debug.Log(adapter.TwiceOf(100f));
Debug.Log(adapter.HalfOf(80f));
}
}
| cs |
공부 : 인프런 - 자바 디자인 패턴의 이해
https://www.youtube.com/watch?time_continue=54&v=gJDZ7pcvlAU
댓글
댓글 쓰기