473,382 Members | 1,717 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

multiple extensions to a class

Hello, I'm experimenting with different ways of extending a class (for a
plug-ins framework for a GUI) with more than one extension when some of
these extensions need to collaborate, but others mustn't know about each
other.

I.e., if I have a class A, and I want to add a block of functionality, I
can derive it into a B that adds that fucntionality. If I want to add more
functionality, I can derive B into a C. But if I want to add a third bit of
functionality D directly to A, such that D knows nothing about B or C, I
won't be able to instantiate an object that has all three extensions, namely
an "ABCD". It will either be an "ABC" or an "AD".

One possibility would be to define the extensions B,C and D as functions and
add those functions to A with new.instancemethod. Any other ideas on how to
do this? E.g. is it possible to *add* base classes to a class? I could add
the extensions by adding them as base classes to A.

Thanks,
Oliver

Jul 18 '05 #1
12 1539
Humpty Dumpty wrote:
Hello, I'm experimenting with different ways of extending a class (for a
plug-ins framework for a GUI) with more than one extension when some of
these extensions need to collaborate, but others mustn't know about each
other.

I.e., if I have a class A, and I want to add a block of functionality, I
can derive it into a B that adds that fucntionality. If I want to add more
functionality, I can derive B into a C. But if I want to add a third bit of
functionality D directly to A, such that D knows nothing about B or C, I
won't be able to instantiate an object that has all three extensions, namely
an "ABCD". It will either be an "ABC" or an "AD".

One possibility would be to define the extensions B,C and D as functions and
add those functions to A with new.instancemethod. Any other ideas on how to
do this? E.g. is it possible to *add* base classes to a class? I could add
the extensions by adding them as base classes to A.

Thanks,
Oliver


I do a template class using a function:

def B(baseclass):
class B(baseclass):
def f(self, x, y):
# do stuff
super(B, self).f(x, y)
return B

def C(baseclass):
class C(baseclass):
def f(self, x, y):
# do stuff
super(C, self).f(x, y)
return C

extendedA = C(B(A))
extendedA.f(3,4)

I think you can find recipes like this in the Python cookbook...

David
Jul 18 '05 #2
Humpty Dumpty wrote:
One possibility would be to define the extensions B,C and D as functions and
add those functions to A with new.instancemethod. Any other ideas on how to
do this? E.g. is it possible to *add* base classes to a class? I could add
the extensions by adding them as base classes to A.


You might consider making class A into a container of some sort -- i.e.,
give it someplace to store a list of "decorator" objects, and then make
B, C, and D into such decorators (rather than subclasses of A). You'll
have to arrange some way of dispatching messages to A that are intended
for the decorators, and possibly some way for the decorators to talk to
each other. (It's hard to say whether you'd be better off making B and
C separate decorators, or whether to subclass C from B -- depends on
your specific application.)

Jeff Shannon
Technician/Programmer
Credit International

Jul 18 '05 #3
Jeff Shannon wrote:
Humpty Dumpty wrote:
One possibility would be to define the extensions B,C and D as
functions and
add those functions to A with new.instancemethod. Any other ideas on
how to
do this? E.g. is it possible to *add* base classes to a class? I could
add
the extensions by adding them as base classes to A.


You might consider making class A into a container of some sort -- i.e.,
give it someplace to store a list of "decorator" objects, and then make
B, C, and D into such decorators (rather than subclasses of A). You'll
have to arrange some way of dispatching messages to A that are intended
for the decorators, and possibly some way for the decorators to talk to
each other. (It's hard to say whether you'd be better off making B and
C separate decorators, or whether to subclass C from B -- depends on
your specific application.)

The advantage of Python here is that you don't need to subclass one from
the other - just make a clear definition of the methods you expect, and
then have a system where you call methods on a list of listening classes...

David
Jul 18 '05 #4
"Humpty Dumpty" <ol***************@utoronto.ca> wrote in message news:<dV*********************@news20.bellglobal.co m>...
Hello, I'm experimenting with different ways of extending a class (for a
plug-ins framework for a GUI) with more than one extension when some of
these extensions need to collaborate, but others mustn't know about each
other.

