External rendering of polymorphic objects

Say we have a classic polymorphic architecture:

class Animal
{
    virtual void eat() = 0;
};

class Frog : public Animal
{
    virtual void eat() { ... }
};

class Chipmunk : public Animal
{
    virtual void eat() { ... }
};

This is normal. But when it comes to rendering, I could add a virtual rendermethod to the base class Animal, like a method eat.

But then the animal source files are dependent on the particular library used. This does not make things modular and maintainable. Plus, their rendering code basically doesn't concern them.

What is a good way to infer specific rendering code from derived classes?

When I say "nice", I mean, not using something like an overloaded method that returns a different number for each derived class, so I can do a big if ... else if ....

+3
source share
3

- render() Visitor, Visitor.renderMe(this). renderMe .

+3

Bridge Pattern.

Render, , API, API . API- . , API.

+1

.

http://en.wikipedia.org/wiki/Dependency_injection

Animal>>render() 

Renderer ,

Animal>>render(Renderer r) 

:

r.render(this).
0
source

Source: https://habr.com/ru/post/1795865/


All Articles