공부/Object-Oriented Design Pattern
[어댑터 패턴] Class Adaptor vs. Object Adaptor
혼밥맨
2021. 3. 27. 01:00
반응형
[어댑터 패턴] Class Adaptor vs. Object Adaptor
- Class Adapter uses inheritance and can only wrap a class. It cannot wrap an interface since by definition it must derive from some base class.
- Object Adapter uses composition and can wrap classes or interfaces, or both. It can do this since it contains, as a private, encapsulated member, the class or interface object instance it wraps.
- 클래스 어댑터는 상속(Inheritance)을 사용하며 클래스만 래핑할 수 있습니다. 정의상 일부 기본 클래스에서 파생되어야 하므로 인터페이스를 래핑할 수 없습니다.
- 객체 어댑터는 컴포지션(Composition; 구성)을 사용하며 클래스나 인터페이스 또는 둘 모두를 래핑할 수 있습니다. 압축된 개인 구성원으로서 랩핑하는 클래스 또는 인터페이스 개체 인스턴스를 포함하므로 이 작업을 수행할 수 있습니다.
Class Adaptor Example,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
class MyExistingServiceClass {
public void show() {
System.out.println("Inside Service method show()");
}
}
interface ClientInterface {
void display();
}
class MyNewClassAdapter extends MyExistingServiceClass implements ClientInterface {
void display() {
show();
}
}
|
cs |
클래스 어댑터는 다중 상속을 사용하여 한 인터페이스를 다른 인터페이스에 적용.
Object Adaptor Example,
1
2
3
4
5
6
7
8
|
class MyNewObjectAdapter implements ClientInterface {
MyExistingServiceClass existingClassObject;
void display() {
existingClassObject.show();
}
}
|
cs |

객체 어댑터는 개체 구성에 따라 달라집니다.
반응형