title: Why inheritance is usually used instead of composition when implementing the observer pattern?
body: <pre><code>#include &lt;iostream&gt;
  #include &lt;set&gt;
  
  class Observer
  {
  public:
  
      virtual void update() = 0;
  };
  
  class Subject
  {
      std::set&lt;Observer*&gt; observers;
  
  public:
  
      void addObserver(Observer* observer)
      {
          observers.insert(observer);
      }
  
      void deleteObserver(Observer* observer)
      {
          observers.erase(observer);
      }
  
      void notifyObservers()
      {
          for (auto observer : observers)
              observer-&gt;update();
      }
  };
  
  class HaveValue1 { public: virtual int getValue1() const = 0; };
  class HaveValue2 { public: virtual int getValue2() const = 0; };
  
  class ObservingForValue1 : public Observer
  {
      HaveValue1* subject;
  
  public:
  
      ObservingForValue1(HaveValue1* subject) : subject(subject) {}
  
      virtual void update()
      {
          std::cout &lt;&lt; "New value of Value1: " &lt;&lt; subject-&gt;getValue1() &lt;&lt; '\n';
      }
  };
  
  class ObservingForValue2 : public Observer
  {
      HaveValue2* subject;
  
  public:
  
      ObservingForValue2(HaveValue2* subject) : subject(subject) {}
  
      virtual void update()
      {
          std::cout &lt;&lt; "New value of Value2: " &lt;&lt; subject-&gt;getValue2() &lt;&lt; '\n';
      }
  };
  
  class UsingInheritance : public HaveValue1, public HaveValue2, public Subject
  {
      int value1;
      int value2;
  
  public:
  
      UsingInheritance(int value1, int value2) : value1(value1), value2(value2) {}
  
      void setValue1(int value) { value1 = value; notifyObservers(); }
      void setValue2(int value) { value2 = value; notifyObservers(); }
  
      virtual int getValue1() const override { return value1; }
      virtual int getValue2() const override { return value2; }
  };
  
  class UsingComposition : public HaveValue1, public HaveValue2
  {
      int value1;
      int value2;
  
      Subject observersForValue1;
      Subject observersForValue2;
  
  public:
  
      UsingComposition(int value1, int value2) : value1(value1), value2(value2) {}
  
      void observeValue1(Observer* observer) { observersForValue1.addObserver(observer); }
      void observeValue2(Observer* observer) { observersForValue2.addObserver(observer); }
  
      void unobserveValue1(Observer* observer) { observersForValue1.deleteObserver(observer); }
      void unobserveValue2(Observer* observer) { observersForValue2.deleteObserver(observer); }
  
      void setValue1(int value)
      { value1 = value; observersForValue1.notifyObservers(); }
  
      void setValue2(int value)
      { value2 = value; observersForValue2.notifyObservers(); }
  
      virtual int getValue1() const override { return value1; }
      virtual int getValue2() const override { return value2; }
  };
  
  int main()
  {
      std::cout &lt;&lt; "With Inheritance:\n";
  
      UsingInheritance inheritance{ 1, 2 };
  
      ObservingForValue1 io1{ &amp;inheritance };
      ObservingForValue2 io2{ &amp;inheritance };
  
      inheritance.addObserver(&amp;io1);
      inheritance.addObserver(&amp;io2);
  
      inheritance.setValue1(11);
      inheritance.setValue2(22);
  
      std::cout &lt;&lt; "\nWith Composition:\n";
  
      UsingComposition composition{ 1, 2 };
  
      ObservingForValue1 co1{ &amp;composition };
      ObservingForValue2 co2{ &amp;composition };
  
      composition.observeValue1(&amp;co1);
      composition.observeValue2(&amp;co2);
  
      composition.setValue1(11);
      composition.setValue2(22);
  }
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>With Inheritance:
  New value of Value2: 2
  New value of Value1: 11
  New value of Value2: 22
  New value of Value1: 11
  
  With Composition:
  New value of Value1: 11
  New value of Value2: 22
  </code></pre>
  
  <p>I've learned the <code>observer</code> pattern from many sources, yet none of them mentioned implementing it with composition. I think using composition is more powerful than inheritance for couple of reasons:</p>
  
  <p>1 - A class can have lots of observers and each one observing for a different thing. If I wanted to achieve the same behavior (having different observers observing <code>n</code> different things), I'll have to change the base class so that it supports <code>n</code> types of observers and later on, if I wanted it to support <code>n + 1</code> observers, I'll have to change it again or inherit from another class. Whereas when using composition, I can just add another <code>Subject</code> and that's it. </p>
  
  <p>2 - With inheritance if I wanted to notify the observers, I'll notify all the subscribed ones, even if each one of them is looking for a different thing. Whereas when using composition, I can just notify the desired ones. </p>
  
  <p>3 - Some languages like <code>java</code> doesn't allow multiple inheritance, if I inherit from an abstract class that implements some logic of <code>Subject</code>, I won't be able to inherit from another class. Whereas using composition, there is a free spot for some class to inherit from.</p>
  
  <p>4 - Composition is prefered over inheritance anyways. If some behavior is achievable using both inheritance and composition, Why the inheritance approach is more popular?</p>
  
  <p>In the code above, I have an <code>Observer</code> and a <code>Subject</code>. There are 2 types of <code>Subject</code>, one using inheritance called <code>UsingInheritance</code>, and the other uses composition, called <code>UsingComposition</code>. Each one of them has 2 values, <code>value1</code> and <code>value2</code>. There are 2 observers, <code>ObservingForValue1</code> which is only interested in <code>value1</code> and <code>ObservingForValue2</code> which is only interested in <code>value2</code>. Notice that when using <code>UsingInheritance</code>, all the subscribed observers get notified even if the value they're interested in didn't change, whereas when using <code>UsingComposition</code>, only the desired ones get updated.</p>