I.e., if I have a class A, and I want to add a block of functionality, I
can derive it into a B that adds that fucntionality. If I want to add more
functionality, I can derive B into a C. But if I want to add a third bit of
functionality D directly to A, such that D knows nothing about B or C, I
won't be able to instantiate an object that has all three extensions, namely
an "ABCD". It will either be an "ABC" or an "AD".

One possibility would be to define the extensions B,C and D as functions and
add those functions to A with new.instancemethod. Any other ideas on how to
do this? E.g. is it possible to *add* base classes to a class? I could add
the extensions by adding them as base classes to A.

Thanks,
Oliver


I am not sure about your question. It seems you want mixins. Do you want
the ability to add a plugin dynamically and add new methods to all
your objects at run-time, including previously instanced objects?
It can be done. Or maybe you want to decide the plugins to use at import
time, i.e. you want "import_with_metaclass". In this case you may
google this newgroup and look at this article:

http://www-106.ibm.com/developerwork.../l-pymeta.html

(there is also a second one http://www-106.ibm.com/developerworks/library/l-pymeta2)

HTH,

Michele Simionato
Jul 18 '05 #5
Humpty Dumpty wrote:
I.e., if I have a class A, and I want to add a block of functionality, I
can derive it into a B that adds that fucntionality. If I want to add more
functionality, I can derive B into a C. But if I want to add a third bit
of functionality D directly to A, such that D knows nothing about B or C,
I won't be able to instantiate an object that has all three extensions,
namely an "ABCD". It will either be an "ABC" or an "AD".


This sounds like the following inheritance tree would be the obvious answer:

class A: pass
class B: pass
class C: pass
class D: pass

class AB(A, B): pass
class ABC(A, B, C): pass
class AD(A, D): pass

I may still be misunderstanding something...

Peter

Jul 18 '05 #6
On Thu, 8 Jul 2004 23:01:52 -0400, Humpty Dumpty
<ol***************@utoronto.ca> wrote:
Hello, I'm experimenting with different ways of extending a class (for a
plug-ins framework for a GUI) with more than one extension when some of
these extensions need to collaborate, but others mustn't know about each
other.

I.e., if I have a class A, and I want to add a block of functionality, I
can derive it into a B that adds that fucntionality. If I want to add more
functionality, I can derive B into a C. But if I want to add a third bit of
functionality D directly to A, such that D knows nothing about B or C, I
won't be able to instantiate an object that has all three extensions, namely
an "ABCD". It will either be an "ABC" or an "AD".

One possibility would be to define the extensions B,C and D as functions and
add those functions to A with new.instancemethod. Any other ideas on how to
do this? E.g. is it possible to *add* base classes to a class? I could add
the extensions by adding them as base classes to A.


maybe I'm missing something, but wouldn't a class E(ABC, AD) work?
--
John Lenton (jl*****@gmail.com) -- Random fortune:
bash: fortune: command not found
Jul 18 '05 #7
From: "John Lenton" <jl*****@gmail.com>
I.e., if I have a class A, and I want to add a block of functionality, I can derive it into a B that adds that fucntionality. If I want to add more functionality, I can derive B into a C. But if I want to add a third bit of functionality D directly to A, such that D knows nothing about B or C, I
won't be able to instantiate an object that has all three extensions, namely an "ABCD". It will either be an "ABC" or an "AD".

One possibility would be to define the extensions B,C and D as functions and add those functions to A with new.instancemethod. Any other ideas on how to do this? E.g. is it possible to *add* base classes to a class? I could add the extensions by adding them as base classes to A.


maybe I'm missing something, but wouldn't a class E(ABC, AD) work?

No because B,C and D are discovered at run-time only, ie class E can't be
known until runtime. You'd have to create a string with the code and use
exec or eval or s/t similar.

Oliver
Jul 18 '05 #8

