{"id":16480,"date":"2018-03-28T08:39:45","date_gmt":"2018-03-28T13:39:45","guid":{"rendered":"https:\/\/stackify.com\/?p=16480"},"modified":"2024-03-26T11:59:21","modified_gmt":"2024-03-26T11:59:21","slug":"solid-design-open-closed-principle","status":"publish","type":"post","link":"https:\/\/stackify.com\/solid-design-open-closed-principle\/","title":{"rendered":"SOLID Design Principles Explained: The Open\/Closed Principle with Code Examples"},"content":{"rendered":"<p>The Open\/Closed Principle is one of five design principles for object-oriented software development described by <a href=\"http:\/\/blog.cleancoder.com\" target=\"_blank\" rel=\"noopener noreferrer\">Robert C. Martin<\/a>. They are best known as the SOLID principles:<\/p>\n<ul>\n<li><a href=\"https:\/\/stackify.com\/solid-design-principles\/\" target=\"_blank\" rel=\"noopener noreferrer\"><strong>S<\/strong>ingle Responsibility Principle<\/a><\/li>\n<li><a href=\"https:\/\/stackify.com\/solid-design-liskov-substitution-principle\/\"><strong>O<\/strong>pen\/Closed Principle<\/a><\/li>\n<li><a href=\"https:\/\/stackify.com\/solid-design-liskov-substitution-principle\/\"><strong>L<\/strong>iskov Substitution Principle<\/a><\/li>\n<li><a href=\"https:\/\/stackify.com\/interface-segregation-principle\/\"><strong>I<\/strong>nterface Segregation Principle<\/a><\/li>\n<li><a href=\"https:\/\/stackify.com\/dependency-inversion-principle\/\"><strong>D<\/strong>ependency Inversion<\/a><\/li>\n<\/ul>\n<p>All 5 of these design principles are broadly\u00a0used, and all experienced software developers should be familiar with them. But don&#8217;t worry, if you haven&#8217;t heard about them yet. I had been working as a software developer for a few years before I learned about the SOLID principles and quickly recognized that they described the rules and principles my coworkers had taught me about writing good code. So, even if you don&#8217;t know them by name, you might be already using them.<\/p>\n<p>But that doesn&#8217;t mean that we shouldn&#8217;t talk and learn about the SOLID principles. In this article, I will focus on the Open\/Closed Principle, and I will explain the other principles in future articles.<\/p>\n<h2>Definition of the Open\/Closed Principle<\/h2>\n<p>Robert C. Martin considered this principle as the \u201cthe most important principle of object-oriented design\u201d. But he wasn&#8217;t the first one who defined it.\u00a0Bertrand Meyer wrote about it in 1988 in his book <a href=\"https:\/\/en.wikipedia.org\/wiki\/Object-Oriented_Software_Construction\" target=\"_blank\" rel=\"noopener noreferrer\">Object-Oriented Software Construction<\/a>. He explained the Open\/Closed Principle as:<\/p>\n<blockquote><p>&#8220;Software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification.&#8221;<\/p><\/blockquote>\n<p>The general idea of this principle is great. It tells you to write your code so that you will be able to add new functionality without changing the existing code. That prevents situations in which a change to one of your classes also requires you to adapt all depending classes. Unfortunately, Bertrand Mayer proposes to use <a href=\"https:\/\/stackify.com\/oop-concept-inheritance\/\" target=\"_blank\" rel=\"noopener noreferrer\">inheritance<\/a> to achieve this goal:<\/p>\n<blockquote><p>&#8220;A class is closed, since it may be compiled, stored in a library, baselined, and used by client classes. But it is also open, since any new class may use it as parent, adding new features. When a descendant class is defined, there is no need to change the original or to disturb its clients.&#8221;<\/p><\/blockquote>\n<p>But as we&#8217;ve learned over the years and as other authors explained in great details, e.g., Robert C. Martin in his articles about the SOLID principles or Joshua Bloch in his book Effective Java, inheritance introduces tight coupling if the subclasses depend on implementation\u00a0details of their parent class.<\/p>\n<p>That&#8217;s why Robert C. Martin and others redefined the Open\/Closed Principle to the\u00a0<a href=\"https:\/\/stackify.com\/oop-concept-polymorphism\/\" target=\"_blank\" rel=\"noopener noreferrer\">Polymorphic<\/a> Open\/Closed Principle. It uses interfaces instead of superclasses to\u00a0allow different implementations which you can easily substitute without changing the code that uses them. The interfaces are closed for modifications, and you can provide new implementations to extend the functionality of your software.<\/p>\n<p>The main benefit of this approach is that an interface introduces an additional level of abstraction which enables loose coupling. The implementations\u00a0of an interface are independent of each other and don&#8217;t need to share any code. If you consider it beneficial that two implementations of an interface share some code, you can either use <a href=\"https:\/\/stackify.com\/oop-concept-inheritance\/\" target=\"_blank\" rel=\"noopener noreferrer\">inheritance<\/a> or <a href=\"https:\/\/stackify.com\/oop-concepts-composition\/\" target=\"_blank\" rel=\"noopener noreferrer\">composition<\/a>.<\/p>\n<p>Let&#8217;s take a look at an example that uses the Open\/Closed Principle.<\/p>\n<h2>Brewing coffee with the Open\/Closed Principle<\/h2>\n<p>You can buy lots of different coffee machines. There are relatively basic ones that just brew filter coffee, and others that include grinders to brew different kinds of coffee, e.g., espresso and filter coffee. All of them serve the same purpose: They brew delicious coffee which wakes us up in the morning.<\/p>\n<p>The only problem is that you need to get out of bed to switch on the coffee machine. So, why not ignore all the challenges of the physical world, e.g., how to put water and ground coffee into the machine or how to put a mug under it without getting out of bed, and implement a simple program\u00a0that serves you a freshly brewed coffee?<\/p>\n<p>To show you the benefits of the Open\/Closed Principle, I wrote a simple application that controls a basic coffee machine to brew you a delicious filter coffee in the morning.<\/p>\n<h3>The <em>BasicCoffeeMachine<\/em> class<\/h3>\n<p>The implementation of the <em>BasicCoffeeMachine<\/em> class is relatively simple. It just has a constructor, a public method to add ground coffee, and a method that brews a filter coffee.<\/p>\n<pre class=\"prettyprint\">import java.util.HashMap;\nimport java.util.Map;\n\npublic class BasicCoffeeMachine {\n\n    private Map&lt;CoffeeSelection, Configuration&gt; configMap;\n    private Map&lt;CoffeeSelection, GroundCoffee&gt;; groundCoffee;\n    private BrewingUnit brewingUnit;\n\n    public BasicCoffeeMachine(Map&lt;CoffeeSelection, GroundCoffee&gt; coffee) {\n    this.groundCoffee = coffee;\n    this.brewingUnit = new BrewingUnit();\n\n    this.configMap = new HashMap&lt;&gt;();\n        this.configMap.put(CoffeeSelection.FILTER_COFFEE, new Configuration(30, 480));\n    }\n\n    public Coffee brewCoffee(CoffeeSelection selection) {\n    Configuration config = configMap.get(CoffeeSelection.FILTER_COFFEE);\n\n    \/\/ get the coffee\n    GroundCoffee groundCoffee = this.groundCoffee.get(CoffeeSelection.FILTER_COFFEE);\n\n    \/\/ brew a filter coffee\n    return this.brewingUnit.brew(CoffeeSelection.FILTER_COFFEE, groundCoffee, config.getQuantityWater());\n    }\n\n    public void addGroundCoffee(CoffeeSelection sel, GroundCoffee newCoffee) throws CoffeeException {\n    GroundCoffee existingCoffee = this.groundCoffee.get(sel);\n    if (existingCoffee != null) {\n        if (existingCoffee.getName().equals(newCoffee.getName())) {\n        existingCoffee.setQuantity(existingCoffee.getQuantity() + newCoffee.getQuantity());\n        } else {\n        throw new CoffeeException(\"Only one kind of coffee supported for each CoffeeSelection.\");\n        }\n    } else {\n        this.groundCoffee.put(sel, newCoffee);\n    }\n    }\n}\n<\/pre>\n<p>You can easily control such a simple coffee machine via an app, right? So, let&#8217;s do that.<\/p>\n<h3>The <em>BasicCoffeeApp<\/em> class<\/h3>\n<p>The <em>main<\/em> method of the <em>BasicCoffeeApp<\/em> prepares a <em>Map<\/em> with ground coffee, instantiates a <em>BasicCoffeeMachine<\/em> object, and calls the <em>prepareCoffee<\/em> method to brew the coffee.<\/p>\n<pre class=\"prettyprint\">public class BasicCoffeeApp {\n\n    private BasicCoffeeMachine coffeeMachine;\n\n    public BasicCoffeeApp(BasicCoffeeMachine coffeeMachine) {\n    this.coffeeMachine = coffeeMachine;\n    }\n\n    public Coffee prepareCoffee(CoffeeSelection selection) throws CoffeeException {\n    Coffee coffee = this.coffeeMachine.brewCoffee(selection);\n    System.out.println(\"Coffee is ready!\");\n    return coffee;\n    }\n\n    public static void main(String[] args) {\n    \/\/ create a Map of available coffee beans\n    Map&lt;CoffeeSelection, GroundCoffee&gt; beans = new HashMap&lt;CoffeeSelection, GroundCoffee&gt;();\n    beans.put(CoffeeSelection.FILTER_COFFEE, new GroundCoffee(\n        \"My favorite filter coffee bean\", 1000));\n\n    \/\/ get a new CoffeeMachine object\n    BasicCoffeeMachine machine = new BasicCoffeeMachine(beans);\n\n    \/\/ Instantiate CoffeeApp\n    BasicCoffeeApp app = new BasicCoffeeApp(machine);\n\n    \/\/ brew a fresh coffee\n    try {\n        app.prepareCoffee(CoffeeSelection.FILTER_COFFEE);\n    } catch (CoffeeException e) {\n        e.printStackTrace();\n    }\n    } \/\/ end main\n} \/\/ end CoffeeApp\n<\/pre>\n<p>That&#8217;s it. From now on, you can stay in bed until you smell the fresh coffee prepared by your <em>BasicCoffeeApp<\/em>.<\/p>\n<h3>Applying the Open\/Closed principle<\/h3>\n<p>But what happens when you replace your <em>BasicCoffeeMachine<\/em>?\u00a0You might get a better one with an integrated grinder, which can brew more than just filter coffee. Unfortunately, the <em>CoffeeApp<\/em> doesn&#8217;t support this kind of coffee machine.<\/p>\n<p>It would be great if your app could control both types of coffee machines. But that will require a few code changes. And as you&#8217;re already on it, why not change it so that you will not need to adapt it to future coffee machines.<\/p>\n<h4>Extracting the <em>CoffeeMachine<\/em> interface<\/h4>\n<p>Following the Open\/Closed Principle, you need to extract an interface that enables you to control the coffee machine. That&#8217;s often the critical part of the refactoring. You need to include the methods that are mandatory for controlling the coffee machine, \u00a0but none of the optional methods which would limit the flexibility of the implementations.<\/p>\n<p>In this example, that&#8217;s only the <em>brewCoffee<\/em> method. So, the <em>CoffeeMachine<\/em> interface specifies only one method, which needs to be implemented by all classes that implement it.<\/p>\n<pre class=\"prettyprint\">public interface CoffeeMachine {\n\n    Coffee brewCoffee(CoffeeSelection selection) throws CoffeeException;\n}<\/pre>\n<h4>Adapting the <em>BasicCoffeeMachine<\/em> class<\/h4>\n<p>In the next step, you need to adapt the <em>BasicCoffeeMachine<\/em> class. It already implements the <em>brewCoffee<\/em> method and provides all the functionality it needs. So, you just need to declare that the <em>BasicCoffeeMachine<\/em> class implements the <em>CoffeeMachine<\/em> interface.<\/p>\n<pre class=\"prettyprint\">public class BasicCoffeeMachine implements CoffeeMachine { ... }\n<\/pre>\n<h4>Add more implementations<\/h4>\n<p>You can now add new implementations of the <em>CoffeeMachine<\/em> interface.<\/p>\n<p>The implementation of the <em>PremiumCoffeeMachine<\/em> class is\u00a0more complex than the <em>BasicCoffeeMachine<\/em> class. Its <em>brewCoffee<\/em> method, which is defined by the <em>CoffeeMachine<\/em> interface, supports two different <em>CoffeeSelection<\/em>s. Based on the provided <em>CoffeeSelection<\/em>, the method calls a separate, private method that brews the selected coffee. As you can see in the implementation of these methods, the class also uses <a href=\"https:\/\/stackify.com\/oop-concepts-composition\/\" target=\"_blank\" rel=\"noopener noreferrer\">composition<\/a> to reference a <em>Grinder,<\/em> which grinds the coffee beans before brewing the coffee.<\/p>\n<pre class=\"prettyprint\">import java.util.HashMap;\nimport java.util.Map;\n\npublic class PremiumCoffeeMachine implements CoffeeMachine {\n\n    private Map&lt;CoffeeSelection, Configuration&gt; configMap;\n    private Map&lt;CoffeeSelection, CoffeeBean&gt; beans;\n    private Grinder grinder;\n    private BrewingUnit brewingUnit;\n\n    public PremiumCoffeeMachine(Map&lt;CoffeeSelection, CoffeeBean&gt; beans) {\n    this.beans = beans;\n    this.grinder = new Grinder();\n    this.brewingUnit = new BrewingUnit();\n\n    this.configMap = new HashMap&lt;&gt;();\n    this.configMap.put(CoffeeSelection.FILTER_COFFEE, new Configuration(30, 480));\n    this.configMap.put(CoffeeSelection.ESPRESSO, new Configuration(8, 28));\n    }\n\n    @Override\n    public Coffee brewCoffee(CoffeeSelection selection) throws CoffeeException {\n    switch(selection) {\n    case ESPRESSO:\n        return brewEspresso();\n    case FILTER_COFFEE:\n        return brewFilterCoffee();\n    default:\n        throw new CoffeeException(\"CoffeeSelection [\" + selection + \"] not supported!\");\n    }\n    }\n\n    private Coffee brewEspresso() {\n    Configuration config = configMap.get(CoffeeSelection.ESPRESSO);\n\n    \/\/ grind the coffee beans\n    GroundCoffee groundCoffee = this.grinder.grind(\n        this.beans.get(CoffeeSelection.ESPRESSO),\n            config.getQuantityCoffee());\n\n    \/\/ brew an espresso\n    return this.brewingUnit.brew(CoffeeSelection.ESPRESSO, groundCoffee,\n        config.getQuantityWater());\n    }\n\n    private Coffee brewFilterCoffee() {\n    Configuration config = configMap.get(CoffeeSelection.FILTER_COFFEE);\n\n    \/\/ grind the coffee beans\n    GroundCoffee groundCoffee = this.grinder.grind(\n        this.beans.get(CoffeeSelection.FILTER_COFFEE),\n            config.getQuantityCoffee());\n\n    \/\/ brew a filter coffee\n    return this.brewingUnit.brew(CoffeeSelection.FILTER_COFFEE, groundCoffee,\n        config.getQuantityWater());\n    }\n\n    public void addCoffeeBeans(CoffeeSelection sel, CoffeeBean newBeans) throws CoffeeException {\n    CoffeeBean existingBeans = this.beans.get(sel);\n    if (existingBeans != null) {\n        if (existingBeans.getName().equals(newBeans.getName())) {\n            existingBeans.setQuantity(existingBeans.getQuantity() + newBeans.getQuantity());\n        } else {\n        throw new CoffeeException(\"Only one kind of coffee supported for each CoffeeSelection.\");\n        }\n    } else {\n        this.beans.put(sel, newBeans);\n    }\n    }\n}\n<\/pre>\n<p>You&#8217;re done with most of the refactoring work. You applied the Open\/Closed Principle by introducing the <em>CoffeeMachine<\/em> interface and providing two independent implementations of it.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-16484 size-full\" title=\"SOLID Design Principles Explained \u2013 The Open\/Closed Principle \" src=\"https:\/\/stackify.com\/wp-content\/uploads\/2018\/03\/Refactored-classes.png\" alt=\"SOLID Design Principles Explained \u2013 The Open\/Closed Principle \" width=\"913\" height=\"455\" \/><\/p>\n<p>The only thing that&#8217;s left is the app to use different implementations of that interface.<\/p>\n<h3>Adapting the CoffeeApp<\/h3>\n<p>The <em>CoffeeApp<\/em> class consists of 2 parts:<\/p>\n<ol>\n<li>the <em>CoffeeApp<\/em> class and<\/li>\n<li>the <em>main<\/em> method<\/li>\n<\/ol>\n<p>You need to instantiate a specific <em>CoffeeMachine<\/em> implementation in the <em>main<\/em> method. So, you will always need to adopt this method, if you replace your current coffee machine. But as long as the <em>CoffeeApp<\/em> class uses the <em>CoffeeMachine<\/em> interface, you will not need to adapt it.<\/p>\n<pre class=\"prettyprint\">import java.util.HashMap;\nimport java.util.Map;\n\npublic class CoffeeApp {\n\n    private CoffeeMachine coffeeMachine;\n\n    public CoffeeApp(CoffeeMachine coffeeMachine) {\n    this.coffeeMachine = coffeeMachine;\n    }\n\n    public Coffee prepareCoffee(CoffeeSelection selection) throws CoffeeException {\n    Coffee coffee = this.coffeeMachine.brewCoffee(selection);\n    System.out.println(\"Coffee is ready!\");\n    return coffee;\n    }\n\n    public static void main(String[] args) {\n    \/\/ create a Map of available coffee beans\n    Map&lt;CoffeeSelection, CoffeeBean&gt;; beans = new HashMap&lt;CoffeeSelection, CoffeeBean&gt;();\n    beans.put(CoffeeSelection.ESPRESSO, new CoffeeBean(\n        \"My favorite espresso bean\", 1000));\n    beans.put(CoffeeSelection.FILTER_COFFEE, new CoffeeBean(\n        \"My favorite filter coffee bean\", 1000));\n\n    \/\/ get a new CoffeeMachine object\n    PremiumCoffeeMachine machine = new PremiumCoffeeMachine(beans);\n\n    \/\/ Instantiate CoffeeApp\n    CoffeeApp app = new CoffeeApp(machine);\n\n    \/\/ brew a fresh coffee\n    try {\n        app.prepareCoffee(CoffeeSelection.ESPRESSO);\n    } catch (CoffeeException e) {\n        e.printStackTrace();\n    }\n    } \/\/ end main\n} \/\/ end CoffeeApp\n<\/pre>\n<h2>Summary<\/h2>\n<p>After taking a closer look at the <a href=\"https:\/\/stackify.com\/solid-design-principles\/\" target=\"_blank\" rel=\"noopener noreferrer\">Single Responsibility Principle<\/a> in the previous post of this series, we now discussed the Open\/Closed Principle. It is one of the five SOLID design principle described by Robert C. Martin. It promotes the use of interfaces to enable you to adapt the functionality of your application without changing the existing code.<\/p>\n<p>We used this principle in the example application to control different kinds of coffee machines via our <em>CoffeeApp<\/em>. As long as a coffee machine implements the <em>CoffeeMachine<\/em> 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 <a href=\"https:\/\/docs.oracle.com\/javase\/tutorial\/ext\/basics\/spi.html\" target=\"_blank\" rel=\"noopener noreferrer\">service loader API<\/a> to replace the instantiation of a specific class.<\/p>\n<p>With APM, server health metrics, and error log integration, improve your application performance with Stackify Retrace.\u00a0 <a href=\"https:\/\/s1.stackify.com\/account\/createclient?_ga=2.57834090.1973545731.1588002198-1971815645.1570122931&amp;_gac=1.238281396.1584390051.EAIaIQobChMIvenD6Oif6AIVnP7jBx3XjACyEAAYBCAAEgJmVPD_BwE\">Try your free two week trial today<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The Open\/Closed Principle is one of five design principles for object-oriented software development described by Robert C. Martin. They are best known as the SOLID principles: Single Responsibility Principle Open\/Closed Principle Liskov Substitution Principle Interface Segregation Principle Dependency Inversion All 5 of these design principles are broadly\u00a0used, and all experienced software developers should be familiar [&hellip;]<\/p>\n","protected":false},"author":18,"featured_media":37794,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[7],"tags":[40],"class_list":["post-16480","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-developers","tag-java"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v25.6 (Yoast SEO v25.6) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>The Open\/Closed Principle with Code Examples - Stackify<\/title>\n<meta name=\"description\" content=\"All 5 SOLID design principles are broadly\u00a0used, If you don&#039;t know them by name, you will quickly recognize that they describe the rules.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/stackify.com\/solid-design-open-closed-principle\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"The Open\/Closed Principle with Code Examples - Stackify\" \/>\n<meta property=\"og:description\" content=\"All 5 SOLID design principles are broadly\u00a0used, If you don&#039;t know them by name, you will quickly recognize that they describe the rules.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/stackify.com\/solid-design-open-closed-principle\/\" \/>\n<meta property=\"og:site_name\" content=\"Stackify\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/Stackify\/\" \/>\n<meta property=\"article:published_time\" content=\"2018-03-28T13:39:45+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-03-26T11:59:21+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/stackify.com\/wp-content\/uploads\/2018\/03\/SOLID-Principles-Open-Closed-principle-881x441-1.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"881\" \/>\n\t<meta property=\"og:image:height\" content=\"441\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Thorben Janssen\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@stackify\" \/>\n<meta name=\"twitter:site\" content=\"@stackify\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Thorben Janssen\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/stackify.com\/solid-design-open-closed-principle\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/stackify.com\/solid-design-open-closed-principle\/\"},\"author\":{\"name\":\"Thorben Janssen\",\"@id\":\"https:\/\/stackify.com\/#\/schema\/person\/6ee3991649f0e3430ec29b20105946ff\"},\"headline\":\"SOLID Design Principles Explained: The Open\/Closed Principle with Code Examples\",\"datePublished\":\"2018-03-28T13:39:45+00:00\",\"dateModified\":\"2024-03-26T11:59:21+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/stackify.com\/solid-design-open-closed-principle\/\"},\"wordCount\":1363,\"publisher\":{\"@id\":\"https:\/\/stackify.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/stackify.com\/solid-design-open-closed-principle\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/stackify.com\/wp-content\/uploads\/2018\/03\/SOLID-Principles-Open-Closed-principle-881x441-1.jpg\",\"keywords\":[\"Java\"],\"articleSection\":[\"Developer Tips, Tricks &amp; Resources\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/stackify.com\/solid-design-open-closed-principle\/\",\"url\":\"https:\/\/stackify.com\/solid-design-open-closed-principle\/\",\"name\":\"The Open\/Closed Principle with Code Examples - Stackify\",\"isPartOf\":{\"@id\":\"https:\/\/stackify.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/stackify.com\/solid-design-open-closed-principle\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/stackify.com\/solid-design-open-closed-principle\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/stackify.com\/wp-content\/uploads\/2018\/03\/SOLID-Principles-Open-Closed-principle-881x441-1.jpg\",\"datePublished\":\"2018-03-28T13:39:45+00:00\",\"dateModified\":\"2024-03-26T11:59:21+00:00\",\"description\":\"All 5 SOLID design principles are broadly\u00a0used, If you don't know them by name, you will quickly recognize that they describe the rules.\",\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/stackify.com\/solid-design-open-closed-principle\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/stackify.com\/solid-design-open-closed-principle\/#primaryimage\",\"url\":\"https:\/\/stackify.com\/wp-content\/uploads\/2018\/03\/SOLID-Principles-Open-Closed-principle-881x441-1.jpg\",\"contentUrl\":\"https:\/\/stackify.com\/wp-content\/uploads\/2018\/03\/SOLID-Principles-Open-Closed-principle-881x441-1.jpg\",\"width\":881,\"height\":441},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/stackify.com\/#website\",\"url\":\"https:\/\/stackify.com\/\",\"name\":\"Stackify\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/stackify.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/stackify.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/stackify.com\/#organization\",\"name\":\"Stackify\",\"url\":\"https:\/\/stackify.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/stackify.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/stackify.com\/wp-content\/uploads\/2024\/05\/logo-1.png\",\"contentUrl\":\"https:\/\/stackify.com\/wp-content\/uploads\/2024\/05\/logo-1.png\",\"width\":1377,\"height\":430,\"caption\":\"Stackify\"},\"image\":{\"@id\":\"https:\/\/stackify.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/Stackify\/\",\"https:\/\/x.com\/stackify\",\"https:\/\/www.instagram.com\/stackify\/\",\"https:\/\/www.linkedin.com\/company\/2596184\",\"https:\/\/www.youtube.com\/stackify\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/stackify.com\/#\/schema\/person\/6ee3991649f0e3430ec29b20105946ff\",\"name\":\"Thorben Janssen\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/stackify.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/00ede092b3c1e3959f249e4b4319c35a?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/00ede092b3c1e3959f249e4b4319c35a?s=96&d=mm&r=g\",\"caption\":\"Thorben Janssen\"}}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"The Open\/Closed Principle with Code Examples - Stackify","description":"All 5 SOLID design principles are broadly\u00a0used, If you don't know them by name, you will quickly recognize that they describe the rules.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/stackify.com\/solid-design-open-closed-principle\/","og_locale":"en_US","og_type":"article","og_title":"The Open\/Closed Principle with Code Examples - Stackify","og_description":"All 5 SOLID design principles are broadly\u00a0used, If you don't know them by name, you will quickly recognize that they describe the rules.","og_url":"https:\/\/stackify.com\/solid-design-open-closed-principle\/","og_site_name":"Stackify","article_publisher":"https:\/\/www.facebook.com\/Stackify\/","article_published_time":"2018-03-28T13:39:45+00:00","article_modified_time":"2024-03-26T11:59:21+00:00","og_image":[{"width":881,"height":441,"url":"https:\/\/stackify.com\/wp-content\/uploads\/2018\/03\/SOLID-Principles-Open-Closed-principle-881x441-1.jpg","type":"image\/jpeg"}],"author":"Thorben Janssen","twitter_card":"summary_large_image","twitter_creator":"@stackify","twitter_site":"@stackify","twitter_misc":{"Written by":"Thorben Janssen","Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/stackify.com\/solid-design-open-closed-principle\/#article","isPartOf":{"@id":"https:\/\/stackify.com\/solid-design-open-closed-principle\/"},"author":{"name":"Thorben Janssen","@id":"https:\/\/stackify.com\/#\/schema\/person\/6ee3991649f0e3430ec29b20105946ff"},"headline":"SOLID Design Principles Explained: The Open\/Closed Principle with Code Examples","datePublished":"2018-03-28T13:39:45+00:00","dateModified":"2024-03-26T11:59:21+00:00","mainEntityOfPage":{"@id":"https:\/\/stackify.com\/solid-design-open-closed-principle\/"},"wordCount":1363,"publisher":{"@id":"https:\/\/stackify.com\/#organization"},"image":{"@id":"https:\/\/stackify.com\/solid-design-open-closed-principle\/#primaryimage"},"thumbnailUrl":"https:\/\/stackify.com\/wp-content\/uploads\/2018\/03\/SOLID-Principles-Open-Closed-principle-881x441-1.jpg","keywords":["Java"],"articleSection":["Developer Tips, Tricks &amp; Resources"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/stackify.com\/solid-design-open-closed-principle\/","url":"https:\/\/stackify.com\/solid-design-open-closed-principle\/","name":"The Open\/Closed Principle with Code Examples - Stackify","isPartOf":{"@id":"https:\/\/stackify.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/stackify.com\/solid-design-open-closed-principle\/#primaryimage"},"image":{"@id":"https:\/\/stackify.com\/solid-design-open-closed-principle\/#primaryimage"},"thumbnailUrl":"https:\/\/stackify.com\/wp-content\/uploads\/2018\/03\/SOLID-Principles-Open-Closed-principle-881x441-1.jpg","datePublished":"2018-03-28T13:39:45+00:00","dateModified":"2024-03-26T11:59:21+00:00","description":"All 5 SOLID design principles are broadly\u00a0used, If you don't know them by name, you will quickly recognize that they describe the rules.","inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/stackify.com\/solid-design-open-closed-principle\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/stackify.com\/solid-design-open-closed-principle\/#primaryimage","url":"https:\/\/stackify.com\/wp-content\/uploads\/2018\/03\/SOLID-Principles-Open-Closed-principle-881x441-1.jpg","contentUrl":"https:\/\/stackify.com\/wp-content\/uploads\/2018\/03\/SOLID-Principles-Open-Closed-principle-881x441-1.jpg","width":881,"height":441},{"@type":"WebSite","@id":"https:\/\/stackify.com\/#website","url":"https:\/\/stackify.com\/","name":"Stackify","description":"","publisher":{"@id":"https:\/\/stackify.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/stackify.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/stackify.com\/#organization","name":"Stackify","url":"https:\/\/stackify.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/stackify.com\/#\/schema\/logo\/image\/","url":"https:\/\/stackify.com\/wp-content\/uploads\/2024\/05\/logo-1.png","contentUrl":"https:\/\/stackify.com\/wp-content\/uploads\/2024\/05\/logo-1.png","width":1377,"height":430,"caption":"Stackify"},"image":{"@id":"https:\/\/stackify.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/Stackify\/","https:\/\/x.com\/stackify","https:\/\/www.instagram.com\/stackify\/","https:\/\/www.linkedin.com\/company\/2596184","https:\/\/www.youtube.com\/stackify"]},{"@type":"Person","@id":"https:\/\/stackify.com\/#\/schema\/person\/6ee3991649f0e3430ec29b20105946ff","name":"Thorben Janssen","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/stackify.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/00ede092b3c1e3959f249e4b4319c35a?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/00ede092b3c1e3959f249e4b4319c35a?s=96&d=mm&r=g","caption":"Thorben Janssen"}}]}},"_links":{"self":[{"href":"https:\/\/stackify.com\/wp-json\/wp\/v2\/posts\/16480"}],"collection":[{"href":"https:\/\/stackify.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/stackify.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/stackify.com\/wp-json\/wp\/v2\/users\/18"}],"replies":[{"embeddable":true,"href":"https:\/\/stackify.com\/wp-json\/wp\/v2\/comments?post=16480"}],"version-history":[{"count":0,"href":"https:\/\/stackify.com\/wp-json\/wp\/v2\/posts\/16480\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/stackify.com\/wp-json\/wp\/v2\/media\/37794"}],"wp:attachment":[{"href":"https:\/\/stackify.com\/wp-json\/wp\/v2\/media?parent=16480"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/stackify.com\/wp-json\/wp\/v2\/categories?post=16480"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/stackify.com\/wp-json\/wp\/v2\/tags?post=16480"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}