Extending Python with C++ singleton pattern using boost pytho n lib 
Author Message
 Extending Python with C++ singleton pattern using boost pytho n lib

Quote:
> The good news is that you do NOT need to wrap/expose non canonical
> objects.  A more reliable, flexible, sounder pattern is to wrap
> and expose "lightweight proxy" C++ objects -- that will internally
> delegate whatever is needed to your "one and only substantial
> object", but will NOT implement the singleton Design Pattern with
> the attendant efforts to ensure only one of them is ever subject
> to instantiation (whence the need to avoid copying, etc, etc, and
> hence the non-canonicality).
...
> Over the last week or so, the singleton DP came up for discussion
> a couple of times on this group -- I seem to be in a tiny minority
> in disliking it intensely in favour of featherweight proxies, though
> I know I'm NOT the only one since I recall a C++ Report article
> that made points very similar to mine.  www.google.com/groups should
> let you fetch and read the few relevant articles for background.

Just to add my support to Alex here - we haven't always agreed on everything
(c.f. ''.join() ;) but I'm in complete agreement in this case. Flyweight is
a *very* useful design pattern, whilst *public* Singleton is incredibly
difficult to enforce. You are best to have a private Singleton (i.e. it is
only visible to your code) and expose its interface via a Flyweight.
Simplest way to do this would be to have a non-static class only declared
and implemented and instantiated in the implementation (.cpp) file where
your Flyweight class is implemented. I think (been a while since I've done
any C++ work).

Tim Delaney



Sat, 11 Oct 2003 08:11:39 GMT  
 Extending Python with C++ singleton pattern using boost pytho n lib

    [snip]
Quote:
> > Over the last week or so, the singleton DP came up for discussion
> > a couple of times on this group -- I seem to be in a tiny minority
> > in disliking it intensely in favour of featherweight proxies, though
    ...
> Just to add my support to Alex here - we haven't always agreed on
everything
> (c.f. ''.join() ;) but I'm in complete agreement in this case. Flyweight

is

Ok, not QUITE so tiny:-).

Quote:
> a *very* useful design pattern, whilst *public* Singleton is incredibly
> difficult to enforce. You are best to have a private Singleton (i.e. it is
> only visible to your code) and expose its interface via a Flyweight.

...and then there is no need for the Singleton Design Pattern -- just
instance the 'whatever' only once, it IS your code so that ain't hard:-).

Quote:
> Simplest way to do this would be to have a non-static class only declared
> and implemented and instantiated in the implementation (.cpp) file where
> your Flyweight class is implemented. I think (been a while since I've done
> any C++ work).

Yep, that would work.  Or you may 'risklessly' expose it by putting it
as a private nested class inside your public flyweight class, with
its sole instance as a private static member for the flyweight class
(I think your idea works better though).

Alex



Sun, 12 Oct 2003 00:13:13 GMT  
 Extending Python with C++ singleton pattern using boost pytho n lib

Quote:
> > a *very* useful design pattern, whilst *public* Singleton
> is incredibly
> > difficult to enforce. You are best to have a private
> Singleton (i.e. it is
> > only visible to your code) and expose its interface via a Flyweight.

> ...and then there is no need for the Singleton Design Pattern -- just
> instance the 'whatever' only once, it IS your code so that
> ain't hard:-).

Precisely - this is in essence the Singleton pattern, taken to its most
basic level (there is and will ever be only one instance of the class), but
doesn't require any of the baggage usually associated with the Singleton
design pattern. It's still worthwhile calling it a "Singleton" (because it
is), but no one else needs to know that. "Singleton" is a useful label so
long as people understand that it means there is one and only one instance,
not that "we've got all this code to attempt to prevent you from
instantiating it again, although in some cases it doesn't quite work, so
don't do that" ...

Tim Delaney



Mon, 13 Oct 2003 08:35:26 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Extending Python with C++ singleton pattern using boost python lib

2. copy construktor problem by extending python with c++ using boost python lib

3. boost::python sharing native c++ object with python

4. Singleton Pattern and Python

5. Using Python to extend Msdev (Visual C++)

6. Python and the Singleton Pattern

7. Singleton Pattern and Teaching Python

8. Boost.Python - C++ binding library updated

9. Boost C++ Python Library (formerly py_cpp)

10. boost::python> exposing instances from c++

11. % operator -- did Python or C++/boost come first?

12. Scripting C++ -- Boost.Python vs CORBA vs ???

 

 
Powered by phpBB® Forum Software