"Peter Otten" <__*******@web.de> wrote in message
news:cc*************@news.t-online.com...
Humpty Dumpty wrote:
I.e., if I have a class A, and I want to add a block of functionality, I can derive it into a B that adds that fucntionality. If I want to add more functionality, I can derive B into a C. But if I want to add a third bit
of functionality D directly to A, such that D knows nothing about B or C, I won't be able to instantiate an object that has all three extensions,
namely an "ABCD". It will either be an "ABC" or an "AD".
This sounds like the following inheritance tree would be the obvious

answer:
class A: pass
class B: pass
class C: pass
class D: pass

class AB(A, B): pass
class ABC(A, B, C): pass
class AD(A, D): pass


Which one offers "ABCD"? Plus, as I just mentioned in a reply to another
poster in this thread, B,C and D are discovered at run-time so anything
based on class Something(derived from combination of A,B,C,D) is doomed
since derivation is not dynamic.

Oliver
Jul 18 '05 #9
From: "David Fraser" <da****@sjsoft.com>
Jeff Shannon wrote:
Humpty Dumpty wrote:
One possibility would be to define the extensions B,C and D as functions and add those functions to A with new.instancemethod. Any other ideas on how to do this? E.g. is it possible to *add* base classes to a class? I could add the extensions by adding them as base classes to A.
You might consider making class A into a container of some sort -- i.e.,
give it someplace to store a list of "decorator" objects, and then make
B, C, and D into such decorators (rather than subclasses of A). You'll
have to arrange some way of dispatching messages to A that are intended
for the decorators, and possibly some way for the decorators to talk to
each other.
Interesting idea, could be done... how does this differ from the mixin
approach?
(It's hard to say whether you'd be better off making B and
C separate decorators, or whether to subclass C from B -- depends on
your specific application.)

I could probably create a forwarding mechanism so that if a class doesn't
understand something, it passes on the request to each decorator in a list.
Then C could just be a decorator of B, just like B is of A.
The advantage of Python here is that you don't need to subclass one from
the other - just make a clear definition of the methods you expect, and
then have a system where you call methods on a list of listening

classes...
Yes, but you have to be careful with that in terms of cohesion: though you
want to allow for extendability through plug-ins, you don't want to
encourage a mish mash of unrelated classes that send messages around. It's a
technique to keep in mind, but I want to maintain aggregation of logic and
function and data as much as possible, ie aim for "the right balance" :)

Oliver
Jul 18 '05 #10
"Humpty Dumpty" <ol***************@utoronto.ca> wrote in message news:<Jq*******************@news20.bellglobal.com> ...

No because B,C and D are discovered at run-time only, ie class E can't be
known until runtime. You'd have to create a string with the code and use
exec or eval or s/t similar.


Not at all. You can create classes at run-time with "type":

type("MyDynamicClass",bases_set_at_run_time,dict_s et_at_run_time)

Michele Simionato
Jul 18 '05 #11
Humpty Dumpty wrote:
class AB(A, B): pass
class ABC(A, B, C): pass
class AD(A, D): pass


Which one offers "ABCD"? Plus, as I just mentioned in a reply to another
poster in this thread, B,C and D are discovered at run-time so anything
based on class Something(derived from combination of A,B,C,D) is doomed
since derivation is not dynamic.


The idea was that the classes you would actually use are arbitrary
combinations of "partial implementation" classes that bear no inheritance
relationships.
Seems like I misunderstood your problem and you were after the technical
means to perform this combination at runtime. - Michele has just shown how
to achieve that with type().

Peter

Jul 18 '05 #12
Humpty Dumpty wrote:
From: "David Fraser" <da****@sjsoft.com>
Jeff Shannon wrote:

You might consider making class A into a container of some sort -- i.e.,
give it someplace to store a list of "decorator" objects, and then make
B, C, and D into such decorators (rather than subclasses of A). You'll
have to arrange some way of dispatching messages to A that are intended
for the decorators, and possibly some way for the decorators to talk to
each other.

Interesting idea, could be done... how does this differ from the mixin
approach?


