开发者

Is it best practice for every property on a class to conform to an interface?

I am increasingly seeing examples of this style. This happens irrespective of whether the interface is ever likely to be needed. In other case, the adoption of new interfaces prove helpful. However, it cannot always be predicted w开发者_StackOverflow中文版hether it will be used or not but a new interfaces are created for new properties to conform to... Best practice or not?


As usual, it depends. If you're writing a public API, this approach is extremely powerful. An example of this is the Progistics / Connectship API. At the time that I used it, there was a single concrete class. The rest of the public interface was all interfaces. They were able to rip out the entire implementation of the product and rewrite it, leaving the interfaces untouched.

Of course, there is a cost associated with this. You have to put a lot of up front time and effort into making an elegant API if you're going to want to cast it in stone (of course, this is the case whether you choose to use interfaces or not).

On the other hand, if you're writing the data access layer for an internal business application, you might not reap as many rewards.


In theory, you should have an interface or abstract class for any class that contains business logic - so you can unit test it and/or easily swap implementations. Classes that are used to represent data - model classes - don't have to implement an interface to be a "best practice" (it has to end somewhere).


I'd have to go with the Extreme Programming and YAGNI (You ain't gonna need it) practice. Only extract the interface when you need it for another class. Otherwise, it's just adding overhead.

The exception, of course, is when you need to add a Mock object into your testing scheme, and subclassing doesn't achieve what you want.

This topic is related to Emergent Design. I consider it to be the mark of excellent designers.


I'd say NOT. If familiar objects share a property, then create the interface. If the need arises then create the interfaces. I think that creating interfaces by default for all properties creates too much overhead.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