[aspectc-user] construction advice

Daniel Lohmann daniel.lohmann at informatik.uni-erlangen.de
Fri May 20 17:44:28 CEST 2005

Gary Duzan wrote:

>   Olaf Spinczyk <Olaf.Spinczyk at informatik.uni-erlangen.de> wrote:
>=>> Formula *f1 = new Formula("A");
>=>> Formula *f2 = new Formula("A");
>=>> I want an aspect such that f1==f2. This is related to the Flyweight 
>=>> pattern.
>=>> Is this possible?
>=>you could use an introduction to insert an 'operator new()' into Formula.
>   I don't think this would necessarily produce the desired result. The
>result of operator new is the storage to use. If you return the same
>storage as for a previous object, the constructor for the new object
>will still execute, writing to the same storage as the previous object,
>potentially resulting in chaos. It might be possible to avoid this with
>careful storage management, but it would likely be messy to try to apply
>as an aspect to arbitrary classes.
>   On the other hand, implementing the FLyweight pattern as an aspect
>does sound like a very intersting idea if it can be made to work.
Thats right. The above works only if the construction operation is 
idempotent. A constructor can be considered as idempotent, if a) its  
execution does not induce side effects, and b) the resulting object 
state depends only on the parameters passed to the constructor. If these 
conditions hold, it is safe to execute the construction code multiple 
times on the same instance.

One can not generally assume that constructors are idempotent. However, 
for flyweight-classes they typically have to be, as otherwise the 
application of the flyweight pattern (e.g. by an aspect) would change 
the semantics of the program. In other words: A non-indempotent 
constructor usually advices to not apply the flyweight pattern to this 

Anyway, technically I do not see any chance to prevent the execution of 
the constructor after a new operation. The only way to go is to do not 
use operator new, but a static factory method for object creation and 
instantiation. However, this means that you have to modify the client 
code :-(
(The compiler will show you all the positions to modify if you declare 
operator new as private in class Formula.)


More information about the aspectc-user mailing list