When is it a good idea to use factory methods within an object instead of a Factory class?
相关问题
- Name for a method that has only side effects
- What is a correct approach to manage test data usi
- Can a [GoF]-ConcreteSubject override the notify me
- Can the builder pattern ever be doing too much?
- Carry STRef implicitly in an environment during co
相关文章
- Why is this factory returning a $$state object ins
- Builders in Java versus C++?
- DI container, factory, or new for ephemeral object
- “Adapter” or “adaptor”?
- Objective-C: Use singleton vs. use class as an obj
- Is copy-and-paste coding ever acceptable?
- How to Create a Custom tabBarController to simulat
- Dependency Injection Container
According to sourcemaking website, it's intentions are:
Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses.
Defining a "virtual" constructor.
The new operator considered harmful.
An example of how it can be used:
And then the testing:
It is important to clearly differentiate the idea behind using factory or factory method. Both are meant to address mutually exclusive different kind of object creation problems.
Let's be specific about "factory method":
First thing is that, when you are developing library or APIs which in turn will be used for further application development, then factory method is one of the best selections for creation pattern. Reason behind; We know that when to create an object of required functionality(s) but type of object will remain undecided or it will be decided ob dynamic parameters being passed.
Now the point is, approximately same can be achieved by using factory pattern itself but one huge drawback will introduce into the system if factory pattern will be used for above highlighted problem, it is that your logic of crating different objects(sub classes objects) will be specific to some business condition so in future when you need to extend your library's functionality for other platforms(In more technically, you need to add more sub classes of basic interface or abstract class so factory will return those objects also in addition to existing one based on some dynamic parameters) then every time you need to change(extend) the logic of factory class which will be costly operation and not good from design perspective. On the other side, if "factory method" pattern will be used to perform the same thing then you just need to create additional functionality(sub classes) and get it registered dynamically by injection which doesn't require changes in your base code.
UML from
Product: It defines an interface of the objects the Factory method creates.
ConcreteProduct: Implements Product interface
Creator: Declares the Factory method
ConcreateCreator: Implements the Factory method to return an instance of a ConcreteProduct
Problem statement: Create a Factory of Games by using Factory Methods, which defines the game interface.
Code snippet:
output:
This example shows a
Factory
class by implementing aFactoryMethod
.Game
is the interface for all type of games. It defines complex method:createGame()
Chess, Ludo, Checkers
are different variants of games, which provide implementation tocreateGame()
public Game getGame(String gameName)
isFactoryMethod
inIGameFactory
classGameFactory
pre-creates different type of games in constructor. It implementsIGameFactory
factory method.game Name is passed as command line argument to
NotStaticFactoryDemo
getGame
inGameFactory
accepts a game name and returns correspondingGame
object.Factory:
FactoryMethod
Use case:
When to use:
Client
doesn't know what concrete classes it will be required to create at runtime, but just wants to get a class that will do the job.It's really a matter of taste. Factory classes can be abstracted/interfaced away as necessary, whereas factory methods are lighter weight (and also tend to be testable, since they don't have a defined type, but they will require a well-known registration point, akin to a service locator but for locating factory methods).
Consider a scenario when you have to design an Order and Customer class. For simplicity and initial requirements you do not feel need of factory for Order class and fill your application with many 'new Order()' statements. Things are working well.
Now a new requirement comes into picture that Order object cannot be instantiated without Customer association (new dependency). Now You have following considerations.
1- You create constructor overload which will work only for new implementations. (Not acceptable). 2- You change Order() signatures and change each and every invokation. (Not a good practice and real pain).
Instead If you have created a factory for Order Class you only have to change one line of code and you are good to go. I suggest Factory class for almost every aggregate association. Hope that helps.
Any class deferring the object creation to its sub class for the object it needs to work with can be seen as an example of Factory pattern.
I have mentioned in detail in an another answer at https://stackoverflow.com/a/49110001/504133