Do I need a factory?

D

Diego

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
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

Diego said:
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?

20 arguments of String / 1 argument of String[]

maybe.

Arne
 
D

Daniel Pitts

Diego said:
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?

20 arguments of String / 1 argument of String[]

maybe.

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.
 
E

Ed Kirwan

Diego said:
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

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.
 
M

Manish Pandit

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

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
 
D

diego.usenet

20 arguments of String / 1 argument of String[]

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.- 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.
 
D

diego.usenet

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.

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
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

Diego 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
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?
20 arguments of String / 1 argument of String[]
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.

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top