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
One situation where I personally find separate Factory classes to make sense is when the final object you are trying to create relies on several other objects. E.g, in PHP: Suppose you have a
House
object, which in turn has aKitchen
and aLivingRoom
object, and theLivingRoom
object has aTV
object inside as well.The simplest method to achieve this is having each object create their children on their construct method, but if the properties are relatively nested, when your
House
fails creating you will probably spend some time trying to isolate exactly what is failing.The alternative is to do the following (dependency injection, if you like the fancy term):
Here if the process of creating a
House
fails there is only one place to look, but having to use this chunk every time one wants a newHouse
is far from convenient. Enter the Factories:Thanks to the factory here the process of creating a
House
is abstracted (in that you don't need to create and set up every single dependency when you just want to create aHouse
) and at the same time centralized which makes it easier to maintain. There are other reasons why using separate Factories can be beneficial (e.g. testability) but I find this specific use case to illustrate best how Factory classes can be useful.I like thinking about design pattens in terms of my classes being 'people,' and the patterns are the ways that the people talk to each other.
So, to me the factory pattern is like a hiring agency. You've got someone that will need a variable number of workers. This person may know some info they need in the people they hire, but that's it.
So, when they need a new employee, they call the hiring agency and tell them what they need. Now, to actually hire someone, you need to know a lot of stuff - benefits, eligibility verification, etc. But the person hiring doesn't need to know any of this - the hiring agency handles all of that.
In the same way, using a Factory allows the consumer to create new objects without having to know the details of how they're created, or what their dependencies are - they only have to give the information they actually want.
So, now the consumer of the ThingFactory can get a Thing, without having to know about the dependencies of the Thing, except for the string data that comes from the consumer.
I liken factories to the concept of libraries. For example you can have a library for working with numbers and another for working with shapes. You can store the functions of these libraries in logically named directories as
Numbers
orShapes
. These are generic types that could include integers, floats, dobules, longs or rectangles, circles, triangles, pentagons in the case of shapes.The factory petter uses polymorphism, dependency injection and Inversion of control.
The stated purpose of the Factory Patterns is:
Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses.
So let's say that you are building an Operating System or Framework and you are building all the discrete components.
Here is a simple example of the concept of the Factory Pattern in PHP. I may not be 100% on all of it but it's intended to serve as a simple example. I am not an expert.
Factory classes are more heavyweight, but give you certain advantages. In cases when you need to build your objects from multiple, raw data sources they allow you to encapsulate only the building logic (and maybe the aggregation of the data) in one place. There it can be tested in abstract without being concerned with the object interface.
I have found this a useful pattern, particularly where I am unable to replace and inadequate ORM and want to efficiently instantiate many objects from DB table joins or stored procedures.
It is good idea to use factory methods inside object when:
It is good idea to use abstract factory class when:
AbstractFactory example.