I was working with java code that is supposedly using the Factory pattern, but I'm not completely convinced by the pattern.
My code does this:
// the factory
class SomeFactoryImpl {
Set<SomeClass> getSomeListOfObjects();
}
And somewhere in the code:
{ ...
SomeFactory factory = new SomeFactoryImpl();
Set<SomeClass> list = factory.getSomeListOfObjects();
}
The point I'm pondering is, if factory classes don't have a static create() method, then one will need to instantiate a factory, which IMO should be just as complex as instantiating an object itself.
I don't consider the argument that such a factory can return collections of objects to be produced is good enough. I feel there can be cleaner workarounds, if a factory instance needs to be created before actually creating objects from the factory.
I feel that it is better if the create method a static method of the factory class. But I'm also sure that my opinion is not completely "correct".
So can the SO community give examples where instantiating a Factory object is better than using static create methods?
Also, I came across an answer to a similar question, which listed these links and the answer: so I need to know clearly the difference between FactoryMethodPattern, FactoryMethod and CreationMethod with code examples.
Factory pattern removes the instantiation of actual implementation classes from client code. Factory pattern makes our code more robust, less coupled and easy to extend. For example, we can easily change PC class implementation because client program is unaware of this.
The Factory Method pattern is a design pattern used to define a runtime interface for creating an object. It's called a factory because it creates various types of objects without necessarily knowing what kind of object it creates or how to create it.
In class-based programming, the factory method pattern is a creational pattern that uses factory methods to deal with the problem of creating objects without having to specify the exact class of the object that will be created.
The factory design pattern says that define an interface (A java interface or an abstract class) and let the subclasses decide which object to instantiate. The factory method in the interface lets a class defer the instantiation to one or more concrete subclasses.
The factory method in the interface lets a class defer the instantiation to one or more concrete subclasses. Since this design patterns talk about instantiation of an object and so it comes under the category of creational design pattern.
Design Pattern - Factory Pattern. Factory pattern is one of the most used design patterns in Java. This type of design pattern comes under creational pattern as this pattern provides one of the best ways to create an object.
Super class in factory design pattern can be an interface, abstract class or a normal java class. For our factory design pattern example, we have abstract super class with overridden toString () method for testing purpose.
Using an instance of a factory shows the real benefits when combined with dependency injection.
So in your example, instead of:
{ ...
SomeFactory factory = new SomeFactoryImpl();
Set<SomeClass> list = factory.getSomeListOfObjects();
}
You would have:
public ThisClass(SomeFactory someFactory) {
this.factory = someFactory;
}
then later...
{ ...
Set<SomeClass> list = factory.getSomeListOfObjects();
}
Some points:
I know this only addresses a subset of your question...
I guess, a static method for the object creation is the most popular approach, but there are also some use-cases where first creating a factory instance makes sense. For example if you want to combine it with a registry (where multiple registries should be allowed to co-exist).
Also if the factory relies on some dynamic context information (database connections, ...) it is in my opinion better to let a factory-instance handle this.
First of all you are forgetting the main aim of the factory pattern, I opened the book of the gang of four and it states:
"Define an interface for creating an objet, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses."
This means that actually you define an interface, so SomeFactoryImpl
should actually implement an interface defined somewhere else. This comes handy when you've got many objects that need to be instantiated but you don't want to care about which kind of object they are.. For example I used them to develop a remote swing application in which the client downloaded through serialization the definition of some classes that didn't exist in the client VM. Every class defined a subclass of JPanel which its specific view but when reaching the client I had to find a way to istantiate these classes without knowing them so I used a factory pattern to call the factory class and let it instantiate my uknown object (althrough it is extending a subclass of JPanel defined by me).
Another example would be the generation of case-specific object to suit your needs. For example (like stated in wikipedia page related to this design pattern) you can think a factory that builds object and then another factory for the same kind of object but used to generate "fake objects" that will fail some kind of unit testing.
However you can solve your specific problem with static methods too but think about splitting the part that generates items from the part that uses them in a big project. Of course who is developing the client part should just know which factory interface is used and know just this to use all the objects definet in the other part.
Creation Pattern is a sort of 'facility' pattern used just to define custom versions of constructors without worrying using standard definition (having the name of the method equal to the name of the class) but it's nothing special. Just a different way to istantiate objects.. the creation pattern actually doesn't solve any kind of specific problem (excluding constructors with same number and kind of arguments).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With