Timeline for How to adhere to the open-closed principle in practice
Current License: CC BY-SA 3.0
6 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| May 21, 2014 at 18:40 | comment | added | Dunk | With regards to definition of OCP. It might be the industries that I have worked in, which tend to require higher levels of verification than just an ordinary commercial company, but generally speaking if a file/class changes then not only do you need to retest the file/class but everything that makes use of that file/class in your regression testing. So it doesn't matter if someone says polymorphic open/closed is fine, changing the interface has wide ranging consequences so it isn't all that fine. | |
| May 21, 2014 at 18:34 | comment | added | Dunk | Liskov-Substitution principle doesn't come for free but it also doesn't come with much cost either. And usually it pays back far more than it ever costs many times over even if another subclass is never inherited from it in the main application. Applying LSP makes automated testing far easier, which is a win already. Also, while you certainly shouldn't go hog wild and assume everything is going to need LSP, if you are building an application and don't have a good feel for what is likely to need it in a future rev then you don't know enough about your application or its domain. | |
| May 21, 2014 at 14:27 | comment | added | Jeffery Thomas | @Dunk The Liskov-Substitution Principle is useful, but does not come for free. If the original specification only requires a car, then creating a more generic vehicle may not be worth the extra cost of money, time, and complexity. In addition, it is unlikely that a more generic vehicle will be perfectly suited to handle unplanned subclasses. Either the interface for a motorcycle will need to be shoehorned into the vehicle interface (which was designed to only handle car), or you will need to modify vehicle to handle motorcycle (a real violation of open/closed). | |
| May 21, 2014 at 14:15 | comment | added | Jeffery Thomas | @Dunk I based my answer on the polymorphic open/closed principle, not the strict Meyer open/closed principle. It's allowable to update classes to support new interfaces. In this example, the car interface is kept separate from the motorcycle interface. These could be formalized as separate driving abstract classes for car and motorcycle which the implementing class could support. | |
| May 21, 2014 at 13:38 | comment | added | Dunk | Modifying a class to add your new method violates the Open-Closed Principle. Your suggestion also eliminates the ability to apply the Liskov-Substitution Principle to all vehicles that can drive which essentially eliminates one of the strongest parts of OO. | |
| May 12, 2014 at 15:23 | history | answered | Jeffery Thomas | CC BY-SA 3.0 |