Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #7122
| From | Lew <lewbloch@gmail.com> |
|---|---|
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: Synchronization of the constructor |
| Date | 2011-08-14 23:03 -0700 |
| Organization | http://groups.google.com |
| Message-ID | <7dd321cd-7f5e-4b96-ae01-023101d35f61@glegroupsg2000goo.googlegroups.com> (permalink) |
| References | (2 earlier) <j264b6$3u5$1@dont-email.me> <fb83dde2-63a4-4990-b8f9-af413301c31d@glegroupsg2000goo.googlegroups.com> <SuadncmuyIkvM9vTnZ2dnUVZ_rydnZ2d@earthlink.com> <e695c538-923e-41c7-9345-659ca2e4e9ab@glegroupsg2000goo.googlegroups.com> <34adnbSkXNNkAtXTnZ2dnUVZ_umdnZ2d@westnet.com.au> |
Qu0ll wrote: > Lew wrote: > [snip] >> Point being that there are particular use cases when it's OK to use a >> constructor if the advantages of a factory method are not particularly >> compelling, or if the hoops to implement and use a factory method are >> too epicyclic. But Patricia's advice and insights are very sound (as they >> always are from her); factory methods bring great advantages for a lot of >> scenarios. I certainly plan to increase my appreciation for them >> because of her post. > > In general is it better to have static factory methods on the class that is > to be instantiated or to have a separate factory class? That is a very good question. I don't think there is a general rule here. You have to do what is smart for the given situation. Either way, make sure the factory method is well written. If the construction is tightly bound to the class under construction, that would argue for the static method belonging to the class to be built. OTOH, if the factory is one of a class of factories, e.g., for collections, then it might make more sense to push it out into a helper class. Do what makes for the cleanest design that is easiest to maintain over the long term for the scenario. One goal is to make it easy to figure out where the factory is when you need instances. Another is to have compiler-enforced type safety. Also, there may be cases where the factory method should be an instance method of a separate class, e.g., if you want to load a configuration to guide the construction and have different instances work from different configurations. Do what makes sense. -- Lew
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar
Synchronization of the constructor MaciekL <__nospam__maclab@o2.pl> - 2011-08-13 11:58 +0200
Re: Synchronization of the constructor Robert Klemme <shortcutter@googlemail.com> - 2011-08-13 12:17 +0200
Re: Synchronization of the constructor "Qu0ll" <Qu0llSixFour@gmail.com> - 2011-08-13 21:36 +1000
Re: Synchronization of the constructor Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2011-08-13 09:30 -0300
[OT] Natural language. Was: Re: Synchronization of the constructor Patricia Shanahan <pats@acm.org> - 2011-08-13 07:10 -0700
Re: Synchronization of the constructor Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-08-13 09:36 -0400
Re: Synchronization of the constructor markspace <-@.> - 2011-08-13 08:13 -0700
Re: Synchronization of the constructor Lew <lewbloch@gmail.com> - 2011-08-13 09:23 -0700
Re: Synchronization of the constructor Patricia Shanahan <pats@acm.org> - 2011-08-13 10:04 -0700
Re: Synchronization of the constructor Lew <lewbloch@gmail.com> - 2011-08-13 21:29 -0700
Re: Synchronization of the constructor kedar mhaswade <kedar.mhaswade@gmail.com> - 2011-08-13 23:16 -0700
Re: Synchronization of the constructor "Qu0ll" <Qu0llSixFour@gmail.com> - 2011-08-15 14:24 +1000
Re: Synchronization of the constructor Lew <lewbloch@gmail.com> - 2011-08-14 23:03 -0700
Re: Synchronization of the constructor kedar mhaswade <kedar.mhaswade@gmail.com> - 2011-08-13 23:12 -0700
Re: Synchronization of the constructor markspace <-@.> - 2011-08-14 07:42 -0700
Re: Synchronization of the constructor markspace <-@.> - 2011-08-14 07:51 -0700
Re: Synchronization of the constructor Lew <lewbloch@gmail.com> - 2011-08-14 09:10 -0700
Re: Synchronization of the constructor markspace <-@.> - 2011-08-14 11:28 -0700
csiph-web