class Person {
private state ="normal" //cripple
run() {
if (this.state === "normal") {
console.log("run")
} else {
console.log("hobble")
}
}
}
//vs
abstract class AttemptRun {
abstract run();
}
class NormalRun extends AttemptRun {
run() {
console.log("run")
}
}
class CrippleRun extends AttemptRun {
run() {
console.log("hobble")
}
}
class Person {
protected runAbility: AttemptRun;
run() {
this.runAbility.run()
}
}
Assuming that I understood the concept here is my question. Why is polymorphism better than if and logic.
Because it seems to me you will still need a factory or another method to set the type of person's ability. So if the logic is not here it's going to be somewhere else.
Why is this repeated some much in books i read, like clean code. It's listed as a code smell.
I feel like it can make unit test a little bit easier because then you only test the abilities and you don't need to test the actual other class that is using it.
Is that all it has to offer.
An if/else and not you need to write a different class and a factory? Hardly seems fair?
Perhaps it's more work but in the long run, it will be better.
What are the weaknesses of each case?
Is this something you would do if it's a small class? Basically, I don't know if I understand the concept, and then assuming i do. How practical is it to use.
A wise developer might use it only when they need something specific. I don't know any of the specifics.