Questions tagged [polymorphism]
Polymorphism is about using objects of a type uniformly, regardless of their subtype.
206 questions
0 votes
2 answers
168 views
A class that implements two interfaces that extend the same interface [duplicate]
I'll get straight to the point. I want to implement a class structure in Java similar to the one in the image. Am I falling into bad practice with this kind of diamond-shaped interface implementation?...
4 votes
1 answer
269 views
How do I handle polymorphic domain models in a Clean Architecture data access layer?
Let's say I'm making a space 4X game (because I am!) and I have an ISpaceObject interface in my domain layer, with a number of classes such as Ship, Starbase, Planet, Wormhole, etc. implementing it. I ...
5 votes
5 answers
1k views
Handling class specific behavior: polymorphism vs instanceof
I'm designing a system with different types of components, and I'm trying to decide whether to use polymorphism or instanceof checks for handling type-specific behavior. I see two possible approaches: ...
7 votes
8 answers
974 views
Why is "one function do more than one thing" a disadvantage of "boolean parameter", but not in "polymorphism"?
According to Is it wrong to use a boolean parameter to determine behavior?, I know using boolean parameters to decide the behaviour is bad, for example, when using boolean parameters as the following: ...
1 vote
7 answers
572 views
How do I cleanly keep track of the type of various objects implementing a common interface without reflection?
In my multiplayer game I keep track of each player's inventory. I have a class for each inventory action that extends the abstract class InventoryItem. I then polymorphically call use() on the ...
2 votes
0 answers
95 views
How to deal with implementation specific implementation accross interface boundaries
What to do if you end up in the following situation The intent here is that b and c can be substitutable for a. The client creates a bar through foo_1, which later may be passed back to foo_1 through ...
-2 votes
2 answers
380 views
Polymorphism with variable default argument count
I'm in the process of writing a library in Python, and I've run into a design problem concerning the use of polymorphism. I have an ABC with abstract method 'foo': class A(ABC): @abstractmethod ...
2 votes
3 answers
1k views
Should I still "replace conditional with polymorphism" if the condition is from dynamic load data?
I know there are already some questions about replacing if else with polymorphism, for example: Applying Replace Conditional with Composition in functional programming Is it wrong to use any type of ...
2 votes
3 answers
330 views
How to retain the concrete type when writing base-class-oriented code?
A scenario often arises when attempting to make some existing code reusable. I introduce an interface to represent the commonality between some new feature I'm implementing and some existing ...
2 votes
1 answer
730 views
Domain models: Can they be an abstract class
When talking about having a rich domain model successfully, in real applications, it needs to, somehow, access some abstraction of complex functionality (instead of being a row state calculator the ...
3 votes
1 answer
407 views
How are interfaces implemented behind the scenes in the Go language?
I have read this article which indicates a double tuple structure, but it is unfortunately light on implementation details, which is what I am looking for. So... how are interfaces implemented in Go? ...
0 votes
0 answers
606 views
What is the Best Practice for handling multiple Entities that behave identically?
Because I have multiple entities with unique fields, I need multiple repositories for each one even though each Entity will be handled exactly the same. What is the best way to handle these separate ...
-1 votes
1 answer
356 views
Avoid use of the visitor pattern in this very common scenario
Let's assume we need to send a message, and to do so we would like our client to be concerned only with constructing the message (DTO) and using a facade service to send it. We already know that we ...
0 votes
1 answer
215 views
How to decide what view to show if I use polymorphism in the Model?
I am making a game of Monopoly. I call a method in my Board class which returns the current players square object! E.g Old Kent Road. Euston, Chance , Free parking. I use polymorphism to decide upon ...
2 votes
3 answers
357 views
how to leverage overloading while keeping business logic out of models
I am running into a conflict between two best practice principles- overloading should be used instead of long chained if/else statements models should not contain business logic I am working on a ...