aspose file tools*
The moose likes OO, Patterns, UML and Refactoring and the fly likes Factory Method pattern Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "Factory Method pattern" Watch "Factory Method pattern" New topic
Author

Factory Method pattern

Joyce Lee
Ranch Hand

Joined: Jul 11, 2003
Posts: 1392
According to GOF, the intent of factory method is to "define an interface for creating an object, but let subclasses decide which class to instantiate. The example given requires two interface/abstract classes: Application and Document. So when it says "let subclasses decide...", it actually refers to the subclasses of Application, e.g MyApplication.

But the examples of factory method which I came across from books or internet require only 1 interface/abstract class:



Does the above example consider using factory method pattern? In this case, Trace interface is analogous to Document interface.

Joyce
[ August 03, 2004: Message edited by: Joyce Lee ]
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
Most would call it a factory method, simply because it is a method which takes responsibility for creating an object. But it does not implement the factory method pattern as the GoF define it. I suspect that the reason they did not define factory methods as broadly as this is that the motivation for the factory "pattern" you describe above is pretty weak: it is just an organisational tool; the type of Trace that gets created and how it gets created is fully known at compile time. It does not solve any design problem, so calling it a "pattern" is giving it too much honour.

The GoF factory method pattern, on the other hand, allows the precise nature of the object created to be determined by subclasses of your class.

The TraceFactory you give is somewhat pointless and a pretty weak example of a factory. It starts to make much more sense as an interface:
This way, we can plug different kind of traces into any code that uses a TraceFactory. This, of course, has lead us to the GoF Abstract Factory pattern, the intent of which is to "Provide an interface for creating families of related or dependent objects without specifying their
concrete classes." The 'family' in this case is a family of one (Trace).

- Peter
[ August 03, 2004: Message edited by: Peter den Haan ]
Joyce Lee
Ranch Hand

Joined: Jul 11, 2003
Posts: 1392
Many thanks, Peter.

By using your approach, I must implement Abstract factory to hide the instantiation of concrete class SystemTraceFactory. Furthermore, I thought Abstract Factory defines a list of creation methods that are used for creating objects that are related to one another. In the TraceFactory, there's only 1 creation method: getTrace().

In my previous example, though the type of object to be created and returned is fixed in the TraceFactory but the user of TraceFactory will not know what type of Trace object is being created.

I modified the code slightly such that the code does not require to be compiled. But does this qualify as Factory pattern?


Joyce
[ August 03, 2004: Message edited by: Joyce Lee ]
Tulsi Rai
Ranch Hand

Joined: Jun 26, 2002
Posts: 55
Hi Joyce,
Originally posted by Joyce Lee:
but the user of TraceFactory will not know what type of Trace object is being created.

I got thrown off by this statement.
Could you explain how the user of TraceFactory will not know what type of Trace objectis being created?
thanks.
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
Joyce, I think everyone will be more than happy to call that a factory method, even though it still isn't quite the Factory Method pattern as GoF has it. Although Design Patterns is still immensely important and influential, it was published twenty years ago and the way in which we use the pattern vocabulary has changed somewhat. We've also come up with lots of new patterns, some of which (like the Null Object pattern) are as important as any of the 23 originals.

- Peter
Joyce Lee
Ranch Hand

Joined: Jul 11, 2003
Posts: 1392
I studied the book "Java Design Patterns A Tutorial" by James W. Cooper. Besides Factory method and Abstract Factory patterns, the book also mentions a pattern calls "Simple Factory Pattern". Peter, most likely the examples given by me fall under this category.

A softcopy of this book can be downloaded here.


Ray, sorry for the confusion. What I said is that the method that calls TraceFactory.getTrace() will not have know if SystemTrace or FileTrace object is returned.

Actually, I got this example Trace here.
[ August 03, 2004: Message edited by: Joyce Lee ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Factory Method pattern
 
Similar Threads
Design Patterns Factory Method example
Abstract Factory and Factory Method
differences between abstarct factory pattern and factory method
Factory Method Pattern: Mental block?
Factory Pattern