mailRe: Vote for the design of the relax data storage object singleton.


Others Months | Index by Date | Thread Index
>>   [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Header


Content

Posted by Chris MacRaild on March 15, 2007 - 15:29:
On Fri, 2007-03-16 at 01:00 +1100, Edward d'Auvergne wrote:
On 3/15/07, Chris MacRaild <c.a.macraild@xxxxxxxxxxx> wrote:
On Thu, 2007-03-15 at 00:09 +1100, Edward d'Auvergne wrote:
On 3/14/07, Chris MacRaild <c.a.macraild@xxxxxxxxxxx> wrote:
On Tue, 2007-03-13 at 19:51 +1100, Edward d'Auvergne wrote:
Gary,

I've written some unit tests which are located in the file
'test_suite/unit_tests/data/__init__.py'.  Would you know what
modifications I need to apply to make these tests run?  These tests
are for the methods of the Data singleton class (the relax data
storage object) which is located in 'data/__init__.py'.  I haven't
used the statement 'from x import Data' for this class so it is the
legal and ubiquitous usage of an __init__.py file.  Would
'unit_test_runner.py' handle this code?

Thanks,

Edward


P.S.  These tests demonstrate the utility of subclassing the 
singleton
and is one reason I was arguing for the use of the class import 
rather
than reference import.


It is of course correct that the Singleton implimentation I proposed
does not allow for subclassing. This however might well be seen as a
design feature, given the well known conceptual problems with
subclassing Singleton. Essentially, subclassing violates the Singleton
Design Pattern:

class Singleton:
    def __new__(self):
        ...

class Sub(Singleton): pass

a = Singleton()
b = Sub()
isinstance(a,Singleton)
True
isinstance(b,Singleton)
True
a is b
False
a == b
False

ie. we have 2 instances of Singleton that are not the same thing, and
not even equal! If this is the behaviour you want, then you need
something other than Singleton. On the other hand, if you really want
Singleton, then you can't also hope for subclassability.

Actually one is an instance of Singleton and the other is an instance
of Sub.

The definition of inheritance is that an instance of the subclass is
also an instance of the parent class. ie. b is an instance of both Sub
and Singleton. Inheritance from a Singleton class is therefore a logical
contradiction.

That is actually incorrect! 

Sorry to drag this on, but I'm not incorrect here, and its a fairly
fundamental point of OO philosophy and practice that is well worth
getting right. This is pasted directly from my Python command line (the
interpreter does not lie!):

class A: pass
...
class B(A): pass
...
a= A()
b = B()
isinstance(a,A)
True
isinstance(a,B)
False
isinstance(b,B)
True
isinstance(b,A)
True

Because B is a subclass of A, all instances of B are also instances of A


 The purpose of inheritance and having a
subclass is to define a common set of operations and behaviours of an
object (the singleton property is a behaviour).  The derived classes
then expand on these properties differently for each new object.  Each
derived class is then a new and different instance, even if they are
singletons.  If two instances of the same derived singleton class are
made, then these will be same reference.  More info about inheritance
is given at http://en.wikipedia.org/wiki/Inheritance_(computer_science).


My vote is for B

As the vote is 2 for B and 1 for A, option B will be adopted.  Chris,
would you like to make the necessary modifications?  The file
'data/__init__.py' will need to be modified.  In addition all the
import statements of the relax data storage object in all the relax
modules will need to be changed.  A few of the unit tests in the file
'test_suite/unit_tests/data/test___init__.py' need to be redesigned as
these test the methods of the relax data storage singleton by creating
a new singleton object, with the same properties as the storage
singleton, as a derived class.



Sure, I'll give it a shot.


Chris

Cheers,

Edward





Related Messages


Powered by MHonArc, Updated Thu Mar 15 16:00:43 2007