2. The Open/Closed Principle
“software entities
(classes, modules, functions, etc.)
should be open for extension,
but closed for modification”
that is, such an entity can allow its
behaviour to be modified without altering
its source code
4. OCP kata
0. Write the first failing test.
Then write a factory that returns an object,
or an aggregate of objects,
that make the test pass.
The factory should be limited to creating objects
and linking them together.
No conditionals allowed in the factory.
5. Factory test example
@Test
public void simpleAddition() {
Calculator calc = new CalculatorFactory().create();
assertEquals(7, calc.eval(“3 + 4”));
}
7. OCP kata
2. Can you make it pass by changing the factory
and/or creating a new class and nothing else?
If yes, great! Go back to 1.
If not, refactor until you can.
The refactoring should bring the system to a state
where it’s possible to implement the next test just
by changing the aggregate of objects that is
returned by the factory. Be careful not to implement
new functionality; the current test must still fail.
Remember: no conditionals in the factory.
(For extra bonus points: no conditionals anywhere!)
8. tests
Credits:
Matteo Vaccari invented the OCP kata:
http://matteo.vaccari.name/blog/archives/293
http://www.slideshare.net/xpmatteo/20101125-ocpxpday
Dave Nicolette created the jigsaw drawing