Reputation: 453
I have 2 separate Interface Implementations and based on a setting I have stored in my database I want to use either one (create object of specific impl class and execute methods).
So,
If setting = 1 then use Interface Implementation 1 Else use Interface Implementation 2
What is the best way to do this? Is there anyway to dynamically instantiate an object of the correct interface implementation based on a variable value?
Upvotes: 1
Views: 213
Reputation: 60559
Yes, you'll want to have a look at the Factory pattern.
In essence, you delegate the creation of the actual object to another object (called the factory). When asked for an instance of the object, the factory would look at the database value and create the appropriate instance.
Upvotes: 2