90
votes
Accepted
Polymorphism case study - Design pattern for morphing?
I know that this is meant to be a fairly contrived example to demonstrate the idea at play. But I would say that as a rule of thumb, you want to exhaust every possible option before relying on Subtype ...
69
votes
What is polymorphism if you can already have methods that are the same defined in different types?
In your example, you don't really show the same message, you show two different messages that happen to have the same name. Polymorphism requires that the sender of a message can send it without ...
37
votes
Accepted
Separating business logic from data tempts me to use instanceof
Your problem is that you're trying to use both rich domain models
I always try to use polymorphism
and anaemic domain models
I try to separate data from business logic
at the same time. Both are ...
36
votes
How is polymorphism used in the real world?
Stream is great example of polymorphism.
Stream represents a "sequence of bytes that can be read or written". But this sequence can come from file, memory, or many kinds of network connections. Or it ...
33
votes
How to handle the methods that have been added for subtypes in the context of polymorphism?
This is a good question and it's the kind of trouble a lot of people when trying to understand how to use OO. I think most developers struggle with this. I wish I could say that most get past it but ...
33
votes
What is polymorphism if you can already have methods that are the same defined in different types?
Polymorphism is the ability to have objects of different types understanding the same message
This seems like a rather poor explanation of polymorphism to me. Technically correct but not very ...
29
votes
Why the industry prefer/use composition over inheritance?
TL;DR
Attempting to use composition first, before attempting to use inheritance, prevents naive mistakes. Also, Class Inheritance:
Is easy to misuse.
Is less versatile than composition.
Is less ...
22
votes
Polymorphism case study - Design pattern for morphing?
The person, whether married or not is still the same person. Morphing it to another kind of person with a copy replace approach does not maintain this fundamental identity.
Several other ...
22
votes
Separating business logic from data tempts me to use instanceof
Though Philip Kendall's answer has a point, I think it is oversimplifying. One can separate data from behaviour and still use object oriented means and polymorphism.
If you need your data (Bar, Baz) ...
21
votes
Accepted
Should you make use of 'accidental polymorphism'?
Your example is a bit too complicated to make your point. You suggest dynamic dispatch, which actually works pretty much like that... if types get in the way and compile-time is a thing, name-mangling ...
20
votes
Accepted
Is ad-hoc polymorphism a good practice in functional programming?
It seems to be a common cargo cult today to ask if something is "a good practice". Usually, this is the wrong question, since in programming there is almost nothing "good" or "...
19
votes
Accepted
How to handle the methods that have been added for subtypes in the context of polymorphism?
Depends. Unfortunately there is no generic solution. Think about your requirements and try to figure out what these things should do.
For example, you said in the morning different animals do ...
18
votes
Is it ok to have an empty abstract class to make concrete classes polymorphic
public interface IDockable<T>
{
public void Dock(T config);
}
By having a type parameter, you can specify the parameter you want to accept within the Dock method in your classes. Example:
...
15
votes
Accepted
Avoiding instanceof vs abstract God Class. Do I have an alternative?
Don't avoid things just because they are considered evil - understand first why they are considered evil, and then decide how to avoid that evilness.
You are warned about these evil things because ...
15
votes
How do you make a GUI for a polymorphic class?
You can use a visitor pattern:
interface QuestionVisitor {
void multipleChoice(MultipleChoice);
void textBox(TextBox);
...
}
interface Question {
void visit(QuestionVisitor);
}
...
15
votes
Accepted
Good design when polymorphism isn't possible
The problem is we can't modify the components to implement a polymorphic method to display the correct window since it's a extern library.
You may not be able to modify the component, but you can ...
14
votes
Accepted
How to retain the concrete type when writing base-class-oriented code?
This is solved with generics.
internal static class Sorter
{
public static IEnumerable<T> Sort(IEnumerable<T> items) where T : IHasTitle
{
return items.OrderBy(item => ...
12
votes
Accepted
How to go ahead with methods that only one derived class implements from a common interface?
How to go ahead with cases in which ONLY one derived class needs to
implement a method from a common interface?
If you end up in this situation then generally you have a problem with your design ...
11
votes
In what ways is Polymorphism used (other than in arrays)?
The point of polymorphism isn't arrays. It's that you can send an object a message without knowing exactly what kind of object you're talking to.
out.print("Hello world");
This might seem like it ...
11
votes
Should you make use of 'accidental polymorphism'?
The philosophical question of the day: is it really accidental ?
You are telling us as example, that a switch processes events, and that you realise how some of these processing methods have the ...
10
votes
Avoiding instanceof vs abstract God Class. Do I have an alternative?
It seems you have already recognized you could use some polymorphic behavior. You have collections of objects and it would be handy to just order each object to "DoTheThing()" even though the details ...
9
votes
Is it ok to have an empty abstract class to make concrete classes polymorphic
Implementation specific parts shouldn't be part of the interface. Instead take every specific config as a constructor parameter.
public interface IDockable
{
void Dock();
}
public class ...
9
votes
How to handle the methods that have been added for subtypes in the context of polymorphism?
TL;DR:
Think about an abstraction and methods that apply to all subclasses and cover everything you need.
Let's first stay with your eat() example.
It's a property of being a human that, as a ...
9
votes
How to tackle extensibility considering the Data/Object Anti-Symmetry?
This has been termed the Expression Problem by Phil Wadler, although it is much older than the discussion in which he came up with this term. Solving it is one of the "holy grails" of Programming ...
9
votes
Replace Conditional with Polymorphism in a proper way?
You're letting the protocol type itself determine behavior. You want to treat all protocols the same throughout your program except in the implementing class itself. Doing it this way is respecting ...
9
votes
Polymorphism case study - Design pattern for morphing?
The State Pattern could be applied here. But whether there is a better way to model the relationship depends entirely on the needs of the using code. You haven't offered any using code so all I can do ...
9
votes
What is polymorphism if you can already have methods that are the same defined in different types?
So circle1 and rectangle1 understood the same message draw() without using Polymorphism!
What makes you think they are not using Polymorphism?
Am I missing something?
Yes: that what you describe ...
8
votes
What is the difference between Haskell's type classes and Go's interfaces?
There are several differences
Haskell typeclasses are nominatively typed -- you have to declare that Maybe is a Monad. Go interfaces are structurally typed: if circle declares area() float64 and so ...
8
votes
Accepted
switching implementations dynamically according to execution time estimates
The standard approach for, say, sorting a list is to use quicksort for lists greater than a threshold k and insertion sort otherwise. So partitioned lists using quicksort of a certain dimension are ...
8
votes
Accepted
Should I follow "dependency inversion principle" rule even if I need one type only and don't need polymorphism?
Yes, but only if you need it.
The Dependency Inversion Principle is not about making something work now.
It talks about abstractions and concretions but it's not really about that either.
It's ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
polymorphism × 202object-oriented × 65
inheritance × 32
c++ × 31
java × 30
c# × 30
design-patterns × 25
object-oriented-design × 25
design × 23
interfaces × 19
php × 9
python × 7
abstract-class × 7
refactoring × 6
haskell × 6
abstraction × 6
generics × 6
architecture × 5
c++11 × 5
type-systems × 5
javascript × 4
unit-testing × 4
functional-programming × 4
dependency-injection × 4
functions × 4