RE: Re: Factory Method Pattern via Inner Classes
Subject: RE: Re: Factory Method Pattern via Inner Classes
Newsgroups: gmane.comp.programming.domain-driven-design
Date: 2008-08-07 02:34:52 GMT
Hello Mr. Moffdub,
I wish I knew your real name! You wrote:
Doing Order.createOrder() works, but it seems contrary to the spirit
of separating the construction of the object from the object itself.
That’s the way we used to do it in Smalltalk (and I still do it that way in Java). Kent Beck even wrote a pattern about it, called Complete Creation Method in his Smalltalk Best Practice Patterns book, renamed Complete Constructor in his more recent Implementation Patterns book.
I'm aware that my static inner class solution is essentially
equivalent, save for clearer syntax for the caller.
Beauty is in the eye of the beholder. I find Order.create(customer, items) to be cleaner than Order.OrderFactory.createOrder(customer, items). The latter seems to unnecessarily expose implementation, and costs the development and maintenance of an additional class which may not be entirely justified.
Kind Regards,
Randy Stafford
http://c2.com/cgi/wiki?RandyStafford
From: moffdub
[mailto:moffdub <at> yahoo.com]
Sent: Wednesday, August 06, 2008
4:07 PM
To: domaindrivendesign <at> yahoogroups.com
Subject: [domaindrivendesign] Re:
Factory Method Pattern via Inner Classes
Thanks for the reply. Wouldn't delegating to a factory
held in an
instance variable cut you off from accessing private members?
Assuming you are talking about a top-level class, the problem with the
factory using a constructor is I've made the constructor of Order
private to force users of Order to use the factory.
Doing Order.createOrder() works, but it seems contrary to the spirit
of separating the construction of the object from the object itself.
I'm aware that my static inner class solution is essentially
equivalent, save for clearer syntax for the caller.
I think Order.OrderFactory.createOrder(), imperfect as it is, makes it
clear what is going on to someone cognizant of patterns.
Order.createOrder() is a little less clear to me.
--- In domaindrivendesign <at> yahoogroups.com,
Michael Hunger <ddd <at> ...> wrote:
>
> I could also imagine using an factory method on the Order itself
> -> Order.createOrder()
> which then either:
> * does the creation itself
> * delegates to a static factory like your example
> * delegates to an factory instance held in an instance variable
(possibly setable by Dependency Injection)
>
> It depends on the complexity of the setup which approach you choose
to start with.
>
> I don't think this pollutes the Order to much because the creation
of itself unproblematic in the sense of SRP.
>
> Another question is why you need access to internal fields in the
factory and not just setting them in the constructor
> (or on the other hand in a public factory method of the order itself)?
>
> Michael
>
> --
> Michael Hunger
> Independent Consultant
>
> Web: http://www.jexp.de
> Email: michael.hunger <at> ...
>
> Enthusiastic Evangelist for Better Software Development
>
> Don't stop where you are: http://creating.passionate-developers.org
> We support Software Engineering Radio (www.se-radio.net)
>
Change settings via the Web (Yahoo! ID required)
Change settings via email: Switch delivery to Daily Digest | Switch to Fully Featured
Visit Your Group | Yahoo! Groups Terms of Use | Unsubscribe
__,_._,___
RSS Feed