NVI for virtual function implemented in every layer of a deep hierarchy

Suppose we have the following class hierarchy:

class Object {
public:
    virtual void update() {
        // Update position
    }
};

class Rocket : public Object {
public:
    virtual void update() {
        Object::update();
        // Orientate towards target
    }
};

class SparklingRocket : public Rocket {
public:
    virtual void update() {
        Rocket::update();
        // Create sparkling particles
    }
};

For obvious reason this is no good idea. For instance an inheritor of any of the classes might forget to call Base::update() and the behaviour of the program would be incomplete.
As I see it, functions that implement important behaviour shouldn’t be made virtual, that’s something better reserved for replaceable behaviour.

So, we would probably change the architecture like that:

class Object {
public:
    void update() {
        // Update position
        afterObjectUpdate();
    }

protected:
    virtual void afterObjectUpdate() {}
};

class Rocket : public Object {
protected:
    virtual final void afterObjectUpdate() override {
        // Orientate towards target
        afterRocketUpdate()
    }

    virtual void afterRocketUpdate() {}
};

class SparklingRocket : public Rocket {
protected:
    virtual final void afterRocketUpdate() {
        // Create sparkling particles
        afterSparklingRocketUpdate();
    }
    virtual void afterSparklingRocketUpdate() {}
};

This is pretty much what I want:

  • The public interface of all classes is only the non-virtual update() – method
  • When that method is called it is ensured that every update()-“submethod” is called
  • Even if the one inheritor forgets to call a afterUpdate() – method, the hierarchy is stable from the base class down. This way a API could ensure its own integrity whilst in the first codeexample it would have to rely on the user to call the Base::update() method

I dislike one thing though: The name of each class is part of the after…Update() methodname. That seems like codesmell to me.

I think the general goal of keeping virtual call hierarchies stable can’t be that uncommon. What is the commonly applied solution that I didn’t come across yet?

Having such virtual call hierarchies in the first place is quite rare. Having an inheritance tree that is more than three levels deep is often already considered a code smell and having a method that can be extended in each level of the hierarchy is rare.
Those two combined makes your scenario extremely rare.

On the other hand, the architecture you came up with has many similarities to a repeated application of the Template Method pattern. In the Template Method pattern, the base class implements an algorithm for which some steps are implemented by a derived class.


As you are having trouble coming up with good names for the afterXXXUpdate method, it could also be that inheritance isn’t the right tool here. Call chains, such as between your update methods are also a hallmark of the Decorator pattern. It might be worth to have a look at.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật