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

Alan Gauld alan.gauld at yahoo.co.uk
Mon Jun 8 11:29:32 EDT 2020


I posted this earlier today but it seems to have vanished into
bit dust... trying again...


On 08/06/2020 04:02, boB Stepp wrote:

> implementations of the headings and row methods.  I don't see
> much real benefit to having the original TableFormatter class
> as used here.  Am I wrong to think this?

You may be right in this specific case and, as you say, Beazley
is probably going to add features that make it more useful.

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.

(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.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list