Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why is it recommended to define service contract as an interface

Tags:

interface

wcf

why is it recommended to define service contract as an interface. Any specific advantages over having them as classes?

like image 843
balalakshmi Avatar asked Apr 02 '10 07:04

balalakshmi


People also ask

Why is an interface like a contract?

Interfaces form a contract between the class and the outside world, and this contract is enforced at build time by the compiler. If your class claims to implement an interface, all methods defined by that interface must appear in its source code before the class will successfully compile.

What is service contract data interface?

A service contract is a set of PHP interfaces that are defined for a module. A service contract includes data interfaces, which preserve data integrity, and service interfaces, which hide business logic details from service requestors such as controllers, web services, and other modules.

Why would you want to use an interface?

Interfaces are useful for the following: Capturing similarities among unrelated classes without artificially forcing a class relationship. Declaring methods that one or more classes are expected to implement. Revealing an object's programming interface without revealing its class.

Is a contract an interface?

An interface is a contract, but a contract doesn't necessarily have to be an interface. The interface in a nutshell defines the contract that the classes must implement. An abstract class is also a contract.


2 Answers

The primary goal is separate definition of your service from implementation

The user of your service should not know anything about how you implemented your service, but he should know what operations he can do and how.

That's why its using an interface instead of class, because interface doesn't contain an implementation.

You can share your interface one time and then never worry for years even if you changing implementation of its methods every day. End users will not need to recompile the code that's using your service

like image 101
Andrey Tagaew Avatar answered Sep 27 '22 15:09

Andrey Tagaew


Of course [there are several advantages] !

The main one is probably the ability to implement multiple classes which support said Interface and to use these classes interchangeably [with regards to the particular interface]. One of the direct uses of this is with Mock classes used for testing; This is also used with IoC (Inversion of Control) pattern, and more generally wherever we care about the "What" rather than the "Who", i.e. What matters is that whichever class is in place it behaves as per the contract (the API) regardless of "who" (which class) it is.

Another salient advantage of Interfaces is the ability to modularize behavior. For example your application may implement a concept which works, say, like a List (can be iterated over, supplies a number of items, etc.) and like a widget validator (some application specific thing). By having two interfaces "describing" this particular object, you can use instances of that class wherevever you'd use a List (and just that) and similarly you can use it as a widget validator (and just that) whereever these validator are needed. This is akin to multiple inheritance but more flexible.

In a nutshell (and some other answers started with this), the Interface defines the contract and the Class(es) implement(s) it.
Technically, a single class could do both of these things, i.e. you do not __need __ to have Interfaces, but it is very preferable to define APIs for most any behavior which may be implemented by several classes (whether multiple implementations of almost the same thing as with "mock classes", or very different classes but supplying one particular generic service/feature as say two very distinct Lists.)

like image 28
mjv Avatar answered Sep 27 '22 15:09

mjv