Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » createXXX parameters in Factories
createXXX parameters in Factories [message #425568] Mon, 01 December 2008 17:03 Go to next message
David Michonneau is currently offline David MichonneauFriend
Messages: 85
Registered: July 2009
Member
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 17:27 Go to previous message
Ed Merks is currently offline Ed MerksFriend
Messages: 30879
Registered: July 2009
Senior Member
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
>
>
>
Previous Topic:Two way references and Dyanmic Feature Delegation
Next Topic:[CDO] NPE when removing an object in containment tree
Goto Forum:
  


Current Time: Mon Feb 17 01:07:06 GMT 2020

Powered by FUDForum. Page generated in 0.02802 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top