Wednesday, February 10, 2016

Refactoring - Collapse hierarchy

If a hierarchy of classes gets simplified over time, eventually it becomes so simple, that is not really needed. Then, it is time to collapse some classes.

Let's consider the following hierarchy:

class Car {
}

class RegularCar: Car {
}

class HybridCar: Car {
}

The government, in his wisdom, releases a new law to allow only hybrid cars in the market so our hierarchy becomes:

class Car {
}

class HybridCar: Car {
}

This is not useful anymore, so we can simply remove HybridCar and move all the functionality to the superclass, Car.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.