[Tutor] What are the benefits of template or abstract base classes?

boB Stepp robertvstepp at gmail.com
Mon Jun 8 14:00:53 EDT 2020


On Mon, Jun 8, 2020 at 10:29 AM Alan Gauld via Tutor <tutor at python.org> wrote:
>
> I posted this earlier today but it seems to have vanished into
> bit dust... trying again...

I did receive the original email prior to it appearing on the Tutor Archive.

> But in the general case abstract classes are the fundamental building
> blocks of most OOP systems. You generalize the entities within the
> system into a smallish set of interacting abstract classes. You build
> the system using those abstract classes. In that way the entire core
> logic of your system is expressed in abstract terms. Then you start
> building the concrete versions of those abstractions and slotting them
> into the system. You don't need to modify the abstract code because the
> objects are polymorphic and will respond exactly like the abstract
> versions (except they actually do something!).
>
> So abstract classes are of limited value in isolation, it's when you get
> a group of them acting according to some pattern of behaviour that they
> become useful. Think about a GUI. It has Window objects(abstract) and
> events (abstract) and possibly a Canvas(possibly abstract). You can then
> design how window objects interact with each other and with events and
> represent themselves on a canvas in purely abstract terms. Then start
> building concrete widgets as subclasses of window. Create real
> events(mouse, keyboard, network etc) and real canvases(screen, printer,
> virtual) etc. The GUI framework will use these new classes with no new
> work. And that's how almost all real-world GUI frameworks are built.

Hmm.  Then ABCs seem quite useful.  I wonder why I normally only see
mention of them as being advanced features?  If I am following you
they would seem very useful for anything beyond small, simple OO
programs.  Sounds like something that I probably should investigate if
I ever get a handle on the basic class writing stuff.

> (The mixin style of multiple inheriance OOP is also based on the
> idea of abstract mixins that you then replace with concrete
> variations on the theme. But with mixins its normally a case
> of partially implemented classes rather than pure abstracts
>  - what C++ calls virtual classes.

So these concepts are language-agnostic and broadly used?

Thanks!

-- 
boB


More information about the Tutor mailing list