Dr.Denis McCracleJizz
Dr.Denis McCracleJizz

Reputation: 870

Interfaces optimizing code?

I was wondering if interfaces would give any benefit at run time.

Let's say i have a manager that handles a extremely large/complex object. If this object needs to interact with many chunk of different logic, would it be beneficial to make it implement an interface for each specific pieces of logic and then pass it as that specific interface. Would this improve performance in any way ?

Upvotes: 1

Views: 160

Answers (3)

Felice Pollano
Felice Pollano

Reputation: 33272

You design your software by interfaces ( and implementations ) to achieve your software components being loosely coupled and allow to choose among different concrete implementation of certain behaviors. This has nothing to do with performance at all, if you want to improve performance you would investigate other aspects of your code, and usually the performance benefits are achieved by modifying the implementation part, not in the way you decuple the code.

Upvotes: 2

Erik Dietrich
Erik Dietrich

Reputation: 6090

It won't be an improvement. You could think of an interface as a different way to represent an instance. Whichever representation (interface) you operate on doesn't change the characteristics of the instance itself. A lot of classes, for example, implement IDisposable. Passing such an instance around as IDisposable doesn't change the rest of the object's behavior and doesn't make its Dispose() logic any more efficient.

Upvotes: 1

Reed Copsey
Reed Copsey

Reputation: 564801

Would this improve performance in any way ?

No. This has no impact on performance.

Interfaces have other advantages, however, especially in terms of decoupling your implementation specifics from the API required and being passed throughout your system.

Upvotes: 7

Related Questions