When is it a good idea to use factory methods within an object instead of a Factory class?
- 3See softwareengineering.stackexchange.com/questions/253254/…tkit– tkit2017-08-08 12:52:54 +00:00Commented Aug 8, 2017 at 12:52
- For an overview of some different kinds of factories, see: How does using the factory design pattern stop a class having to anticipate the class of objects it must create?jaco0646– jaco06462021-01-01 22:38:03 +00:00Commented Jan 1, 2021 at 22:38
- For specifically when to use Factory Method, see: Applicability for the Factory Method Pattern.jaco0646– jaco06462021-01-01 22:40:37 +00:00Commented Jan 1, 2021 at 22:40
17 Answers
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.
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.
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.
public interface IThingFactory { Thing GetThing(string theString); } public class ThingFactory : IThingFactory { public Thing GetThing(string theString) { return new Thing(theString, firstDependency, secondDependency); } } 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.
9 Comments
within an object instead of a Factory class. I think he meant the scenario where you make the ctor private and use a static method to instantiate the class (create an object). But to follow this example one must instantiate the ThingFactory class first to get Thing objects, which makes this a Factory class in effect.Factory methods should be considered as an alternative to constructors - mostly when constructors aren't expressive enough, ie.
class Foo{ public Foo(bool withBar); } is not as expressive as:
class Foo{ public static Foo withBar(); public static Foo withoutBar(); } Factory classes are useful when you need a complicated process for constructing the object, when the construction need a dependency that you do not want for the actual class, when you need to construct different objects etc.
3 Comments
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 a Kitchen and a LivingRoom object, and the LivingRoom object has a TV 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):
$TVObj = new TV($param1, $param2, $param3); $LivingroomObj = new LivingRoom($TVObj, $param1, $param2); $KitchenroomObj = new Kitchen($param1, $param2); $HouseObj = new House($LivingroomObj, $KitchenroomObj); 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 new House is far from convenient. Enter the Factories:
class HouseFactory { public function create() { $TVObj = new TV($param1, $param2, $param3); $LivingroomObj = new LivingRoom($TVObj, $param1, $param2); $KitchenroomObj = new Kitchen($param1, $param2); $HouseObj = new House($LivingroomObj, $KitchenroomObj); return $HouseObj; } } $houseFactory = new HouseFactory(); $HouseObj = $houseFactory->create(); 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 a House) 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.
9 Comments
HouseFactory class?create method. E.g. if your House is going to always have the same kind of LivingRoom then it may make sense to have its params hardcoded in the factory class rather than passed as arguments. Or you may want to provide a type argument to your HouseFactory::create method if you have a few kinds of LivingRooms and have a switch inside with the parameters hardcoded for each type.It is good idea to use factory methods inside object when:
- Object's class doesn't know what exact sub-classes it have to create
- Object's class is designed so that objects it creates were specified by sub-classes
- Object's class delegates its duties to auxiliary sub-classes and doesn't know what exact class will take these duties
It is good idea to use abstract factory class when:
- Your object shouldn't depend on how its inner objects are created and designed
- Group of linked objects should be used together and you need to serve this constraint
- Object should be configured by one of several possible families of linked objects that will be a part of your parent object
- It is required to share child objects showing interfaces only but not an implementation
Comments
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.
interface Deliverable { /*********/ } abstract class DefaultProducer { public void taskToBeDone() { Deliverable deliverable = factoryMethodPattern(); } protected abstract Deliverable factoryMethodPattern(); } class SpecificDeliverable implements Deliverable { /***SPECIFIC TASK CAN BE WRITTEN HERE***/ } class SpecificProducer extends DefaultProducer { protected Deliverable factoryMethodPattern() { return new SpecificDeliverable(); } } public class MasterApplicationProgram { public static void main(String arg[]) { DefaultProducer defaultProducer = new SpecificProducer(); defaultProducer.taskToBeDone(); } } Comments
Problem statement: Create a Factory of Games by using Factory Methods, which defines the game interface.
Code snippet:
import java.util.HashMap; /* Product interface as per UML diagram */ interface Game{ /* createGame is a complex method, which executes a sequence of game steps */ public void createGame(); } /* ConcreteProduct implementation as per UML diagram */ class Chess implements Game{ public Chess(){ } public void createGame(){ System.out.println("---------------------------------------"); System.out.println("Create Chess game"); System.out.println("Opponents:2"); System.out.println("Define 64 blocks"); System.out.println("Place 16 pieces for White opponent"); System.out.println("Place 16 pieces for Black opponent"); System.out.println("Start Chess game"); System.out.println("---------------------------------------"); } } class Checkers implements Game{ public Checkers(){ } public void createGame(){ System.out.println("---------------------------------------"); System.out.println("Create Checkers game"); System.out.println("Opponents:2 or 3 or 4 or 6"); System.out.println("For each opponent, place 10 coins"); System.out.println("Start Checkers game"); System.out.println("---------------------------------------"); } } class Ludo implements Game{ public Ludo(){ } public void createGame(){ System.out.println("---------------------------------------"); System.out.println("Create Ludo game"); System.out.println("Opponents:2 or 3 or 4"); System.out.println("For each opponent, place 4 coins"); System.out.println("Create two dices with numbers from 1-6"); System.out.println("Start Ludo game"); System.out.println("---------------------------------------"); } } /* Creator interface as per UML diagram */ interface IGameFactory { public Game getGame(String gameName); } /* ConcreteCreator implementation as per UML diagram */ class GameFactory implements IGameFactory { HashMap<String,Game> games = new HashMap<String,Game>(); /* Since Game Creation is complex process, we don't want to create game using new operator every time. Instead we create Game only once and store it in Factory. When client request a specific game, Game object is returned from Factory instead of creating new Game on the fly, which is time consuming */ public GameFactory(){ games.put(Chess.class.getName(),new Chess()); games.put(Checkers.class.getName(),new Checkers()); games.put(Ludo.class.getName(),new Ludo()); } public Game getGame(String gameName){ return games.get(gameName); } } public class NonStaticFactoryDemo{ public static void main(String args[]){ if ( args.length < 1){ System.out.println("Usage: java FactoryDemo gameName"); return; } GameFactory factory = new GameFactory(); Game game = factory.getGame(args[0]); if ( game != null ){ game.createGame(); System.out.println("Game="+game.getClass().getName()); }else{ System.out.println(args[0]+ " Game does not exists in factory"); } } } output:
java NonStaticFactoryDemo Chess --------------------------------------- Create Chess game Opponents:2 Define 64 blocks Place 16 pieces for White opponent Place 16 pieces for Black opponent Start Chess game --------------------------------------- Game=Chess This example shows a Factory class by implementing a FactoryMethod.
Gameis the interface for all type of games. It defines complex method:createGame()Chess, Ludo, Checkersare different variants of games, which provide implementation tocreateGame()public Game getGame(String gameName)isFactoryMethodinIGameFactoryclassGameFactorypre-creates different type of games in constructor. It implementsIGameFactoryfactory method.game Name is passed as command line argument to
NotStaticFactoryDemogetGameinGameFactoryaccepts a game name and returns correspondingGameobject.
Definition from Wikipedia/Gang of Four ( Design Patterns: Elements of Reusable Object-Oriented Software book)
Factory: Creates objects without exposing the instantiation logic to the client.
Factory Method: Define an interface for creating an object, but let subclasses decide which class to instantiate. The Factory method lets a class defer instantiation it uses to subclasses." (Gang Of Four)
Use case:
When to use: Client doesn't know which concrete classes it will be required to create the object at runtime. It just wants to get a class that will do the job.
4 Comments
getArea() is not a factory method at all.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).
Comments
Factory classes are useful for when the object type that they return has a private constructor, when different factory classes set different properties on the returning object, or when a specific factory type is coupled with its returning concrete type.
WCF uses ServiceHostFactory classes to retrieve ServiceHost objects in different situations. The standard ServiceHostFactory is used by IIS to retrieve ServiceHost instances for .svc files, but a WebScriptServiceHostFactory is used for services that return serializations to JavaScript clients. ADO.NET Data Services has its own special DataServiceHostFactory and ASP.NET has its ApplicationServicesHostFactory since its services have private constructors.
If you only have one class that's consuming the factory, then you can just use a factory method within that class.
Comments
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.
Comments
GOF Definition :
Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory method lets a class defer instantiation to subclasses.
Generic example :
public abstract class Factory<T> { public abstract T instantiate(Supplier<? extends T> supplier); } The concrete class
public class SupplierFactory<T> extends Factory<T> { @Override public T instantiate(Supplier<? extends T> supplier) { return supplier.get(); } } The Implementation
public class Alpha implements BaseInterface { @Override public void doAction() { System.out.println("The Alpha executed"); } } public class Beta implements BaseInterface { @Override public void doAction() { System.out.println("The Beta executed"); } } public interface BaseInterface { void doAction(); } public class Main { public static void main(String[] args) { Factory<BaseInterface> secondFactory = new SupplierFactory<>(); secondFactory.instantiate(Beta::new).doAction(); secondFactory.instantiate(Alpha::new).doAction(); } } Brief advantages
- You are separating code that can vary from the code that does not vary (i.e., the advantages of using a simple factory pattern is still present). This technique helps you easily maintain code.
- Your code is not tightly coupled; so, you can add new classes like Lion, Beer, and so forth, at any time in the system without modifying the existing architecture. So, you have followed the “closed for modification but open for extension” principle.
Comments
My short explanation will be that we use the factory pattern when we don't have enough information to create a concrete object. We either don't know the dependencies or we don't know the type of the object. And almost always we don't know them because this is information that comes at runtime.
Example: we know that we have to create a vehicle object but we don't know if it flies or it works on ground.
Comments
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.
Comments
if you want to create a different object in terms of using. It is useful.
public class factoryMethodPattern { static String planName = "COMMERCIALPLAN"; static int units = 3; public static void main(String args[]) { GetPlanFactory planFactory = new GetPlanFactory(); Plan p = planFactory.getPlan(planName); System.out.print("Bill amount for " + planName + " of " + units + " units is: "); p.getRate(); p.calculateBill(units); } } abstract class Plan { protected double rate; abstract void getRate(); public void calculateBill(int units) { System.out.println(units * rate); } } class DomesticPlan extends Plan { // @override public void getRate() { rate = 3.50; } } class CommercialPlan extends Plan { // @override public void getRate() { rate = 7.50; } } class InstitutionalPlan extends Plan { // @override public void getRate() { rate = 5.50; } } class GetPlanFactory { // use getPlan method to get object of type Plan public Plan getPlan(String planType) { if (planType == null) { return null; } if (planType.equalsIgnoreCase("DOMESTICPLAN")) { return new DomesticPlan(); } else if (planType.equalsIgnoreCase("COMMERCIALPLAN")) { return new CommercialPlan(); } else if (planType.equalsIgnoreCase("INSTITUTIONALPLAN")) { return new InstitutionalPlan(); } return null; } } Comments
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
Comments
I think it will depend of loose coupling degree that you want to bring to your code.
Factory method decouples things very well but factory class no.
In other words, it's easier to change things if you use factory method than if you use a simple factory (known as factory class).
Look into this example: https://connected2know.com/programming/java-factory-pattern/ . Now, imagine that you want to bring a new Animal. In Factory class you need to change the Factory but in the factory method, no, you only need to add a new subclass.
Comments
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 or Shapes. 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.
class NumbersFactory { public static function makeNumber( $type, $number ) { $numObject = null; $number = null; switch( $type ) { case 'float': $numObject = new Float( $number ); break; case 'integer': $numObject = new Integer( $number ); break; case 'short': $numObject = new Short( $number ); break; case 'double': $numObject = new Double( $number ); break; case 'long': $numObject = new Long( $number ); break; default: $numObject = new Integer( $number ); break; } return $numObject; } } /* Numbers interface */ abstract class Number { protected $number; public function __construct( $number ) { $this->number = $number; } abstract public function add(); abstract public function subtract(); abstract public function multiply(); abstract public function divide(); } /* Float Implementation */ class Float extends Number { public function add() { // implementation goes here } public function subtract() { // implementation goes here } public function multiply() { // implementation goes here } public function divide() { // implementation goes here } } /* Integer Implementation */ class Integer extends Number { public function add() { // implementation goes here } public function subtract() { // implementation goes here } public function multiply() { // implementation goes here } public function divide() { // implementation goes here } } /* Short Implementation */ class Short extends Number { public function add() { // implementation goes here } public function subtract() { // implementation goes here } public function multiply() { // implementation goes here } public function divide() { // implementation goes here } } /* Double Implementation */ class Double extends Number { public function add() { // implementation goes here } public function subtract() { // implementation goes here } public function multiply() { // implementation goes here } public function divide() { // implementation goes here } } /* Long Implementation */ class Long extends Number { public function add() { // implementation goes here } public function subtract() { // implementation goes here } public function multiply() { // implementation goes here } public function divide() { // implementation goes here } } $number = NumbersFactory::makeNumber( 'float', 12.5 ); 2 Comments
NumbersFactory::makeNumber( 'float', 12.5 ); giving me over just saying new Float(12.5); if I know I need a Float? This is what I don't understand about factories... what is the point?