createXXX parameters in Factories [message #425568] |
Mon, 01 December 2008 12:03  |
Eclipse User |
|
|
|
Hi,
Is it possible to generate factories with parameters in the createXXX
methods? The idea would be to pre-set the necessary parameters when the
object is instantiated, and this should normally happen in the factory.
To be more precise the pattern now looks like this:
MyObject obj = MyModelFactory.createMyObject();
obj.setParameter1(param1);
obj.setParameter2(param2);
The issue is that param1 and param2 might be mandatory for the contruction
of the object, so this pattern is not enforcing it (somebody might forget to
set one of them).
A better way would be:
MyObject obj = MyModelFactory.createMyObject(param1, param2);
with its implementation as
MyObjectImpl obj = new MyObjectImpl();
obj.setParameter1(param1);
obj.setParameter2(param2);
Can this be automated through the code generation?
In this second pattern, we also want to prevent the setParameter() methods
to surface on the API , but they must still be available on the
implementation. Is there any way to do this? the changeable attribute seems
to remove the generated setXXX at the implementation level too.
Thanks,
David
|
|
|
Re: createXXX parameters in Factories [message #425569 is a reply to message #425568] |
Mon, 01 December 2008 12:27  |
Eclipse User |
|
|
|
David,
Comments below.
David Michonneau wrote:
> Hi,
>
> Is it possible to generate factories with parameters in the createXXX
> methods?
No.
> The idea would be to pre-set the necessary parameters when the
> object is instantiated, and this should normally happen in the factory.
>
> To be more precise the pattern now looks like this:
>
> MyObject obj = MyModelFactory.createMyObject();
> obj.setParameter1(param1);
> obj.setParameter2(param2);
>
It would be sort of just a convenience...
> The issue is that param1 and param2 might be mandatory for the contruction
> of the object, so this pattern is not enforcing it (somebody might forget to
> set one of them).
>
Keep in mind that when you deserialize or create a copy, you might not
be in a position to provide all the arguments up front.
> A better way would be:
>
> MyObject obj = MyModelFactory.createMyObject(param1, param2);
>
> with its implementation as
> MyObjectImpl obj = new MyObjectImpl();
> obj.setParameter1(param1);
> obj.setParameter2(param2);
>
> Can this be automated through the code generation?
>
No.
> In this second pattern, we also want to prevent the setParameter() methods
> to surface on the API ,
You can already do the suppression of the accessors in the API with an
EAnnotation (as produced by EcoreUtil.setSuppresssedVisibility), and you
can add methods to the factory by hand.
> but they must still be available on the
> implementation. Is there any way to do this? the changeable attribute seems
> to remove the generated setXXX at the implementation level too.
>
Yes.
> Thanks,
>
> David
>
>
>
|
|
|
Powered by
FUDForum. Page generated in 0.46195 seconds