中国开发网: 论坛: 程序员情感CBD: 贴子 74711
furnace
Give you an AOP example, by java. (ZT)
//--------------------------------------------------------------------------------
public class Point
{
private double x;
private double y;

Point(double x, double y)
{
this.x = x;
this.y = y;
}

public void setX(double x)
{
this.x = x;
}

public void setY(double y)
{
this.y = y;
}

public void setPolarCoordinates(double theta, double r)
{
x = r * Math.cos(theta);
y = r * Math.sin(theta);
}
}



//--------------------------------------------------------------------------------
public aspect PointChanged
{
private boolean Point.isDirty = false;

public void Point.makeDirty()
{
isDirty = true;
}

public boolean Point.isDirty()
{
return isDirty;
}

public void Point.save()
{
isDirty = false;
// add code to save point
}

pointcut fieldChanged(Point changed) :
call( * Point.set*(..)) && target(changed);

after(Point changed ) : fieldChanged(changed)
{
changed.makeDirty();
}
}



--------------------------------------------------------------------------------Doc 3, AspectJ Syntax Slide # 4

import junit.framework.*;
public class PointTests extends TestCase
{

public static void main( String args[])
{
junit.swingui.TestRunner.run( PointTests.class);
}

public void testPointChange()
{
Point sample = new Point( 1, 1);
assertFalse( sample.isDirty() );
sample.setX(5);
assertTrue( sample.isDirty());
sample.save();
assertFalse( sample.isDirty() );
sample.setY(2);
assertTrue( sample.isDirty());
sample.save();
assertFalse( sample.isDirty() );
}
}
I don't mind if you r FAT.
I don't mind if you r UGLY.
I don't mind if you ACT CUTE.
But I can't STAND if you r FAT, UGLY and still ACT CUTE.

相关信息:


欢迎光临本社区,您还没有登录,不能发贴子。请在 这里登录