Refactoring 책에서 Delegation 관련 내용이 나와서 궁금하여 찾아보았다.
위임(Delegation)이란, 어떤 메소드의 처리를 다른 인스턴스의 메소드에 맡기는 것이다.
상속의 경우, class형이 정적으로 정해지기 때문에, 다른 자료형이 필요해진다면 프로그램의 구조 자체를 몽땅 수정해야 하는 단점이 있다. 위임(Delegation)은, 보다 유연하고 손쉽게 새로운 자료형을 추가/새로운 자료형으로의 변환 등을 할 수 있다는 장점이 있다.
위키피디아 에 있는 Delegation pattern 의 정의는 다음과 같다 :
In software engineering, the delegation pattern is a technique where an object outwardly expresses certain behaviour but in reality delegates responsibility for implementing that behavior to an associated object in an Inversion of Responsibility. The delegation pattern is the fundamental abstraction that underpins composition (also referred to as aggregation), mixins and aspects.
--> 대략의 뜻은, "delegation pattern은 외부 object가 어떠한 행위를 표현하지만 실제로는 그 행위를 구현하기 위한 책임을 관련된 object에 위임(delegation)한다." 이다.
역시 위키피디아에서 긁어 온 java 예제는 다음과 같다.
class A {
void f() { System.out.println("A: doing f()"); }
void g() { System.out.println("A: doing g()"); }
}
class C {
// delegation
A a = new A();
void f() { a.f(); }
void g() { a.g(); }
// normal attributes
X x = new X();
void y() { /* do stuff */ }
}
public class Main {
public static void main(String[] args) {
C c = new C();
c.f();
c.g();
}
}
==> Class C 가 실제 행위를 하는 것처럼 보이지만 실제 구현은 Class A에 되어 있다.
위임(Delegation)이란, 어떤 메소드의 처리를 다른 인스턴스의 메소드에 맡기는 것이다.
상속의 경우, class형이 정적으로 정해지기 때문에, 다른 자료형이 필요해진다면 프로그램의 구조 자체를 몽땅 수정해야 하는 단점이 있다. 위임(Delegation)은, 보다 유연하고 손쉽게 새로운 자료형을 추가/새로운 자료형으로의 변환 등을 할 수 있다는 장점이 있다.
위키피디아 에 있는 Delegation pattern 의 정의는 다음과 같다 :
In software engineering, the delegation pattern is a technique where an object outwardly expresses certain behaviour but in reality delegates responsibility for implementing that behavior to an associated object in an Inversion of Responsibility. The delegation pattern is the fundamental abstraction that underpins composition (also referred to as aggregation), mixins and aspects.
--> 대략의 뜻은, "delegation pattern은 외부 object가 어떠한 행위를 표현하지만 실제로는 그 행위를 구현하기 위한 책임을 관련된 object에 위임(delegation)한다." 이다.
역시 위키피디아에서 긁어 온 java 예제는 다음과 같다.
class A {
void f() { System.out.println("A: doing f()"); }
void g() { System.out.println("A: doing g()"); }
}
class C {
// delegation
A a = new A();
void f() { a.f(); }
void g() { a.g(); }
// normal attributes
X x = new X();
void y() { /* do stuff */ }
}
public class Main {
public static void main(String[] args) {
C c = new C();
c.f();
c.g();
}
}
==> Class C 가 실제 행위를 하는 것처럼 보이지만 실제 구현은 Class A에 되어 있다.
