- Home /
programming design patterns are not Unity specific and widely explained in the internet
When to use a factory?
I am delving into the complex and intriguing world of code patterns and structures. In my journey into the void I have stumbled upon the Factory Pattern. My question is as follows: When should one use the Factory Pattern? To my limited knowledge, The Factory Pattern should be used when one needs to create a large number of objects that are all the same, except for some values that will be decided by parameters. Is this correct?
Well, in the world of app program$$anonymous$$g, Factories will instantiate instances of classes. You use it when you need a varying number of class instances.
In Unity, or game development in general, the question is, do you want to instantiate classes or game objects ?
$$anonymous$$ost of the time, you want to instantiate game objects, for ennemies, items and the like.
So, the Factory is more a "Spawner" component. On thing that you want to look at, is Object Pooling.
This would be the main reason behind a "Game Object Factory".
What do you mean by a varying number of class instances?
UnitCoach is right, but I wanted to underscore that the purpose is to create different types (of classes or structs) relative to an abstract base class. The situation this comes from a design approach where a base represents a generic, nondescript concept from which derived classes represent specific implementations. One classic example is a Shape class, representing any shape, with derived classes like Circle, Rectangle, NGon, etc. Code is write to call functions in Shape without regard to the specific shape, and abstract functions in the derived classes adapt to the specific implementation, so, for example, a Circle doesn't draw a Rectangle.
The problem the factory solves is how to create these derived objects from code that should not even know what those types are. The factory hides the need to know the types, viewing creation from the Shape perspective. This is more important in C++ than C#, where separating the knowledge of types is used as a means of organizing code to simplify compilation.
Just reading the description carefully should be enough. If it isn't and you still do not understand the purpose of a factory, i can asure you that you don't need it.
Questions like this are pointless and can't be answered. This is a question like "When to use a shovel". It's a tool. Once you understand what it is and what it does you can decide what you use it for or if you just keep it in your toolbox.
Ah good sir, for a Factory is not a shovel but a plasma torch. I seek not to waste your time but only to how I may use such a valuable tool. For if one does not understand the tool that they hold they may but cut themselves.
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Decision tree without a ton of if else statements 3 Answers
While loop ignores conditions in coroutine 1 Answer
How can I adjust a different touch spots on the object 0 Answers