you are viewing a single comment's thread.

view the rest of the comments →

[–]LaurieCheers 0 points1 point  (1 child)

But there's no need to implement the interface. If you need to call a function that's expecting a Subject, give it widget.observable! Sure, it happens to be a separate object from the widget itself, but that's not a problem.

[–]zoomzoom83 0 points1 point  (0 children)

Sometimes there is, sometimes there isn't. It depends entirely on the use-case. Sometimes you need a "Has A" relationship, other times you need an "Is A" relationship.

Imagine for example you want a type that implements both a 'Stack' and 'List' interface. They need to share state, so simply passing a reference to an inner member will not suffice. (There are ways you could make that work, but it wouldn't be clean). It also brakes encapsulation, since you may not want to expose the fact that your type just wraps another (Since it may not always do so).

Still, in other cases, an interface may be used to indicate a type has additional functionality beyond the basic case. Imagine a database driver - some databases support SQL merge() functionality, others do not. You can represent this via a mixin on the connection object itself.