strategy pattern and non-public virtual functions
Maric Michaud
maric at aristote.info
Mon Jun 5 12:22:54 EDT 2006
Le Lundi 05 Juin 2006 16:07, pythoncurious at gmail.com a écrit :
> 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();
> }
This is just polymorphism, not strategy pattern, and I would expect f_ to be
protected here not private.
You want to ensure derived class will use a given method in the Base class,
this could be done explicit with a good naming convention as Duncan said, but
here is a strategy pattern to ensure a sanity check for example :
class strategyBase(object) :
def __call__(self, *sa) : raise NotImplementedError('abstract class')
class InputProcessor(object) :
def sanitize(self, *a) :
return a
def f(self, *a) :
sa = self.sanitize(*a)
return self.strategy(*sa)
def __setStrategy(self, strat) :
if not isinstance(strat, strategyBase) :
raise ValueError("strat must be of type strategyBase")
self.__strat = strat
strategy = property(fget=lambda s : s.__strat, fset=__setStrategy)
The main purpose of this is to define a common API for all Strategies, and
this is really useful if you intend to manage many of them.
--
_____________
Maric Michaud
_____________
Aristote - www.aristote.info
3 place des tapis
69004 Lyon
Tel: +33 426 880 097
More information about the Python-list
mailing list