0
$\begingroup$

I'm currently reading Design Patterns: Elements of Reusable Object-Oriented Software by GoF and having trouble understanding the following sentence (page 19, Section 1.6):

Composition requires objects to respect each others' interfaces, which in turn requires carefully designed interfaces that don't stop you from using one object with many others

  1. Don't we always access objects through their interfaces? How can we not respect another object's interface?
  2. How can an interface stop me from using one object with many others?
  3. Does this statement "one object with many others" mean that an object should have a interface general enough so that it can be composed with many other objects?
$\endgroup$
3
  • $\begingroup$ It's meant as opposed to inheritance. When you're inheriting, you're exposing the superclass' interface which both limits your class and future modifications to the superclass. $\endgroup$
    – Pål GD
    Jan 28, 2023 at 15:24
  • $\begingroup$ When you use composition, you fully control the interface of your class and can even tailor the interface of the objects you (indirectly) expose. $\endgroup$
    – Pål GD
    Jan 28, 2023 at 15:25
  • $\begingroup$ Thank you for the reply. I'm aware of the implementation dependency between a subclass and a parent class. Do you imply by "limit" the fact that the subclass can't modify the interface that it inherited from the parent class? If so, how does it limit the superclass? $\endgroup$ Jan 28, 2023 at 16:02

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.