Consider two classes Dog
and Cat
both conforming to Animal
protocol (in terms of Swift programming language. That'd be interface in Java/C#).
We have a screen displaying a mixed list of dogs and cats. There's Interactor
class that handles logic behind scenes.
Now we want to present a confirmation alert to the user when he wants to delete a cat. However, dogs need to be deleted immediately without any alerts. The method with conditionals would look like this:
func tryToDeleteModel(model: Animal) {
if let model = model as? Cat {
tellSceneToShowConfirmationAlert()
} else if let model = model as? Dog {
deleteModel(model: model)
}
}
How can this code be refactored? It obviously smells