List list = new ArrayList();
list.add("data"); // this is OK, list is valid
list.ensureCapacity(4); // this is not, ensureCapacity() is ArrayList only
/* You can even test for this dynamically at runtime using list instanceof ArrayList. */
| Cast 사용시 | Adapter 사용시 |
Object o = new ArrayList(); List list = (List)o; | IAdaptable adaptable = new ArrayList(); List list = (List)adaptable.getAdapter(java.util.List.class); |
- 아래 예처럼 HashMap를 List로 바꿀 수 있음
IAdaptable adaptable = new HashMap(); List list = (List)adaptable.getAdapter(java.util.List.class);- 대신 IAdaptable 인터페이스 구현이 필요함
public class HashMap implements IAdaptable {
public Object getAdapter(Class clazz) {
if (clazz == java.util.List.class) {
List list = new ArrayList(this.size());
list.addAll(this.values());
return list;
}
return null;
}
// ...
}
- UI와 딸린 데이터간에 Adapter를 사용할 수 있음
댓글 없음:
댓글 쓰기