user327999
user327999

Reputation: 453

C# How to use multiple interface implementations

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

Answers (2)

Eric Petroelje
Eric Petroelje

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

Darin Dimitrov
Darin Dimitrov

Reputation: 1039498

You could use the factory design pattern.

Upvotes: 3

Related Questions