Mixins are all derived from the parent class. Combining multiple mixins
requires (as you've noted) knowing ahead of time which combinations
you'll need, and creating a derived class that includes all of the
necessary mixins. (Python does include enough facilities that you could
do this at runtime, but I'm personally rather leery about using this
level of deep magic unless it's truly necessary.)

Decorators, on the other hand, are designed to work with a particular
class, but are not linked directly in an inheritance tree. There might
be a fair amount of coupling between the decorator and the class that
it's designed to decorate, but not necessarily. (IIRC, the Gang of Four
used an example based on a windowing toolkit -- a "border" class could
be designed as a decorator; it wouldn't care what variety of "window"
class it was decorating, and the "window" class wouldn't care what
variety of "border" it carried. The communication protocol between them
would be fairly straightforward and wouldn't depend on the specific
implementation of either the decorator or the decorated class.)
Yes, but you have to be careful with that in terms of cohesion: though you
want to allow for extendability through plug-ins, you don't want to
encourage a mish mash of unrelated classes that send messages around. It's a
technique to keep in mind, but I want to maintain aggregation of logic and
function and data as much as possible, ie aim for "the right balance" :)


Definitely a balance to be had, here. On the whole, though, I don't see
an overgrown inheritance tree to be more maintainable than coherent set
of decorators. Whether the classes are related or not makes scant
difference when there's a mish-mash of them sending messages around. ;)
To my mind, an inherited mixin class is conceptually no simpler than an
aggreggated helper class, and depending on the specific application
aggreggated helpers may result in a cleaner, less-cluttered design. In
particular, for the case where varying sets of additional features will
be needed and those needs won't be known until runtime (i.e., your
case), aggreggation is better able to deal with the dynamic nature of
the requirements.

What I'm envisioning is a system wherein class A contains a list of
decorators. You could probably add and remove decorators at will;
certainly it'd be trivial to pass a list of decorators when A is
instantiated. Now, whenever a significant state change occurs in A, it
will iterate over that list, calling a generic method on each decorator
and passing itself as a parameter:

for dec in self.decorators:
dec.Go(self)

Then the Go() method on each decorator can inspect class A for the
attributes that it understands, and act on them as appropriate. This
does require some coupling between A and the decorators, as they must
both follow a particular protocol, but it doesn't require that either
side of the protocol be a specific class. This means that if you later
extend A in another way, all subclasses of A will work with the same set
of decorators that A does... and a complete replacement for A, not
rooted in the same inheritance tree, will work just as well.

Jeff Shannon
Technician/Programmer
Credit International

Jul 18 '05 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Alphonse Giambrone | last post by:
Is there a way to use multiple search patterns when calling Directory.GetFiles. For instance Directory.GetFiles("C:\MyFolder", "*.aspx") will return all files with the aspx extension. But what if...
6
by: G.Ashok | last post by:
Hi, Does anybody know how Multiple polymorphism can be done in VB.NET or DOT doesn't support it? Is there any third party extensions available like for Java to do this? Regards, ....Ashok...
3
by: Chris Paul | last post by:
I'm having trouble with PHP & PostgreSQL/OpenLDAP/Apache on Windows. I've set this up countless times on BSD (piece of cake) but I'm trying to do this on Windows now so that my developer can work...
10
by: musosdev | last post by:
Hi guys I'm trying to migrate to VS2005... I've managed to do that, but realised I'd opened my web projects as file projects, and I'm getting the error about network BIOS command limit. ...
10
by: Noozer | last post by:
I'm writing an ASP application and have a noob question... I have a class that access an MS SQL database. I have another class also accesses an MS SQL database and this second class uses objects...
14
by: dl | last post by:
I have two classes, say A and B, both having a data member 'int n'; private in A, public in B. When I derive class C from both public A and public B, B::n should be visible to C while A::n...
2
by: kelvin.koogan | last post by:
Our company has a mix of users with VS2003 & VS2005. I'm am having difficulty working out how components written with these 2 tools can be written together. I want to be able to a) incorporate a...
3
by: Davo1977 | last post by:
Does anyone know a regular expression that will rename multiple files that have different extensions to have the same extension. For example, you could use this code when several text files exist in...
0
by: Adam Salisbury | last post by:
**To members of microsoft.public.dotnet.framework, apologies for the crosspost. I originally posted this message into that group however have since realised this may have been a better...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.