When should the factory template be used?

As soon as the headline asks when the trigger should appear in your head, meaning "Aha! I have to use the factory template here!"? I find these moments to come across many other design patterns, but I never stop and think about this pattern.

+4
source share
6 answers

The factory pattern is best used in situations where you want to encapsulate the instantiation of a group of objects within a method.

In other words, if you have a group of objects that all inherit from the same base class or all implement the same interface, which will be the instance where you would like to use the factory template (this will be a "template" that you would searched).

+4
source

Quote from GoF :

Use the Factory Method Template when

  • a class cannot foresee a class of objects that it must create.
  • the class wants its subclasses to indicate the object it creates.
  • the class delegates responsibility to one of several auxiliary subclasses, and you want to localize the knowledge of which auxiliary subclass is a delegate.

I highly recommend the GoF book. It has a section on the applicability of each of the 23 patterns that it covers.

+4
source

Whenever you find that the code looks something like this, you should probably use a factory:

IFoo obj; if ( someCondition ) { obj = new RegularFoo(); } else if ( otherCondition ) { obj = new SpecialFoo(); } else { obj = new DefaultFoo(); } 
+4
source

I can imagine two specific cases that I think of a factory pattern:

  • When a constructor has logic.
  • When I don't want the application to worry about what type the instance gets (for example, I have an abstract base class or interface that I return).
+3
source

Are you talking about a Factory method or an abstract Factory?

The main problem that both are solved is to allow clients to specify the exact class that creates the code construct. For example, if you provide an interface that clients can implement, and then in your code there is something like:

 IMyInterface x = new ConcreteClass(); 

Customers have no way to change the exact class that was created without access to this code.

A Factory Method is a virtual method that creates a specific class of a particular interface. Your code clients can provide an object that overrides this method to select the class they want to create. It might look like this in your code:

 IMyInterface x = factory.Create(); 

factory was passed by the client and implements an interface containing Create () - they can choose the exact class.

The Factory Summary should be used if you have hierarchies of related objects, and they should be able to write code that only leads to the underlying interfaces. The Factory annotation contains several Factory methods that create a specific concrete object from each hierarchy.

In the book "Samples of drawings" "Gangs of four" they give an example of a maze with rooms, walls and doors. The client code may look like this:

 IRoom r = mazeFactory.CreateRoom(); IWall nw = mazeFactory.CreateWall(); IWall sw = mazeFactory.CreateWall(); IWall ew = mazeFactory.CreateWall(); IWall ww = mazeFactory.CreateWall(); r.AddNorthWall(nw); r.AddSouthWall(sw); r.AddEastWall(ew); r.AddWestWall(ww); 

(etc.)

Exact concrete walls, rooms, doors can be decided by the mazeFactory developer, who will implement the interface you provide (IMazeFactory).

So, if you provide the interfaces or abstract classes that you expect from other people to implement and provide, then factories are a way for them to also provide a way for your code to create its own specific classes when you need them.

+1
source

Factories are often used in localization, where you have a screen with various layouts, tips and appearance for each market. You get a Factory screen to create a screen based on your language and creates an appropriate subclass based on its parameter.

0
source

Source: https://habr.com/ru/post/1286567/


All Articles