Admittedly I'm loathe to feed the troll, but...
raylopez99 said:
Well, Ciaran *did* give a specific example where it's better. You
just didn't catch it. Try harder.
Ciaran and I disagree. That's not a crime, and in particular it's
pretty common. I still disagree with that example. I don't claim it
won't - just that I prefer to avoid subclassing unless there's a
significant benefit.
Perhaps it's heavy handed, but it works. You 'mind's eye' example
works--in your 'mind's eye'. But if you know anything about how oop
works, you'd know that there's no guarantee that the event driven
nature of C# will work the way you think it works. Here's a 'thought
experiment' for you, Einstein: you subscribe, like you say, just
after construction. But you code for the publisher/subscriber model
is kluge, and takes a long time to run. Meanwhile, other events are
firing right and left. And your program fails, since other stuff is
going on, especially if you're using multi-threaded CPUs in parallel.
When Windows Forms starts executing event handlers in parallel, please
let me know. For one thing, all hell will break loose at that point,
given the thread affinity exhibited by WinForms.
In other words: I think you're the one who ought to learn a bit more
about how event handlers are executed. While it's possible to make them
execute in parallel, that would be very rare and *particularly* odd for
Windows Forms. I can't remember ever seeing *any* event which
parallelised its handlers. Care to provide an example?
OH, I see. You backtracked. Now it's "not common".
No backtracking involved. Let's look at what I wrote previously:
<quote>
Unless you're writing your own control at a low level, you generally
don't need to.
</quote>
Note the "generally". Generally not needing to do something sounds
pretty much like it not being a common requirement, doesn't it?
You use subcribe/ publish much more often--whereas I have never used
it, except to understand how it works.
Really? So every time you use any WinForms control you subclass it, do
you? That sounds unlikely to me.
I suspect you're actually using the pub/sub model without realising it,
if you ever write WinForms or ASP.NET code.
Overriding is conceptually easier, unless you work in a 500 person
team where nobody knows what is being worked on, and a
publish/subscribe scheme makes more sense because you're building
code for a specified series of interfaces.
Inheritance is very limiting, and comes with a number of its own
issues. Pub/sub is cleaner OO design when you fundamentally want to
react to something happening, like a button being clicked.