Refactoring observer pattern interface I have a observer pattern implemented and I'm using two base classes called Subject and Observer. - The Subject class is an abstract class that has methods to insert the observers. And the Observer is just an interface.

What I want is to provide a safer interface to classes that inherit to access the vector of observers and that is the way I implemented...

What would you do to make this better? Is this a bad design? Anything wrong?

#include &lt;iostream&gt;
#include &lt;vector&gt;
#include &lt;memory&gt;
#include &lt;algorithm&gt;

class Observer
{
public:
    virtual ~Observer() {}

    virtual void notifyHuman() = 0;
    virtual void notifyRobot() = 0;
};

class Subject
{
    typedef std::vector&lt;std::unique_ptr&lt;Observer&gt;&gt; vector_type;
public:
    virtual ~Subject() {}

    void attachObserver(vector_type::value_type observer)
    {
        container.push_back(std::move(observer));
    }

    vector_type::const_iterator begin() const
    {
        return container.begin();
    }

    vector_type::const_iterator end() const
    {
        return container.end();
    }

    virtual void notifyHuman() = 0;
    virtual void notifyRobot() = 0;
private:
    vector_type container;
};

class FooSubject : public Subject
{
public:
    FooSubject() {}
    ~FooSubject() {}

    void notifyHuman()
    {
        std::for_each(begin(), end(), [] (auto &amp; observer) {
            observer-&gt;notifyHuman();
        });
    }

    void notifyRobot()
    {
        std::for_each(begin(), end(), [] (auto &amp; observer) {
            observer-&gt;notifyRobot();
        });
    }
};

class BarObserver : public Observer
{
public:
    BarObserver() {}
    ~BarObserver() {}

    void notifyHuman()
    {
        std::cout &lt;&lt; "received notification of human." &lt;&lt; std::endl;
    }

    void notifyRobot()
    {
        std::cout &lt;&lt; "received notification of robot." &lt;&lt; std::endl;
    }
};

int main()
{
    FooSubject subject;
    subject.attachObserver(std::make_unique&lt;BarObserver&gt;());
    subject.notifyHuman();
    subject.notifyRobot();

    return 0;
}
Looks good.

This is a little confusing:

void attachObserver(vector_type::value_type observer)


To see what I have to pass to this function. I need to look up vector_type (which is in the class). I would make it more explicit so that I can see exactly what is expected:

void attachObserver(std::unique_ptr&lt;Oberver&gt; observer)


Don't see the need for:

class FooSubject : public Subject


I would just put all methods in Subject (no need for a FoodSubject). But this is a pattern not a design and you may have a reason that are not obvious from the current question.

There is no need to define methods that are covered by the default versions.

// I would not bother with this
FooSubject() {}
~FooSubject() {}