Questions tagged [polymorphism]
Polymorphism is about using objects of a type uniformly, regardless of their subtype.
202
questions
1
vote
7
answers
447
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
66
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 ...
-3
votes
2
answers
175
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
499
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
238
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
441
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
300
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
440
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
279
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
162
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
289
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 ...
1
vote
1
answer
3k
views
Polymorphism: Overriding properties versus overwriting attributes
In polymorphism, your base class defines an interface. The inheriting classes fill in the data for that interface. The key is that the interface remains the same.
Given that, if your interface is ...
-1
votes
1
answer
204
views
How to get rid of multiple if statements in code flow related to different modes/datasets - Python? [closed]
My Python project comprises data handlers, models and can be run with different modes.
Throughout my code I use statements like
if mode=='mode1':
# or
if isinstance(model, Model1):
To adapt the flow ...
14
votes
6
answers
5k
views
Separating business logic from data tempts me to use instanceof
class Foo {
abstract doStuff() {}
}
class Bar extends Foo {
doStuff() { ... }
}
class Baz extends Foo {
doStuff() { ... }
}
From a functional perspective doStuff does exactly the same ...
1
vote
1
answer
250
views
Compile-time type-safe access to collection with polymorphic items, and extending it
I would like to have type-safe access to items in a collection with polymorphic items at compile-time. For this, I set up the code below as a proof of concept. Passing the DataItemProps instances to ...