1. Introduction.Last week I facilitated a guided kata for a Gran Canaria Ágil event in Aplicaciones Informáticas Domingo Alonso (thanks to both for inviting me) in which I explained a possible way to solve Luca Minudel's Tire Pressure Monitoring System exercise.
This exercise is part of his TDD with Mock Objects: Design Principles and Emergent Properties exercises.
I like these exercises very much because they contain clear violations of the SOLID principles but they are sill small enough to be able to finish the refactoring in a short session at a slow pace. This makes possible to explain, answer questions and debate about design principles, dependency-breaking techniques and refactoring techniques as you apply them.
I'd like to thank Luca Minudel for creating and sharing these great exercises.
I co-facilitated this exercise with Álvaro García several month ago in a Software Craftsmanship Barcelona event and we received a lot of positive feedback.
That time, many people couldn't attend due to space limits, so I'd like to make a summary of the exercise here for them.
2. The initial code.The initial code has two classes: Alarm and Sensor.
The exercise focuses on refactoring the Alarm class.
3. SOLID violations, hidden dependencies.All the logic of the Alarm class is in the check method.
Alarm's main problem is that it depends on a concrete class, the sensor that measures pressure values.
This is a clear violation of the Dependency Inversion principle (DIP) which states that:
Abstractions should not depend on details.To make things worse this dependency is hidden inside Alarm's check method (see J. B. Rainsberger's The Pain of Implicit Dependencies post).
Details should depend on abstractions.
The violation of the DIP and the implicit dependency makes also impossible to fulfill the Open Closed Principle (OCP).
Fulfilling those two principles will be one of the main goals of this refactoring.
This is the first post in a series of posts about solving the Tire Pressure Monitoring System exercise in Java:
- Solving the Tire Pressure Monitoring System exercise (I)
- Solving the Tire Pressure Monitoring System exercise (II)
- Solving the Tire Pressure Monitoring System exercise (III)
- Solving the Tire Pressure Monitoring System exercise (IV)