1

Open for extension, closed for modification as an architectural pattern

 2 years ago
source link: https://ayende.com/blog/194369-B/open-for-extension-closed-for-modification-as-an-architectural-pattern?Key=f1781074-3cc9-42f2-9982-b84860904968&utm_campaign=Feed%3A+AyendeRahien+%28Ayende+%40+Rahien%29
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Jul 30 2021

Open for extension, closed for modification as an architectural pattern

time to read 5 min | 955 words

The Open Closed Principle is part of the SOLID principles. It isn’t new or anything exciting, but I wanted to discuss this today in the context of using that not as a code artifact but as part of your overall architecture.

The Open Closed Principle states that the code should be opened for extension, but closed for modification. That is a fancy way to say that you should spend most of your time writing new code, not modifying old code. Old code is something that is known to be working, it is stable (hopefully), but messing around with old code can break that. Adding new code, on the other hand, carry far less risk. You may break the new things, but the old stuff will continue to work.

There is also another aspect to this, to successfully add new code to a project, you should have a structure that support that. In other words, you typically have very small core of functionality and then the entire system is built on top of this. 

Probably the best example of systems that follow the Open Closed Principle is the vast majority of PHP applications.

Hold up,I can hear you say. Did you just called out PHP as an architectural best practice? Indeed I did, and more than that, the more basic the PHP application in question, the closer it is to the ideal of Open Closed Principle.

Consider how you’ll typically add a feature to a PHP application. You’ll create a new script file and write the functionality there. You might need to add links to that (or you already have this happen automatically), but that is about it. You aren’t modifying existing code, you are adding new one. The rest of the system just know how to respond to that and handle that appropriately.

Your shared component might be the site’s menu, a site map and the like. Adding a new functionality may occasionally involve adding a link to a new page, but for the most parts, all of those operations are safe, they are isolated and independent from one another.

In C#, on the other hand, you can do the same by adding a new class to a project. It isn’t at the same level of not even touching anything else, since it all compiles to a single binary, but the situation is roughly the same.

That is the Open Closed Principle when it applies to the code inside your application. What happens when you try to apply the same principle to your overall architecture?

I think that Terraform is a great example of doing just that. They have a plugin system that they built, which spawns a new process (so completely independent) and then connect to it via gRPC. Adding a new plugin to Terraform doesn’t involve modifying any code (you do have to update some configuration, but even that can be automated away). You can write everything using separate systems, runtime and versions quite easily.

If we push the idea a bit further, we’ll discover that Open Closed Principle at the architecture level is the Service Oriented Architecture.  Note that I explicitly don’t count Microservices in this role, because they are usually intermixed (yes, I know they aren’t supposed to, I’m talking about what is).

In those situations, adding a new feature to the system would involve adding a new service. For example, in a banking system, if you want to add a new feature to classify fraudulent transactions, how would you do it?

One way is to go to the transaction processing code and write something like:

public void processTransactions(HttpServletRequest request, HttpServletResponse response) { String src = request.getParameter("from"); String dst = request.getParameter("to"); int amountInCents = Integer.parseInt(request.getParameter("amount")); /// new code here TransactionProcessingFactory fact = new TransactionProcessingFactory(); // will throw if fraud fact.createProcessor().process(new Transaction(src, dst, amountInCents/1.0)); // new code here

/* ** Rest of the actual functionality **/ }

That, of course, would mean that you are going to have to modify existing code, that is not a good idea. Welcome to six months of meeting about when you can deploy your changes to the code.

On the other hand, applying the Open Closed Principle to the architecture, we won’t ever touch the actual system that process transactions. Instead, we’ll use a side channel. Transactions will be written to a queue and we’ll be able to add listeners to the queue. In such a way, we’ll have the ability to add additional processing seamlessly. Another fraud system will just have to listen to the stream of messages and react accordingly.

Note that there is a big difference here, however, unlike with modifying the code directly, we can no longer just throw an exception to stop the process. By the time that we process the message, the transaction has already been applied. That requires that we’ll build the system in such a way that there are ways to stop transactions after the fact (maybe by actually submitting them to the central bank after a certain amount of time, or releasing them to the system only after all the configured endpoints authorized it).

At the architecture level, we are intentionally building something that is initially more complex, because we have to take into account asynchronous operations and work that happens out of band, including work that we couldn’t expect. In the context of a bank, that means that we need to provide the mechanisms for future code to intervene. For example, we may not know what we’ll want the additional code to do, but we’ll have a way to do things like pause a transaction for manual review, add additional fees, raise alerts, etc.  Those are the capabilities of the system, and the additional behavior would be policy around building that.

There are other things that make this very attractive, you don’t have to run everything at the same time, you can independently upgrade different pieces and you have clear lines of demarcation between the different pieces of your system.


Recommend

  • 1
    • simonwillison.net 2 years ago
    • Cache

    The Baked Data architectural pattern

    The Baked Data architectural pattern I’ve been exploring an architectural pattern for publishing websites over the past few years that I call the “Baked Data” pattern. It provides many of the advantages of static site generators whi...

  • 8

    Open data standards design behind closed doors? Ana Brandusescu  (McGill University) Michael Canares (StepUp Consulting) Silvana Fumega (ILDA) In June 2020, we had the opportunity to joi...

  • 8
    • en.wikipedia.org 3 years ago
    • Cache

    Open–closed principle

    Open–closed principle From Wikipedia, the free encyclopedia Jump to navigation

  • 4
    • blog.cleancoder.com 3 years ago
    • Cache

    The Open Closed Principle

    The Open Closed Principle 12 May 2014 In 1988 Bertrand Meyer defined one of the most important principles of software engineering. The Open Closed Principle (OCP). In his book

  • 0
    • blog.cleancoder.com 3 years ago
    • Cache

    An Open and Closed Case

    An Open and Closed Case 08 March 2013 I awoke this morning to see a twitter conversation about the Open-Closed Principle. The tweeter was complaining that it didn’t make a lot of sense. He said things like:...

  • 9
    • blog.revillweb.com 3 years ago
    • Cache

    Open vs. Closed Shadow DOM

    Open vs. Closed Shadow DOMOut of the four specifications created for Web Components, the Shadow DOM is one of the most powerful. The Shadow DOM allows the component author to create an encapsulated sub-DOM tree for their c...

  • 3
    • reflectoring.io 3 years ago
    • Cache

    The Open-Closed Principle Explained

    Robert C. Martin, maybe better known to you as „Uncle Bob“, has defined a set of principles for software engineering and software architecture. Together, they are known as the SOLID Principles. One of them is the

  • 6

    What’s the difference between open-back and closed-back headphones? THE CLUE IS IN THE NAME (AND HEADER IMAGE) ...

  • 5
    • www.dotnetforall.com 2 years ago
    • Cache

    Open Closed Principle Implementation and Use Case

    May 31 2021Open Closed Principle Implementation and Use Case Hello friends, Open Closed principle is the second SOLID principle after single re...

  • 7

    Should We Follow The Open-Closed Principle?When I first heard...

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK