The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

tunca (1) [Avatar] Offline
#1
I'm very new to AOP and have been working on a case.

Let's assume that we have a class RectButton which draws a button with rectangle shape.

Our purpose is to change its look and add more shapes on the buttons. Summary of the required shapes are like this:

class OvalAndRectButton extends RectButton
class StringAndRectButton extends RectButton
class OvalAndStringAndRectButton extends StringAndRectButton

And a very simple OO way is as follows

class RectButton
{
public int x = 100;
public int y = 100;

public void draw(Graphics g)
{
g.drawRect(x, y, 10, 10);
}
}

class OvalAndRectButton extends RectButton
{
public void draw(Graphics g)
{
super.draw(g);
g.drawOval(x, y, 10, 10);
}
}

class StringAndRectButton extends RectButton
{
public void draw(Graphics g)
{
super.draw(g);
g.drawString("Button", x, y);
}
}

class OvalAndStringAndRectButton extends StringAndRectButton
{
public OvalAndRectButton ovalAndRectButton = new OvalAndRectButton();
public void draw(Graphics g)
{
super.draw(g);
ovalAndRectButton.x = x;
ovalAndRectButton.y = y;
ovalAndRectButton.draw(g);
}

}

Above, there are a few problems with OvalAndStringAndRectButton:

1) ovalAndRectButton draws not only an oval but a rectangle too where the rectangle was already drawn by StringAndRectButton. (Therefore the rectangle was drawn twice)
2) OvalAndStringAndRectButton must set x and y of ovalAndRectButton to draw which is a performance problem (not to be mentioned practically but theoretically)
3) x and y are both hold in OvalAndStringAndRectButton and ovalAndRectButton which is a memory problem (holds unnecessary memory for two int fields)
4) There is a problem of assymmetry. OvalAndStringAndRectButton could also be like this

class OvalAndStringAndRectButton extends OvalAndRectButton
{
public StringAndRectButton stringAndRectButton =
new StringAndRectButton();
...

How can we solve the problems using AOP.

Can it be implemented with AspectJ? And what is the "right" way of doing it?

King regards,

Erkin

Message was edited by:
tunca
ramnivas (171) [Avatar] Offline
#2
Re: OvalAndStringAndRectButton? AOP question regarding a problem
While I can think of a few ways AOP may help here, I think this can be solved in OOP if you modify the use of inheritance with composition. The way I see, you need something like ComposititeButton with a method such as addShape(). Then the draw() method simply iterates overs and draws all the shapes. You may, of course, use convenient subclasses that call addShape() in their constructor.

I hope this helps.

-Ramnivas