strategy pattern and non-public virtual functions

Bruno Desthuilliers bdesth.quelquechose at free.quelquepart.fr
Mon Jun 5 22:18:52 EDT 2006


pythoncurious at gmail.com a écrit :
> Hi python experts
> 
> In C++ I can do something like this:
> class Base {
>   public:
>     void f() { this->f_(); }
>   private:
>     virtual void f_() = 0;
> };
> 
> class Derived : public Base {
>   private:
>     void f_() { // Do something }
> };
> 
> int main() {
>     Derived d;
>     d.f();
> }

<ot>
This is eventually the template method pattern, but certainly not the 
strategy pattern.
</ot>


> The point of this is that the a number of classes will inherit from
> Base and only implement a private member function that only will be
> accessed from the base class public 'f' function.
> The Base::f() can then perform validation of input/return values, add
> logging and things like that.

This is usually done in Python with function decorators. But the 
implementer of the derived class

(snip - cf other posts in this thread)

> So my questions are:
> 1. Is there a "pythonic" way to do what I'm trying to do?
 >
> 2. Should I be doing this at all? Any thoughts?

Doing what ? adding logging, validation etc, or using the template 
method pattern ?-)





More information about the Python-list mailing list