hi,
I have a rather abstract question. I was doing some experiments and now I'm stuck to decide which method to use : object or class "composition" (if I can use that word, OO gurus will tell).
For the sake of experiment let's say I want to build a Server which we want to be able to handle tcp/ip,streams etc.. (look at this just as simplified example, but don't forget the subject of the message :), this is my real question) . One of the things it will do is as you may expect is to send and recieve messages, so let's do it like this (in pseudo language):
class SendRecv {
send{}
recv{}
}
class Server {
use base qw(SendRecv)
....
}
As you see from this code I'm composing the Server with something like interface..now if i make many different SendRecv-classes I can compose different servers that will handle all types of data.Not only that i can also send/recv data differently i.e. I may also do compression, encryption ..etc..w/o modifyng the Server-class
Now the other approach that many will use probably look like this :
class SendRecv {
send{}
recv{}
new{}
}
class Server {
use SendRecv;
new {
my $sr = SendRecv->new();
bless $class, { pool => [$sr] }
}
....
}
i.e. object-composition..
The second way in this concentrate example seems better solution or at least easy to comprehend, but it also adds more runtime overhead. I have now to store pool of objects and add additional logic to support them. Also I have to add alot of additional logic inside these "object-classes" (f.e. new()-method).
One other drawback of this approach is that for any distinct functionality I will have to add another pool of objects (say I want to add IO operation that are not specific i.e. be able to store messages in file, redirect message to anoter server, steram them..etc.)
On the other hand the class-composition wont work 'cause I can have only one way of handling send/recv depending which interface-class I'm inheriting i.e. at runtime i will have to create pool of Servers !! Also this method gives you much leaner and clear way to compose the final Server class, 'cause all of the functionality can be developed separately and added in a LEGO-like way
I know that most of the things I mentioned here has to be decided on the per-project basis, depending on your final design.
But what if the final design is not entirily clear and you want to have both options available at any time..;)
Is there some middle-ground where you can have both methods, but still don't need to write alot of code
What is your opinion ? Sorry if I ask a trivial question that probably was solved somewhere.
Also of cource I'm interested for perl elaboration on the problem.