어댑터 패턴
위키백과, 우리 모두의 백과사전.
어댑터 패턴(Adapter pattern)은 클래스의 인터페이스를 사용자가 기대하는 다른 인터페이스로 변환하는 패턴으로, 호환성이 없는 인터페이스 때문에 함께 동작할 수 없는 클래스들이 함께 작동하도록 해준다.
예 - 오브젝트 어댑터[편집]
# Python code sample class Target(object): def specific_request(self): return 'Hello Adapter Pattern!' class Adapter(object): def __init__(self, adaptee): self.adaptee = adaptee def request(self): return self.adaptee.specific_request() client = Adapter(Target()) print client.request()
예 - 클래스 어댑터[편집]
/** * Java code sample */ /* the client class should instantiate adapter objects */ /* by using a reference of this type */ interface Stack<T> { void push (T o); T pop (); T top (); } /* DoubleLinkedList is the adaptee class */ class DList<T> { public void insert (DNode pos, T o) { ... } public void remove (DNode pos) { ... } public void insertHead (T o) { ... } public void insertTail (T o) { ... } public T removeHead () { ... } public T removeTail () { ... } public T getHead () { ... } public T getTail () { ... } } /* Adapt DList class to Stack interface is the adapter class */ class DListImpStack<T> extends DList<T> implements Stack<T> { public void push (T o) { insertTail (o); } public T pop () { return removeTail (); } public T top () { return getTail (); } }
| 이 글은 컴퓨터 과학에 관한 토막글입니다. 서로의 지식을 모아 알차게 문서를 완성해 갑시다. |