oop - Why should we place interfaces with classes that use them rather than those that implement them? -
i going through article robert c. martin , @ 1 place gave example this:
the first image shows there's cyclic dependency between 2 packages. remove dependency new interface added in second image. b implements interface , y uses it. , martin makes following point:
interfaces included in package uses them, rather in package implements them.
my question is, why should arrange interfaces way? reasoning behind packaging interfaces way? according common closure principle classes change should stay together. interface closer implementer or user, in terms of change???
technically, user no closer interface implementor. in terms of change both need change when interface changes.
however, why interface change?
the user calls interface can independent of whatever implementor available. therefore definition of interface dictated needs of user.
as user dictates definition of interface, there no point changing interface if user doesn't need it. implementor requiring change interface accommodate implementation should send red flags. why need more or different information user? use user?
also, implementor "merely" depends on interface in as needs provide implementations each of methods in interface. free provide empty stubs, in essence delivering nop clients.
so, user's needs drive changes interface , changes interface drive changes implementor(s). such user functionally closer interface implementor. makes case declare interface user , not implementor.
Comments
Post a Comment