Understanding factories and using them?

I have never used Factories before for the simple reason I don’t understand when I need them. I am working on a little game in my free time, and I decided to implement FMOD for sound. I looked at the packaging intended for OpenAL (another sound setting), and it looked something like this ...

SoundObject * SoundObjectManager * SoundObjectFactory *

SoundObject was basically an instance of every sound object. SoundObjectManager manages all of these objects. This is fairly straightforward and makes a lot of sense, but I don’t understand what the factory does or what it is used for. I read at Factorys but still don't understand them.

Any help would be appreciated!

+5
source share
4 answers

Think of Factory as a "virtual constructor." It allows you to create objects with a common type of compilation time, but with different types of runtime. You can switch behavior by simply telling Factory to instantiate a different type of runtime.

+2
source

Factories are used when implementation needs to be parameterized. FMOD is a cross platform, it needs to decide which specific implementation you will provide for your platform. This is what Factory does. There are two main patterns Abstract Factory Sample and Factory Method Template .

+1
source

: , . , , , Windows OSX Linux.

, , Linux Windows - . 4 API. ( , , factory - , , ).

, SoundObject, , . LinuxSoundObject, WindowsSoundObject 5 SoundObject. SoundObject. SoundObjectFactory, , SoundObject, .

2 Windows. , , , .

, , , .

Factories isolate interface consumers from what type of implementation (i.e. implementation) is actually used.

+1
source

Factories can be used to implement inversion of control and to separate instance code ("new") from the logic of your components. This is useful when writing unit tests, as you may not want the test objects to create a bunch of other objects.

0
source

All Articles