Should every single object have an interface and all objects loosely coupled?

后端 未结 7 1981
刺人心
刺人心 2020-12-24 05:23

From what I have read best practice is to have classes based on an interface and loosely couple the objects, in order to help code re-use and unit test.

Is this corr

7条回答
  •  死守一世寂寞
    2020-12-24 05:59

    I agree with kpollock. Interfaces are used to get a common ground for objects. The fact that they can be used in IOC containers and other purposes is an added feature.

    Let's say you have several types of customer classes that vary slightly but have common properties. In this case it is great to have a ICustomer interface to bound them together, logicaly. By doing that you could create a CustomerHander class/method that handels ICustomer objects the same way instead of creating a handerl method for each variation of customers.

    This is the strength of interfaces. If you only have a single class that implements an interface, then the interface isn't to much help, it just sits there and does nothing.

提交回复
热议问题