CSharpened
CSharpened

Reputation: 12574

Interfaces or classes when using objects

In my project I have defined an interface to describe the methods that each of my different database connection classes must have. This is called IDatabaseConnectivityObject. Each of my classes implements this interface to make sure that they all contain the same methods for running queries, making connections etc etc.

Consider the following code:

IDatabaseConnectivityObject adoDataBaseConnection = new DbProviderFactoryConnection();

DbProviderFactoryConnection adoDataBaseConnection = new DbProviderFactoryConnection();

Will the above lines both behave the same? If so why? If not then why not? What are the benefits of both?

It may be a really stupid question but I havent used interfaces all that long and I am not sure what line 1 does. It was my understanding that you couldnt make an instance of an interface as it merely defines behaviour so how is that line possible?

Upvotes: 0

Views: 108

Answers (4)

Adam Houldsworth
Adam Houldsworth

Reputation: 64467

Interfaces define the contract that consumers can rely on, the API if you will. The two lines are functionally the same if you intend to use members that are present on the interface.

Using the interface just expresses that you don't care how something is implemented, just that it is implemented to contract. Interfaces also promote Loose Coupling.

The benefit of programming against an interface is that you are programming against the "agreement" and not the "execution", or the contract and not the implementation. This has a large positive impact on testing as you can mock or stub interfaces during tests to reduce the number of extraneous dependencies in a test scenario.

A similar benefit to above is that using interfaces along with something like the factory pattern, IoC, or DI you can provide different implementations of the interface on the fly - for example, a different business logic handler for a different customer.

Another benefit is that it helps get around the lack of multiple inheritance. Most things can be expressed nicely via interfaces, and classes can implement multiple interfaces.

Upvotes: 1

Darin Dimitrov
Darin Dimitrov

Reputation: 1038720

Will the above lines both behave the same? If so why?

At runtime yes. The difference is that if you declare the variable of type IDatabaseConnectivityObject then at compile time you will be able to see only the members of this interface on the variable. If on the other hand you declare it as DbProviderFactoryConnection then you will see all the members of this class.

As a good practice it is recommended to always work with the highest possible type in the hierarchy when declaring the variable (IDatabaseConnectivityObject in this case) which allows you to access all members that the consumer will need.

Upvotes: 2

Myrtle
Myrtle

Reputation: 5831

You've asked for benefits.

When you are programming against interfaces (and are using factories to create the concrete types) this makes unit-testing easier as you can mock the instance that is put into the logic (unit).

This is actually a 'Design Pattern Principle':

"Program to an 'interface', not an 'implementation'." (Gang of Four 1995:18)

Upvotes: 0

Daniel Elliott
Daniel Elliott

Reputation: 22857

The first would allow you to swap in a different implementation of you IDatabaseConnectivityObject moving forward.

This is a good practice as it allows your system to be more resistant to change moving forward. Keeping the cost of change low is key.

Upvotes: 1

Related Questions