In , Bertrand Meyer proposed a guiding principle to alleviate this problem. That is, modules should be written so that they can be extended, without requiring them to be modified. In other words, changing what the modules do without changing the source code of the modules. In object-oriented programming, these two seemingly opposing requirements can be achieved in various ways. This often requires separating the specification interface of a module from its implementation.
It requires that a software artifact should be open for extension , but closed for modification. To fulfil this requirement, we could apply inheritance or better yet, introduce a layer of abstraction with different implementations in our design to avoid tight coupling between particular classes.
Nevertheless, it is worthwhile to follow the Open-Closed Principle as far as possible, as it encourages us to develop cohesive, loosely coupled components.
In a distributed system, many services can be involved in creating a response to a single request. In this article, I share a bit about my successes and failures as a creator of code, text, and coming up a SaaS product.
Hence for applications to function gracefully, they need to consume APIs elegantly and consistently. Software Craft. In object-oriented programming, OCP can be achieved in various ways. This often requires separating the specification i. In the design given below, the behavior of the CommandQueue class can be altered by adding more concrete Command subclasses.
For example, by including a Delete class alongside List , Sort , and Reset , the CommandQueue can now perform delete commands without modifying its code at all. That is, its behavior was extended without having to modify its code. Hence, it is open to extensions, but closed to modification.
The behavior of a Java generic class can be altered by passing it a different class as a parameter. In the code below, the ArrayList class behaves as a container of Students in one instance and as a container of Admin objects in the other instance, without having to change its code.
That is, the behavior of the ArrayList class is extended without modifying its code. We used this principle in the example application to control different kinds of coffee machines via our CoffeeApp. As long as a coffee machine implements the CoffeeMachine interface, you can control it via the app.
The only thing you need to do when you replace your existing coffee machine is to provide a new implementation of the interface and change the main method which instantiates the specific implementation. If you want to take it one step further, you can use dependency injection, reflection or the service loader API to replace the instantiation of a specific class.
With APM, server health metrics, and error log integration, improve your application performance with Stackify Retrace.
Try your free two week trial today. Click here to read more about the acquisition. Try Our Free Code Profiler. Try Our Code Profiler. By Role. By Technology. By Language. Documentation Support Ideas Portal Menu. Start Free Trial. Tip: Find application errors and performance problems instantly with Stackify Retrace.
Troubleshooting and optimizing your code is easy with integrated errors, logs and code level performance insights.
0コメント