When to use an activator and when to use a factory method?

I studied the design template of the factory method, and at the same time I came across an Activator object and how to use it by reading a tutorial (I often came across this object in intellisense, though).

The activator allows you to use the last binding, which can be extremely useful. But this is because we do not know which class we want to create. Similarly, the factory method deals with the same problem in software development.

At a simple level, a bunch of ifs or case case, and then instantiating an object based on an if condition is an implementation of the factory method, right?

In connection with the relevant topic, I read that polymorphism can reduce the connection between objects by excluding case statements. Is there an example of this?

thanks

+4
source share
1 answer

If during compilation you know all the potential classes that you want to create, use the Factory template, it will be faster and allow the compiler to check the security of your type.

On the other hand, if you do not know all the classes that you might need to instantiate (for example, if you are trying to provide a plugin architecture), the only option is to use Activator .

The simple rule here is this: select Factory using Activator (or any other type of runtime binding) if the script allows it.

+8
source

All Articles