| AOP, AspectJ | 2007/04/20 13:47:00 | |
| 트랙백 주소 : http://freetime.freelog.net/trb.php?id=32310 | ||
AOP (Aspect-Oriented Programming), 우리말로 표현하자면 상황중심 프로그래밍이라고 해야할까? AOP의 개념을 담기에는 좀 어색하지만 적당한 말이 버뜩 생각이 나지 않으니 그냥 넘어가야겠다.
AOP란 개념은 crosscutting concern에 대해 프로그래밍 코드를 적절히 모듈화할 수 있는 방법을 제공해준다. 적절한 예제는 아니지만(OOP 개념에 의하면 Point와 Line은 하나의 부모 class에서 상속받을 수 있으므로 setX를 overloading하면 되니까..)
어쨌든, 각 객체의 setX(int x)를 수행하기 전에 항상 System.out.println(x+" is setted") 를 실행하고 싶다면 Logger라는 aspect를 생성하고 Logger에서 pointcut을 생성한 다음 각 pointcut에 대한 code를 작성하면 된다.
class Point {
int x;
public void setX(int x)
{
this.x = x;
}
};
class Line {
int x;
public void setX(int x)
{
this.x = x;
}
};
public aspect Logger {
pointcut setX(int x):
(call(void Line.setX(int)) && args(x))
|| (call(void Point.setX(int)) && args(x));
before(int x): setX(x) {
System.out.println(x+" is setted");
}
};
===
public class Painter {
public static void main(String[] args)
{
Line l = new Line();
l.setX(0);
l.setX(1);
Point p = new Point();
p.setX(9);
p.setX(11);
}
};
실행결과.
0 is setted
1 is setted
9 is setted
11 is setted
pointcut에 대한 코드 (advisor라고 하는 것 같은데)는 before, after 등 여러가지를 삽입할 수 있다. 실제로 규모가 큰 소프트웨어를 작성하게 되는 경우에는 이와 같은 concept이 OOP와 합쳐진다면 시너지 효과를 발휘할 것 같다. 다만, 실제 무엇을 pointcut으로 잡을 것인가에 대한 design priciple이 일반적이지 않기 때문에 적응에 어려울 수도 있을 것 같다.
AOP란 개념은 crosscutting concern에 대해 프로그래밍 코드를 적절히 모듈화할 수 있는 방법을 제공해준다. 적절한 예제는 아니지만(OOP 개념에 의하면 Point와 Line은 하나의 부모 class에서 상속받을 수 있으므로 setX를 overloading하면 되니까..)
어쨌든, 각 객체의 setX(int x)를 수행하기 전에 항상 System.out.println(x+" is setted") 를 실행하고 싶다면 Logger라는 aspect를 생성하고 Logger에서 pointcut을 생성한 다음 각 pointcut에 대한 code를 작성하면 된다.
class Point {
int x;
public void setX(int x)
{
this.x = x;
}
};
class Line {
int x;
public void setX(int x)
{
this.x = x;
}
};
public aspect Logger {
pointcut setX(int x):
(call(void Line.setX(int)) && args(x))
|| (call(void Point.setX(int)) && args(x));
before(int x): setX(x) {
System.out.println(x+" is setted");
}
};
===
public class Painter {
public static void main(String[] args)
{
Line l = new Line();
l.setX(0);
l.setX(1);
Point p = new Point();
p.setX(9);
p.setX(11);
}
};
실행결과.
0 is setted
1 is setted
9 is setted
11 is setted
pointcut에 대한 코드 (advisor라고 하는 것 같은데)는 before, after 등 여러가지를 삽입할 수 있다. 실제로 규모가 큰 소프트웨어를 작성하게 되는 경우에는 이와 같은 concept이 OOP와 합쳐진다면 시너지 효과를 발휘할 것 같다. 다만, 실제 무엇을 pointcut으로 잡을 것인가에 대한 design priciple이 일반적이지 않기 때문에 적응에 어려울 수도 있을 것 같다.
aop, aspectj
|
댓글(0) l 트랙백(0) | |





카테고리