views:

92

answers:

6

I have observed that most of the design patterns (I refer mostly from the Gang of Four book) are all based on polymorphism. That leads me to a "enlightened" moment that OOP's polymorphism is the most important feature in the paradigm.

Some of the patterns use polymorphism are: strategy, factory, bridge...

With that, I don't understand why we are not just teaching developers really good polymorphic behavior of OOP instead of overloading them with a bunch of patterns which in fact are based on polymorphism?

A: 

Wouldn't

really good polymorphic behavior

actually be how to apply the Gang of Four patterns in a good way?

As in, how to apply strategy as a tool so that you don't violate SRP for example.

Joseph
That's my point! When reading GoF book, there is no mention about polymorphism. Instead readers are just presented with the patterns and what they do. If you have really good knowledge about polymorphism you would probably say "oh, just a bunch of sugar-coded wrapper around polymorphism"
Doug
@Doug The patterns are not "wrappers" around polymorphism. You can make the relation because you use them with polymorphism, but the patterns could also be applied using other principles as well (duck-typing, for example)
Joseph
A: 

Every language has its features that are the "killer features" of the whole concept. OOP, being some of the largest and oldest branch of advanced languages has polymorphism, but I can point out other amazing features such as strong functional programming, closures, parallel execution, and other unique things to other languages that are along the same lines of amazingly-powerful features that are must-haves.
Teaching just one method quickly turns you into a one-trick-pony. A strong foundation in theory allows you to pick up a very "different" language like Lisp or Erlang and know what's going on.

Andrew Arace
A: 

Because, while polymorphism is used to implement the patterns in OO languages, but the patterns can be implemented without it (for example, imagine implementing a factory in C).

Additionally, design patterns are, like polymorphism, tools that help developers solve problems. Only learning a subset of the tools (eg, only polymorphism) will put developers at a disadvantage because they will need to “work from first principals“ (so to speak) instead of basing their work (and their thought) on higher level concepts (like design patterns).

David Wolever
+1  A: 

Well, polymorphism is one of the fundamental concepts of OOP -- it's at a different level of abstraction than the more detailed patterns.

I don't think there is a problem having names for and teaching the more detailed/dependent patterns, it really does help with communication. But as you suggest, a solid understanding of polymorphism is definitely required before a developer would be able to effectively implement any of the dependent patterns.

Guy Starbuck
+1: Yes, basically what Doug is saying is "Oh my god!! Biology is nothing more than chemistry!". He is trying to be a "greedy reductionist". Design-patterns are useful *precisely* because they are polymorphic standards...
rsenna
A: 

This is a red herring. Some languages implement this as polymorphism. Others can implement it as interfaces. Still others can use duck-typing. Forcing readers/students into thinking "polymorphism" doesn't actually help in all situations without also being taught the other two mechanisms, which don't exist in every language. At that point it becomes noise.

Ignacio Vazquez-Abrams
Probably we have distinct interpretations of polymorphism. To me, "Polymorphism" happens when the same contract "abstracts" distinct implementations. So I could argue that both duck-typing (in dynamic languages) and interfaces (in static languages) are just distinct implementations of polymorphism...
rsenna
A: 

@Ignacio Vazquez-Abrams I don't think duck-typing, interface and abstract class are different interpretation of polymorphism. If a implementation can not be determined at compile time and must be defined at runtime, it is polymorphism. You can name that concept anything you like, at the end of the day it is the definition of polymorphism. This is coined late/dynamic binding albeit the same principle.

@user129148: 1) I said that, not Ignacio. 2) Still, you are wrong. Please look at wikipedia: "In strongly typed languages, polymorphism usually means that type A somehow derives from type B, or type C implements an interface that represents type B. In weakly typed languages types are implicitly polymorphic". So your definition of polymorphism is actual *only* on static languages.
rsenna