• Post Reply Bookmark Topic Watch Topic
  • New Topic

Clarification on two patterns Factory vs Prototype  RSS feed

 
Rick Reumann
Ranch Hand
Posts: 281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've been going through this very useful site http://home.earthlink.net/~huston2/dp/patterns.html and could still use some "dummy" clarification on the difference between the Factory pattern and the Prototype pattern. I know a GOF difference states: "Factory Method: creation through inheritance. Protoype: creation through delegation" but I still a bit unclear how this relates to these patterns.

It looks to me that in the factory pattern, you end up with a Factory returning you a new instance of a "type" that you are intested in.

In the prototype pattern, you have the subclass you are concerned with return an instance of itself from an over-ridden clone method (obviously taking advantage of polymorphism to call this clone method).

I'd love some more clarification on the prototype pattern and its benefits over abstract factory. In the meantime I'll be plugging away trying to figure it out myself. Thanks.
 
Rick Reumann
Ranch Hand
Posts: 281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, I 'think' now after looking at the GOF "Applicability" section, I see some major differences. I think the main problem I was having was that the example I were looking at typically just showed the clone method implementations returning a brand new instance.

In reality though the clone method could/should return a duplicate state (hence the word clone.. ahh lightbulb of the class in question. You could (even at runtime) set up the class to return prototypes with certain objects with a pre-set state and thus returned clones objects of them. The factory pattern doesn't provide this type of behavior. Am I on the right track?
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you are.
 
abhishek jhawar
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Prototype pattern

Prototype results in a cloned object which is different from the original object. The state of the original is the same as the clone, at the time of cloning. Thereafter each object may undergo state change. You can think of this as something similar photocopying the original and then modifying the photocopy at a few places.
Example
DVD duplication: Duplication of the master dvd to create several copies

Reporting object: Consider a report object that contains processed information to be passed to the GUI. The original report contains the data in ascending order. Now, using this pattern one can create a similar report but with data sorted in descending order.

Benefits

Performance: Cloning (using MemberwiseClone) is considerably less expensive than creating a new object afresh (with new operator). Note that one needs to override the MemberwiseClose() to perform a deep copy.
Objects can be clone very dynamically, without any insistence on up-front instantiation. The first created object can be created at any time in the application execution, and further duplication can take place at any time ahead.

When to use it
When the classes to instantiate are specified at run-time, for example, by dynamic loading.

When instances of a class can have one of only a few different combinations of state. It may be more convenient to install a corresponding number of prototypes and clone them rather than instantiating the class manually, each time with the appropriate state.

Comparison with Factory Pattern
Prototype pattern allows an object to create customized objects without knowing their class or any details of how to create them. So, it is this aspect it appears to be a lot like the Factory Method pattern. In both these patterns, the client can create any of the derived class objects without knowing anything about their own structure.
But the difference between the two patterns is the fact that for the Factory Method concentrates on creating one object of a non existing object type as a fresh creation (by understanding the exact sub-type of the Creator class). The Prototype pattern uses the class itself, especially the derived class for self duplication action.

Factory Method pattern
In this pattern, the client (or consumer) asks the Creator (or factory) for a specific type of object from a class hierarchy. The Creator method of the factory class delegates the creation of the specific object to the derived classes and return the object of the class of the type asked by client. In essence, you have a single point of contact for the creation of several objects of a class hierarchy.
You can think of this as going to a airline ticket counter (controller) and asking for a ticket by giving your preference of the ticket type (first class, executive or economy). The user is not concerned with how the ticket is being generated, even though in an object representation the first class and the economy ticket are both derived from the base ticket class.

When to use
Flexibility is important (low coupling)

Objects can be extended in subclasses

There is a specific reason why one subclass would be chosen over another—this logic forms part of the Factory Method.

A client delegates responsibilities to subclasses in parallel hierarchies.

Source:-difference between Abstract factory and Prototype design
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!