Strangler Design Pattern

Before I explain about Strangler let me tell you about Open Closed Principle ,  one of the principle in SOLID

What is Open Closed Principle?

Open Closed Principle says that the class should be Open for extensible but closed for Modification.Essentially what this means is that when you written a class and put the class in production or other clients rely on that class you are no longer allow to make changes to that class.Instead if you want to redefine the behavior that class it should be Open for extensible which means that anyone should be able to extend the class in order to redefine new behavior.The only exception that do not allow to make changes to classes that already being used is if it’s make bug in that class.Bug Fixing is 0k but otherwise you are not allow to make changes to that class once is being used by other class. The motivation of doing this is once other clients are beginning to rely on that class that wrote ,change the behavior of such class would impact all the clients that use this class.

Essentially Open Closed Principle says that the class should be Closed For modification and Open for extensible . Closed for modification means you can’t really update or delete the class, you can only create the class and obviously read the class .That sounds strange. This is not how you are used to think writing source code So you probably wonder what is the benefit of this? This sounds like a very constraining and a very difficult way of writing software. could this actually out to be beneficial?. It almost seems to assume that you would  have to write perfect code all the time and obviously that not possible because we are humans, we make mistake all the times.The way we approach this problem is using a pattern called Strangler Pattern.

What is Strangler Pattern?

In a software design the idea is that Instead of changing an existing system we add new behavior on your implementation around that old system, then we gradually move the clients from using the old system to using the new system and end up with only using new system and then finally we can turn off the old system.Originally the Strangler Pattern is a architectural design pattern and the pattern describes how to exchange one system with another gradually but you can also use this pattern inside an object oriented code base to describe how to you gradually replace old classes that turn out to be incomplete or have fault behavior with new classes that have the correct behavior.

The motivation of the Open Closed Principle is that if you have a class that you put in to production that has lot of clients that depend on it. it will impact if we change the class . If you change the behavior of the class , the other clients depend on it will affect this. So take what you like from the old class and create a new class and then before you publish that new class make the modification in that class.So that the new class has the behavior that you wanted to have. And then you can started to write new clients that use that class published.Then you can tell the clients that depend on the old class that they should move on using to using the new class.

There is one example in .Net framework X509Certificate, after Microsoft has published a version of .Net Framework with X509Certificate class they realize that its only wok with a specific format of X509Certificate. In that case they are ended up creating a new class called X509Certificate2.

Enjoy Programming