简体   繁体   中英

Use of Interfaces on a service layer

In our project architecture we are using a classic MVC pattern including a classic service layer (opening the transaction and calling the DAO layer).

For each service we have an implementation and his interface. But to be honest, I'm pretty sure that for one service and his interface, we will never have more than one implementation. So ok maybe it's more clear to have the public method declared in the interface helping to know what the service does, but an interface is used to have multiple implementation and if we know that we won't have more than one implementation, should we keep them?

i think this is a good approach for keeping interfaces.

reasons: 1. say you want to write junits for the same with a different implementations ex. inspite of getting data from database you want to get data from a separate datasource then a different implementation will suffice.

From the documentation :

Implementing an interface allows a class to become more formal about the behavior it promises to provide. Interfaces form a contract between the class and the outside world, and this contract is enforced at build time by the compiler.

If you know that you will only have one implementation, the implementation itself will define the contract, so you can remove the interfaces.

But writing an interface could help you to better define the contract and also, you could need at a given point to write a mock for the service, in such a case you would benefit from the use of interfaces.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM