Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / October 2007

Tip: Looking for answers? Try searching our database.

Do I need a factory?

Thread view: 
Diego - 04 Oct 2007 02:54 GMT
I have a class with 20+ class variables, mostly String and Date types.
The constructor of this class receives a String which is raw text that
has to be parsed. The parsing will extract data to populate the class
variables.

It does not seem right to me to perform this operation in the
constructor. I looked into the GoF creational design patterns but can't
seem to be able to find the appropriate solution for this problem.
Neither Factory Method nor Abstract Factory look like the right thing to
do (at least to me).

Can anybody suggest a good alternative to doing the parsing in the
constructor?

Thanks,
Diego
Arne Vajhøj - 04 Oct 2007 03:18 GMT
> I have a class with 20+ class variables, mostly String and Date types.
> The constructor of this class receives a String which is raw text that
[quoted text clipped - 9 lines]
> Can anybody suggest a good alternative to doing the parsing in the
> constructor?

20 arguments of String / 1 argument of String[]

maybe.

Arne
Daniel Pitts - 04 Oct 2007 03:49 GMT
On Oct 3, 7:18 pm, Arne Vajh?j <a...@vajhoej.dk> wrote:
> > I have a class with 20+ class variables, mostly String and Date types.
> > The constructor of this class receives a String which is raw text that
[quoted text clipped - 15 lines]
>
> Arne

The real question should be, why are you parsing the string? Is it not
possible to specify this as several strings?

If you must parse, then perhaps using the Builder pattern is more
appropriate.
diego.usenet@gmail.com - 04 Oct 2007 13:07 GMT
> On Oct 3, 7:18 pm, Arne Vajh?j <a...@vajhoej.dk> wrote:
>
[quoted text clipped - 23 lines]
> If you must parse, then perhaps using the Builder pattern is more
> appropriate.- Ocultar texto de la cita -

I am parsing the string because we have no control over it. It is a
bunch of text that we receive from a 3rd-party provider.
Arne Vajhøj - 04 Oct 2007 22:21 GMT
>>>> I have a class with 20+ class variables, mostly String and Date types.
>>>> The constructor of this class receives a String which is raw text that
[quoted text clipped - 16 lines]
> I am parsing the string because we have no control over it. It is a
> bunch of text that we receive from a 3rd-party provider.

If code outside of your control will call your code with a long string
and you code need the separate parts, then obviously you need to parse.

And I can not see why parsing in constructor should be worse
than parsing so many other places (well maybe exceptions ...).

Arne
Ed Kirwan - 04 Oct 2007 04:15 GMT
> I have a class with 20+ class variables, mostly String and Date types.
> The constructor of this class receives a String which is raw text that
[quoted text clipped - 12 lines]
> Thanks,
> Diego

Firstly, why does it not seem right to you to perform this operation in the
constructor?

Constructor are generally used to establish a minimum safe state of an
object, before which this object cannot be reliably depended upon.

If your object is useless (or perhaps dangerous) before all its 20+ class
variables are created, then parsing your string in the constructor seems
right to me: you are providing the shortest possible time between creating
the object and having it serve a useful purpose, thus minimising the time
during which a drunken, "Half-instantiated," object could cause problems
for its poor neighbours.

I don't think any of the GOF patterns were codified as a solution to this
problem (there is an unlimited number of problems that the GOF patterns
don't address).

The only alternative I can think necessarily begins with the assumption that
your object can play a useful role without all its 20+ variables created
(which I don't think is the case here), particularly if later interactions
with the object could alter either the nature of the 20+ variables to be
created, or the behaviour of the object itself.

Signature

.ed

www.EdmundKirwan.com - Home of The Fractal Class Composition

diego.usenet@gmail.com - 04 Oct 2007 13:09 GMT
> > I have a class with 20+ class variables, mostly String and Date types.
> > The constructor of this class receives a String which is raw text that
[quoted text clipped - 40 lines]
>
> www.EdmundKirwan.com- Home of The Fractal Class Composition- Ocultar texto de la cita -

I have no control over the incoming raw text received as a constructor
parameter, as it comes from a 3rd-party provider. No assumptions can
be made about this string, neither about its validity nor its
completeness. Each of the fields of my class has constraints for their
values (min/max length, optional/mandatory, etc) and all of these have
to be checked by the parsing process.

At first glance, it occured to me that the parsing would be non-
trivial and thus I could use a "helper" class (or factory or whatever
name it should receive) that would take care of the validation rules
and parsing mechanics, so as to not introduce too much complexity into
the constructor.

However, I understand that these rules are inherent to my business
entity and from this point of view it does not sound wrong to add them
into this class. And you are right when you point out that my object
would be useless before being fully initialized.

Not being able to decide between these two options, I am seeking for
your advice.

Thanks for your replies,
Diego
Manish Pandit - 04 Oct 2007 06:02 GMT
> I have a class with 20+ class variables, mostly String and Date types.
> The constructor of this class receives a String which is raw text that
[quoted text clipped - 12 lines]
> Thanks,
> Diego

This sort-of sounds like an Antipattern called 'Accumulate and Fire'
based on the description.

You should not use factory unless different behavior for the same
interface is desired. Can you provide more information about the
problem this class is trying to solve?

-cheers,
Manish


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.