Java Forum / General / March 2005
name for a factory class
- - 21 Mar 2005 03:50 GMT what would be an appropriate name for a factory class that has a public static method that accepts a string and returns a Message or Reply object depending on the command found in the string?
e.g: string format : prefix command param1 param2 .... will return a Message if command is ABC will return a Reply if command is 123
iamfractal@hotmail.com - 21 Mar 2005 09:45 GMT > what would be an appropriate name for a factory class that has a public > static method that accepts a string and returns a Message or Reply [quoted text clipped - 4 lines] > will return a Message if command is ABC > will return a Reply if command is 123 Hi, nobody!
For a start, do distinguish between factory method and abstract factory; there is no, "Factory pattern." I'll presume here, judging by the simplicity of the problem (i.e., how well you've simplified the problem) that you're looking for the name of a factory method - do let me know if I'm wrong.
A factory method doesn't just create objects: it must return a superclass common to all objects it creates. In your case, there must be some class (or interface, if you're a Java/C#-dude) that both Message and Reply extend; let's call this class Text. This being the case, a good name for your factory method would be createText().
.ed
www.EdmundKirwan.com - Home of The Fractal Class Composition.
- - 21 Mar 2005 10:11 GMT > A factory method doesn't just create objects: it must return a > superclass common to all objects it creates. In your case, there must > be some class (or interface, if you're a Java/C#-dude) that both > Message and Reply extend; let's call this class Text. This being the > case, a good name for your factory method would be createText(). hi ed, would getInstance() be ok too? i notice there is a getControlInstance(Control ctl) in the javax.naming.ldap.ControlFactory class
iamfractal@hotmail.com - 21 Mar 2005 17:21 GMT > > A factory method doesn't just create objects: it must return a > > superclass common to all objects it creates. In your case, there must [quoted text clipped - 5 lines] > i notice there is a getControlInstance(Control ctl) > in the javax.naming.ldap.ControlFactory class Hi, again!
"getInstance()" is not a great name, because that's usally (or should we say, "Conventionally") the name of the method that returns the lone instance of a singleton; so people may think that this method is returning an instance of the factory, rather than an instance of one of the implementations that the factory's creating.
An analogy with the Sun name you provided above would be, "getTextInstance()," again referring to some superclass common to both of your implementations.
(It should be noted, however, that naming a method perhaps isn't so big a deal: choose a name, and move on. If similar naming questions arise, then be consistent. There'll be plenty of other nastinesses more deserving of your time.)
.ed
www.EdmundKirwan.com - Home of The Fractal Class Composition.
Patricia Shanahan - 21 Mar 2005 17:31 GMT ...
> (It should be noted, however, that naming a method > perhaps isn't so big a deal: choose a name, and move on. > If similar naming questions arise, then be consistent. > There'll be plenty of other nastinesses more deserving of > your time.) ...
Difficulty finding even one good name for a method may indicate a deeper problem that does need to be addressed. Ideally, a method has a single cohesive purpose, and the name reflects that. A method that does this or that, without an obvious purpose that covers both, may not be that cohesive.
This is different from having several good names for a method. In that case, I agree with "choose a name, and move on". Unless it is going to be a widely distributed library method, you can always refactor later for consistency.
Patricia
iamfractal@hotmail.com - 22 Mar 2005 09:34 GMT > ... Snoiip.
> Difficulty finding even one good name for a method may > indicate a deeper problem that does need to be addressed. [quoted text clipped - 8 lines] > > Patricia Verily doth Patricia speaketh the truth.
.ed
www.EdmundKirwan.com - Home of The Fractal Class Composition.
Chris Uppal - 22 Mar 2005 10:40 GMT > Difficulty finding even one good name for a method may > indicate a deeper problem that does need to be addressed. I agree entirely.
But in this case I suspect that the difficulty is caused by focussing on the Pattern (with a capital 'P') rather than on what the code is /for/. Just going from the minimal information that the OP presented, it sounds as if parseCommand() might be a decent name -- the fact that its acting as a factory for <whatever the common class of Message and Request is>, is less important than that it's parsing the command string.
OTOH, if the command string is /really/ only being used to parameterise the factory method (an idea I dislike, btw. /Strongly/ dislike...), then I don't see anything wrong with something simple like makeXxxx() (where Xxx is the mysterious common superclass).
Similar observations hold if the OP is actually considering a factory object of some kind, rather than a factory method.
-- chris
iamfractal@hotmail.com - 23 Mar 2005 10:58 GMT Clippity snip.
> OTOH, if the command string is /really/ only being used to parameterise the > factory method (an idea I dislike, btw. /Strongly/ dislike...), > > -- chris Hi, Chris!
Just curious: what do you dislike here?
(A) Parameterized factory methods in general. (B) Using a string as the parameter of a parameterized factory method.
I only ask because I use factory methods a lot, so if (A), I'd be keen to see what you use instead.
If (B), well I tend to parameterize with an int so I can use a switch.
Thanks,
.ed
www.EdmundKirwan.com - Home of The Fractal Class Composition.
Chris Uppal - 23 Mar 2005 11:34 GMT > > OTOH, if the command string is /really/ only being used to parameterise > > the > > factory method (an idea I dislike, btw. /Strongly/ dislike...), [...]
> Just curious: what do you dislike here? > [quoted text clipped - 3 lines] > I only ask because I use factory methods a lot, so if (A), I'd be keen > to see what you use instead. I have nothing at all against factory methods -- I like them and think they should be used more than they are.
No, my objection is to encoding important structural information in Strings. If a string comes from outside the program and needs to be parsed, then that's fair enough, but when one part of a program communicates with another part of the same program using (what amounts to) a separate programming language encoded as Java strings, then that strikes me as obfuscation for obfuscation's sake.
(I'm not, btw, claiming that there can /never/ be good reasons to use such a technique, just that they don't come up very often.)
-- chris
Free MagazinesGet 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 ...
|
|
|