[fpc-pascal] Implementing Factory Method with Pascal
Michael Van Canneyt
michael at freepascal.org
Sun Nov 29 09:17:06 CET 2015
On Sun, 29 Nov 2015, Marcos Douglas wrote:
> On Sat, Nov 28, 2015 at 5:34 PM, Michael Van Canneyt
> <michael at freepascal.org> wrote:
>>
>> !! OMG !! Maybe we just discovered a new form of multiple inheritance...
>> Definite proof we should all use C++, or maybe it is time for C+++ !!
>
> You have humor, very funny. [clap clap clap]
>
>> Please...
>>
>> There is no golden rule which encapsulates (sic) all situations.
>
> I agree with you.
>
>> People should use their brain and choose a pattern or methodology
>> judiciously, instead of wasting other people's time with stupid rants on
>> blogs.
>
> As I said before, I put some links just because they have examples to
> simplify what I was saying.
> If you would like, I can search some links "less stupid" for you. Some
> people, more important and influent than me, should have written about
> it too... maybe?
Don't bother.
>
> Now, please...
> Tell us yours arguments. Explain why I'm wrong (and many others on the web).
I did not say you were wrong ? Where did you read that ?
> Inheritance is good... why?
> Inheritance is better than composition... why?
I did not say that inheritance is good, and I did not say that inheritance is better than composition.
Where did you read that ?
They are simply different techniques, and you must choose the one appropriate for the situation you have.
First of all, inheritance is inherent in object oriented programming, since every object descends from a base object.
So saying that 'inheritance must die' is a stupid sentence or title for a blog.
You can read the following everywhere:
Inheritance describes a relation between objects of the type 'is a'
Composition describes a relation between objects of the type 'has a'
When you construct your object tree (if you want a tree) you must
choose your objects and relations in the best possible way.
> What problems we have using composition or inheritance? Which is
> better or simpler to use?
The point I am trying to make is that these are wrong questions to begin with.
Sometimes inheritance is "better", sometimes composition is "better".
The answer depends on the situation.
The good engineer will probably make more appropriate choices than the bad engineer.
An engineer that says 'I will always use inheritance' is rigid, and so is the engineer
that says 'I will always use composition'.
The good engineer chooses the right tool for the job.
The good engineer will understand better when he has to use 'is a' and when to use 'has a'.
You can have a hammer and a saw. They are different tools.
Depending on the situation you will use the hammer or the saw.
No carpenter will say that the hammer is better than the saw.
Or the saw is better than the hammer.
The exact same is true for inheritance and composition.
That said, there are some objective minuses to composition:
- Longer typing. Composition:
MyObject.MyHasABehaviour.DoSomething
Inheritance:
MyObject.DoSomething
- Composition results in slower code (1), since the program must always dereference and fetch an extra object.
- Composition results in more memory being used. Inheritance puts everything in 1 block: parent+child.
Composition uses 2 blocks, plus a pointer to connect the two. Every memory block introces an extra area.
If you have a really bad designed hierarchy, then the 'child' object needs an extra pointer to the 'parent'.
Again extra memory.
- Composition results in slower code (2) because the extra memory blocks must be managed.
You can argue that this is irrelevant these days: well, it is relevant,
because the effect of this is exponential, not linear.
Not a technical argument, but nevertheless: If you think about it,
composition is just some syntactic sugar for procedural programming,
just as they do in C.
>
> If I'm wrong and you, using valid arguments, explain to me where are
> my mistakes, I will thank you for teaching me some new for me. But if
> you have only jokes please don't waste my time to read and answer you.
I thought it was obvious that my joke was aimed at the authors of the web pages you posted, not at you.
Most likely, they were frustrated by having to use wrongly designed classes.
If I have offended you, I apologize for that, this surely was not my intention.
Michael.
More information about the fpc-pascal
mailing list