{"id":1787,"date":"2021-04-10T10:10:46","date_gmt":"2021-04-10T10:10:46","guid":{"rendered":"https:\/\/gauravw.com\/blog\/?p=1787"},"modified":"2021-04-10T10:10:51","modified_gmt":"2021-04-10T10:10:51","slug":"design-principles-design-patterns","status":"publish","type":"post","link":"https:\/\/gauravw.com\/blog\/2021\/04\/design-principles-design-patterns\/","title":{"rendered":"Design Principles &#038; Design patterns"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>SOLID <\/strong>Design Principles<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li><strong>Single Responsibility Pattern <\/strong>&#8211; class has a single responsibility like Class Sort won&#8217;t have functions to add or delete from the list. ex Microservices<br><\/li><li><strong>Open &#8211; Closed Principle <\/strong>&#8211; Open for extension and closed for modification. It tells you to write your code so that you will be able to add new functionality without changing the existing code.<br><\/li><\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">\/\/Bad &#8211; Are of shape is inside the caller class and not of shape. So when a new shape is introduced, it needs to be changed.&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">class GraphicEditor {<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">public void areaOfShape(Shape s) {<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">if (s.m_type==1)<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">areaOfRectangle(s);<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">else if (s.m_type==2)<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">areaOfCircle(s);<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">}<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">public void drawCircle(Circle r) {&#8230;.}<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">public void drawRectangle(Rectangle r) {&#8230;.}<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">}<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">class Shape {<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">int m_type;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">}<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">class Rectangle extends Shape {<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Rectangle() {<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">super.m_type=1;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">}<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">}<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">class Circle extends Shape {<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Circle() {<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">super.m_type=2;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">}<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">}<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Good &#8211; Area method is in interface itself so, Shapes define it on their own<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\/ Open-Close Principle &#8211; Good example<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">class GraphicEditor {<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">public void areaOfShape(Shape s) {<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">s.area();<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">}<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">}<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">class Shape {<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">abstract void area();<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">}<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">class Rectangle extends Shape&nbsp; {<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">public void draw() {<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\/\/ draw the rectangle<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">}<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">}<\/p>\n\n\n\n<ol class=\"wp-block-list\" start=\"3\"><li><strong>Liskov Substitution Principle<\/strong><\/li><\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">The principle defines that objects of a superclass shall be replaceable\/<strong>substituted<\/strong> with objects of its subclasses without breaking the application. Achieve this by using interfaces. It uses ISP and Open Closed Principle.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><br>Example<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Rectangle and Square example. Though every square is a rectangle but it will not pass the \u201csubstitutability\u201d as it behaves differently. So if you increase the width of the rectangle by 1 then it will impact its area by a factor of 1. But if you do that with squares, it will also change its length.<br><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Similarly, Bird interface has fly() and it can be inherited by Duck and Penguin. But Penguins can&#8217;t fly. So, it&#8217;s better to have another segregation of interfaces as the FlyingBird with fly() method.<\/p>\n\n\n\n<ol class=\"wp-block-list\" start=\"4\"><li><strong>ISP &#8211; Interface Segregation Principle<\/strong>&nbsp;<\/li><\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">It says the interface should contain the most minimal set of functions needed by an implementing class.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A class that might implement an interface that contains many functions may leave them blank or throw exceptions in them.&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Example &nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Bad &#8211; Printer Interface with functions for print, fax and scan<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Good &#8211; Printer print() , Fax Interface only has fax() and Scanner only scan().<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Creational<\/p>\n\n\n\n<ol class=\"wp-block-list\" start=\"5\"><li><strong>Dependency Inversion Principle (DIP) <\/strong>&#8211; focuses on the approach where the higher classes are not dependent on the lower classes instead depend upon the abstraction of the lower classes.<\/li><\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">The basis for all other patterns. We should depend on interfaces and not directly on classes<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Example &#8211; Manager could manage Developer, Designer, Testers<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Good &#8211; Manager manages Employee &#8211; Developer, Designer, Testers(implement Employee)<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Gang of Four<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Creational Patterns<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">1. Object Pool&nbsp; &#8211; for large no of objects &#8211; sourcemaking.com<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">a. class which inherits an interface having three functions<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">create()<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">expire()<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">validate()<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Objects r supposed to expire if not used after a certain time.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Maintain two hashmaps locked and unlocked of objects<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">so a new function checkOut() {<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; loop over unlocked list {<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if ( now &#8211; unlocked.get(t) ) &gt; expirationTime ) \/\/condition to test if its invalid {<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; unlock.remove(t) &#8230;expire object<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } else {<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if( valid(t) ) {<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; unlocked.remove(t)<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; locked.put(t)<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return t;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } else<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; unlock.remove(t) &#8230;expire<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; t = create();<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;locked.put(t, now);<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;return (t);<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">}<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This enables reuse of created objects.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">2. builder pattern &#8211; Separate the construction of a complex object from its representation so that the same construction process can create different representations.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;An application needs to create the elements of a complex aggregate. The specification for the aggregate exists on secondary storage and one of many representations needs to be built in primary storage.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">public class Computer {<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\/\/required parameters<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">private String HDD;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">private String RAM;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\/\/optional parameters<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">private boolean isGraphicsCardEnabled;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">private boolean isBluetoothEnabled;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">public String getHDD() {<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">return HDD;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">}<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">public String getRAM() {<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">return RAM;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">}<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">public boolean isGraphicsCardEnabled() {<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">return isGraphicsCardEnabled;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">}<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">public boolean isBluetoothEnabled() {<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">return isBluetoothEnabled;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">}<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">private Computer(ComputerBuilder builder) {<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">this.HDD=builder.HDD;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">this.RAM=builder.RAM;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">this.isGraphicsCardEnabled=builder.isGraphicsCardEnabled;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">this.isBluetoothEnabled=builder.isBluetoothEnabled;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">}<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\/\/Builder Class<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">public static class ComputerBuilder{<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\/\/ required parameters<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">private String HDD;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">private String RAM;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\/\/ optional parameters<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">private boolean isGraphicsCardEnabled;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">private boolean isBluetoothEnabled;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">public ComputerBuilder(String hdd, String ram){<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">this.HDD=hdd;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">this.RAM=ram;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">}<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">public ComputerBuilder setGraphicsCardEnabled(boolean isGraphicsCardEnabled) {<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">this.isGraphicsCardEnabled = isGraphicsCardEnabled;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">return this;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">}<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">public ComputerBuilder setBluetoothEnabled(boolean isBluetoothEnabled) {<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">this.isBluetoothEnabled = isBluetoothEnabled;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">return this;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">}<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">public Computer build(){<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">return new Computer(this);<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">}<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">}<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">}<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Computer comp = new Computer.ComputerBuilder(<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&#8220;500 GB&#8221;, &#8220;2 GB&#8221;).setBluetoothEnabled(true)<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">.setGraphicsCardEnabled(true).build();<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-wp-embed is-provider-journaldev wp-block-embed-journaldev\"><div class=\"wp-block-embed__wrapper\">\nhttps:\/\/www.journaldev.com\/1425\/builder-design-pattern-in-java\n<\/div><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">3. abstract factory \u2013 Provide an interface for creating families of related or dependent objects without specifying their concrete classes.<br><br>DocumentBuilder in java<br>Frameworks use abstract Factory<\/p>\n\n\n\n<figure class=\"wp-block-embed\"><div class=\"wp-block-embed__wrapper\">\nhttps:\/\/www.tutorialspoint.com\/design_pattern\/abstract_factory_pattern.htm\n<\/div><\/figure>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh3.googleusercontent.com\/5tI-978FnMAuscZEFonU8gnqyNmLuIDCAVLIG3Y8VHdD152FfLLXGEDh8X5U_-BKPLVdN0xTmxOxg-4YIH23hEDDCuvCuKfhcjG-2MMDd0-S129omHFoGwfUO6VW5UoM5CFmx2pO\" alt=\"\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">4. Factory Method<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses.<\/li><\/ul>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh5.googleusercontent.com\/MyP1wV7G8_0S7qE9wbh-JJO5Wpe6s9N1M6d5WLgJFtCYoGX101BZrb0UnALNkpDIqazhsVBdLdkrgpyMJYrcc50LHEj8z0FnIUsID92glZl8DmVYkvD1ZdE0IpMe8hlnpi0q7Lby\" alt=\"\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">5. Singleton \u2013 Only single instance remains in the system<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>public<\/strong> <strong>final<\/strong> <strong>class<\/strong> <strong>Singleton<\/strong> {<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>private<\/strong> <strong>static<\/strong> <strong>volatile<\/strong> Singleton instance;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>private<\/strong> Singleton() {}<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>public<\/strong> <strong>static<\/strong> Singleton getInstance(<strong>String<\/strong> value) {<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp; <strong>if<\/strong> (instance == <strong>null<\/strong>) {<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong>synchronized<\/strong> (Singleton.<strong>class<\/strong>) {<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong>if<\/strong> (instance == <strong>null<\/strong>) {<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;instance = <strong>new<\/strong> Singleton();<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp; }<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp; <strong>return<\/strong> instance;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">}<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">}<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Structural Pattern<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">1.&nbsp; &nbsp; &nbsp; Adapter &#8211; Convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn&#8217;t otherwise because of incompatible interfaces. It is used for third party API \/ it is retrofitted for previous code.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">public interface IWetherFinder {<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;public double getTemperature(String cityName);<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">}<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">class WeatherFinder implements IWetherFinder{<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;@Override<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;public double getTemperature(String cityName){<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return 40;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;}<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">}<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">interface IWeatherFinderClient<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">{<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;public double getTemperature(String zipcode);<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">}&nbsp;&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">public class WeatherAdapter implements IWeatherFinderClient {<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;@Override<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;public double getTemperature(String zipcode) {<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/method to get cityname by zipcode&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String cityName = getCityName(zipcode);<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/invoke actual service<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;IWetherFinder wetherFinder = new WeatherFinder();<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return wetherFinder.getTemperature(cityName);<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;}<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;private String getCityName(String zipCode) {<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return &#8220;Bangalore&#8221;;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;}<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">}<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh4.googleusercontent.com\/jZ1StetV4vNBsBR2HAsOvoM2JINGLn8bNfUGBh7UWv4F-X27tu5qJ-Ux7tRPJGQqW0xB7zu4kLIauZy8LPRk-5s3nuKUWm9Rc8dVxF21uqyWCwMHKZWtQ28qxJ3xVX0tglEUy9yD\" alt=\"\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Here AudioPlayer can play mp3 files by default. If it uses MediaAdapter then it can play other formats too.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">MediaAdapter and AudioPlayer both implement a MediaPlayer interface that only has a play function.&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">While AudioPlayer invokes mp3 based on filetype, MediaAdapter invokes different players in case of filetype.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">AudioPlayer uses MediaAdapter and finds file formats of some other type.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This way AudioPlayer is decoupled from knowing the interface of VLC and MP4 players.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Adapter knows the interface of the existing system. It provides another interface to access multiple such objects.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">2.&nbsp; &nbsp; &nbsp;&nbsp;Composite Design Pattern<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Compose objects into tree structures to represent whole-part hierarchies. Composite lets clients treat individual objects and compositions of objects uniformly.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Recursive composition<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&#8220;Directories contain entries, each of which could be a directory.&#8221;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">File and directories are the same as inodes in linux.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh6.googleusercontent.com\/zz3IyZhyFkGTdvOGFIIViIFXrLowZReDYh0kN2pnr4BucOrWYDqnaqNf3DR7kVESRrnwFIAnJ2tk-ZxhUb8xBspkvh3zCE_dYoU4jJLCMROSv4-gSECoV3U9nwFudpzsIK6HbVb_\" alt=\"\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">So if it is listing contents of a file &#8211; the list function will show filename for file and for folder it will loop over all files in the folder and show list for each of them.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In case it is not implemented then, for the list function of folders , we need to find the type of the object it is and then call its list method.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">3.&nbsp; &nbsp; &nbsp; Decorator pattern<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You want to add behavior or state to individual objects at run-time. Inheritance is not feasible because it is static and applies to an entire class.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh5.googleusercontent.com\/hC9zHwmapxeqT1vV4F9CjXtTbjevlxEs8GT-OFrIsDLD3F2If9lP57IaxmmrX5FRql65kKwIQUVcVRKJJVhdQhr8lfjdpOL3xVMDXUpT3s_eMndlX6DgC-vWGTeCqPGbMNRuOj-8\" alt=\"\"\/><\/figure>\n\n\n\n<ol class=\"wp-block-list\"><li>Create a &#8220;lowest common denominator&#8221; that makes classes interchangeable<\/li><li>Create a second level base class for optional functionality<\/li><li>&#8220;Core&#8221; class and &#8220;Decorator&#8221; class declare an &#8220;is a&#8221; relationship<\/li><li>Decorator class &#8220;has a&#8221; instance of the &#8220;lowest common denominator&#8221;<\/li><li>Decorator class delegates to the &#8220;has a&#8221; object<\/li><\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">There is \u201c is a\u201d and \u201chas a\u201d relationship together<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Used for Readers, Writers in Java, UI components<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh5.googleusercontent.com\/QC7gzss1TOI6oiSsEcbfvle2882R7oDIaP_QjVub67p6qA-3K_B12GEMM4DonsJcLJg-Y5nr3vjJuzF78Pgy4_HoqEoXrZoEihj8nJWV0Fr3e6SuFZkP_Hd7Co90tQ-npiFwlT_t\" alt=\"\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">4.&nbsp; &nbsp; &nbsp; Fa\u00e7ade Pattern<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Facade pattern hides the complexities of the system and provides an interface to the client using which the client can access the system.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh4.googleusercontent.com\/mpU5DNrtEg3Sp1AJPHQMIAS8SurnMm0Ng41eUql9T1VGxos2vzBrhPZcMWkMjQ8XDWwsCVPzBJZkf-VTmSYp6DGZTE_c-i9H3uLi0hYqzE3TqmBYQcYNy1-50SjgIaRWYr3jChFk\" alt=\"\"\/><\/figure>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh3.googleusercontent.com\/3gexAzZflT2YZkeAexDsbuuFiY1Q5F4gVdu5DTjh74xt1oiZrKjkR9Vtp9JqMaaJoLAANzH4bLX5D-Rxa03CBD4D79UAR4rxlA9VCnPcSXPeYABO-0BKnoOOA7DRBrM2Vhwfy_m8\" alt=\"\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">5.&nbsp; &nbsp; &nbsp; Proxy Pattern<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Use an extra level of indirection to support distributed, controlled, or intelligent access.&nbsp;<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh3.googleusercontent.com\/rBw09T2HHewqkN8gOiT-1Wy2khLa6CF6ApF8fU4NkiIk-qjOOYN7Ku0baaczmRc_-EJZFjP6iXhaAuJY_2kVm4egRVxE4Hkd-MfWomqnF6lMK6KakFwihjvy6r4h-hUTD0diWIi5\" alt=\"\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Proxy class has all methods and properties of the actual class. Also, it \u201chas a\u201d real class instance. Whenever a call goes to proxy class then it sees if the real object is instantiated or not, if not then it instantiates.&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Proxy function does something extra and then calls the real class\u2019s function.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Used for lazy initialization also for fallback methods in Hystrix<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">6. Bridge Design pattern<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The Bridge design pattern allows you to separate the abstraction from the implementation.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">There are 2 parts in Bridge design pattern :<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Abstraction<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Implementation<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Example &#8211; JDBC driver connections&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Similar to Adapter but it is made during the implementation of the code.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh3.googleusercontent.com\/zPqPN11nGveep7I9UkC_bX6NucGqS4UgKS9fA9olEzULxp7bAkQ5IaGz2U8mfDRu_agUqYWQ_RRqTzQN5uxDgZjd_oH1XVOr_g6G33oEj-wihaV5ljsrXs4RpPuifr7hmzf0WTS6\" alt=\"\"\/><\/figure>\n\n\n\n<figure class=\"wp-block-embed\"><div class=\"wp-block-embed__wrapper\">\nhttps:\/\/www.geeksforgeeks.org\/bridge-design-pattern\/\n<\/div><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Behavioural<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">1. &nbsp; &nbsp; &nbsp;Template pattern<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Define the skeleton of an algorithm in an operation, deferring some steps to client subclasses. Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithm&#8217;s structure.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh4.googleusercontent.com\/BqeALrn59tEQoCN7TYmqzLcBP05mkVgt-Z8IqUs2n1vELFT1HOFi8QkAdsGzPSBsDKcAF0L7xBB6qtUrQB_NgUD9SX0x_8znt8FJfdfTtEG8YiZFvOIBt2MHenOKEUcbzroy0i4j\" alt=\"\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">2. Observer Pattern<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Observer pattern is used when there is one-to-many relationship between objects such as if one object is modified, its dependent objects are to be notified automatically.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh6.googleusercontent.com\/LIV06dAGByUondzA1AixGLCHWH2bicIf2wyIw4I-nWEBIujvAx92i5wQSI-A7W7c44mOLVf1A54A6akJezcdYnYXSFR5eqa-uyONEcg_IAMWKk52kYRMvuIzg0jmPq349N90TiZh\" alt=\"\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Very useful in UI &#8211; for inter widget communication<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">3. Strategy Pattern<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In a Strategy pattern, a class behavior or its algorithm can be changed at run time.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh4.googleusercontent.com\/YLr3ZH8bhs0ECtU5QMKHGOqDshdsC-9Ha4kUsUfPWsPYZLLg9yDelKr8bReDFoGHCDNkOVyFDvU8Ks7teE_P-4aI4xOUyqoaScMkbUFLVVU9Il1kf1LO4cIs_r-XPgJk29p8hNzl\" alt=\"\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n","protected":false},"excerpt":{"rendered":"<p>SOLID Design Principles Single Responsibility Pattern &#8211; class has a single responsibility like Class Sort won&#8217;t have functions to add or delete from the list. ex Microservices Open &#8211; Closed Principle &#8211; Open for extension and closed for modification. It tells you to write your code so that you will be able to add new [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[97],"tags":[],"class_list":["post-1787","post","type-post","status-publish","format-standard","hentry","category-tech-learnings"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.6 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Design Principles &amp; Design patterns &#187; Gaurav Wadhwani<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/gauravw.com\/blog\/2021\/04\/design-principles-design-patterns\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Design Principles &amp; Design patterns &#187; Gaurav Wadhwani\" \/>\n<meta property=\"og:description\" content=\"SOLID Design Principles Single Responsibility Pattern &#8211; class has a single responsibility like Class Sort won&#8217;t have functions to add or delete from the list. ex Microservices Open &#8211; Closed Principle &#8211; Open for extension and closed for modification. It tells you to write your code so that you will be able to add new [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/gauravw.com\/blog\/2021\/04\/design-principles-design-patterns\/\" \/>\n<meta property=\"og:site_name\" content=\"Gaurav Wadhwani\" \/>\n<meta property=\"article:published_time\" content=\"2021-04-10T10:10:46+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-04-10T10:10:51+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/lh3.googleusercontent.com\/5tI-978FnMAuscZEFonU8gnqyNmLuIDCAVLIG3Y8VHdD152FfLLXGEDh8X5U_-BKPLVdN0xTmxOxg-4YIH23hEDDCuvCuKfhcjG-2MMDd0-S129omHFoGwfUO6VW5UoM5CFmx2pO\" \/>\n<meta name=\"author\" content=\"Gaurav Wadhwani\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Gaurav Wadhwani\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"10 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/gauravw.com\\\/blog\\\/2021\\\/04\\\/design-principles-design-patterns\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/gauravw.com\\\/blog\\\/2021\\\/04\\\/design-principles-design-patterns\\\/\"},\"author\":{\"name\":\"Gaurav Wadhwani\",\"@id\":\"https:\\\/\\\/gauravw.com\\\/blog\\\/#\\\/schema\\\/person\\\/9a05a9c3487f35f6b4577c6956cf252e\"},\"headline\":\"Design Principles &#038; Design patterns\",\"datePublished\":\"2021-04-10T10:10:46+00:00\",\"dateModified\":\"2021-04-10T10:10:51+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/gauravw.com\\\/blog\\\/2021\\\/04\\\/design-principles-design-patterns\\\/\"},\"wordCount\":2025,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/gauravw.com\\\/blog\\\/#\\\/schema\\\/person\\\/9a05a9c3487f35f6b4577c6956cf252e\"},\"image\":{\"@id\":\"https:\\\/\\\/gauravw.com\\\/blog\\\/2021\\\/04\\\/design-principles-design-patterns\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/lh3.googleusercontent.com\\\/5tI-978FnMAuscZEFonU8gnqyNmLuIDCAVLIG3Y8VHdD152FfLLXGEDh8X5U_-BKPLVdN0xTmxOxg-4YIH23hEDDCuvCuKfhcjG-2MMDd0-S129omHFoGwfUO6VW5UoM5CFmx2pO\",\"articleSection\":[\"Tech Learnings\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/gauravw.com\\\/blog\\\/2021\\\/04\\\/design-principles-design-patterns\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/gauravw.com\\\/blog\\\/2021\\\/04\\\/design-principles-design-patterns\\\/\",\"url\":\"https:\\\/\\\/gauravw.com\\\/blog\\\/2021\\\/04\\\/design-principles-design-patterns\\\/\",\"name\":\"Design Principles & Design patterns &#187; Gaurav Wadhwani\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/gauravw.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/gauravw.com\\\/blog\\\/2021\\\/04\\\/design-principles-design-patterns\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/gauravw.com\\\/blog\\\/2021\\\/04\\\/design-principles-design-patterns\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/lh3.googleusercontent.com\\\/5tI-978FnMAuscZEFonU8gnqyNmLuIDCAVLIG3Y8VHdD152FfLLXGEDh8X5U_-BKPLVdN0xTmxOxg-4YIH23hEDDCuvCuKfhcjG-2MMDd0-S129omHFoGwfUO6VW5UoM5CFmx2pO\",\"datePublished\":\"2021-04-10T10:10:46+00:00\",\"dateModified\":\"2021-04-10T10:10:51+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/gauravw.com\\\/blog\\\/2021\\\/04\\\/design-principles-design-patterns\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/gauravw.com\\\/blog\\\/2021\\\/04\\\/design-principles-design-patterns\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/gauravw.com\\\/blog\\\/2021\\\/04\\\/design-principles-design-patterns\\\/#primaryimage\",\"url\":\"https:\\\/\\\/lh3.googleusercontent.com\\\/5tI-978FnMAuscZEFonU8gnqyNmLuIDCAVLIG3Y8VHdD152FfLLXGEDh8X5U_-BKPLVdN0xTmxOxg-4YIH23hEDDCuvCuKfhcjG-2MMDd0-S129omHFoGwfUO6VW5UoM5CFmx2pO\",\"contentUrl\":\"https:\\\/\\\/lh3.googleusercontent.com\\\/5tI-978FnMAuscZEFonU8gnqyNmLuIDCAVLIG3Y8VHdD152FfLLXGEDh8X5U_-BKPLVdN0xTmxOxg-4YIH23hEDDCuvCuKfhcjG-2MMDd0-S129omHFoGwfUO6VW5UoM5CFmx2pO\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/gauravw.com\\\/blog\\\/2021\\\/04\\\/design-principles-design-patterns\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/gauravw.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Design Principles &#038; Design patterns\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/gauravw.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/gauravw.com\\\/blog\\\/\",\"name\":\"Gaurav Wadhwani\",\"description\":\"Where I write \\\/ scribble\",\"publisher\":{\"@id\":\"https:\\\/\\\/gauravw.com\\\/blog\\\/#\\\/schema\\\/person\\\/9a05a9c3487f35f6b4577c6956cf252e\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/gauravw.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/gauravw.com\\\/blog\\\/#\\\/schema\\\/person\\\/9a05a9c3487f35f6b4577c6956cf252e\",\"name\":\"Gaurav Wadhwani\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/788ed9666a6c4e011516ae9c744df4be274dcf933161c99a4ec7e06311d2d416?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/788ed9666a6c4e011516ae9c744df4be274dcf933161c99a4ec7e06311d2d416?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/788ed9666a6c4e011516ae9c744df4be274dcf933161c99a4ec7e06311d2d416?s=96&d=mm&r=g\",\"caption\":\"Gaurav Wadhwani\"},\"logo\":{\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/788ed9666a6c4e011516ae9c744df4be274dcf933161c99a4ec7e06311d2d416?s=96&d=mm&r=g\"},\"sameAs\":[\"http:\\\/\\\/gauravw.com\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Design Principles & Design patterns &#187; Gaurav Wadhwani","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:\/\/gauravw.com\/blog\/2021\/04\/design-principles-design-patterns\/","og_locale":"en_US","og_type":"article","og_title":"Design Principles & Design patterns &#187; Gaurav Wadhwani","og_description":"SOLID Design Principles Single Responsibility Pattern &#8211; class has a single responsibility like Class Sort won&#8217;t have functions to add or delete from the list. ex Microservices Open &#8211; Closed Principle &#8211; Open for extension and closed for modification. It tells you to write your code so that you will be able to add new [&hellip;]","og_url":"https:\/\/gauravw.com\/blog\/2021\/04\/design-principles-design-patterns\/","og_site_name":"Gaurav Wadhwani","article_published_time":"2021-04-10T10:10:46+00:00","article_modified_time":"2021-04-10T10:10:51+00:00","og_image":[{"url":"https:\/\/lh3.googleusercontent.com\/5tI-978FnMAuscZEFonU8gnqyNmLuIDCAVLIG3Y8VHdD152FfLLXGEDh8X5U_-BKPLVdN0xTmxOxg-4YIH23hEDDCuvCuKfhcjG-2MMDd0-S129omHFoGwfUO6VW5UoM5CFmx2pO","type":"","width":"","height":""}],"author":"Gaurav Wadhwani","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Gaurav Wadhwani","Est. reading time":"10 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/gauravw.com\/blog\/2021\/04\/design-principles-design-patterns\/#article","isPartOf":{"@id":"https:\/\/gauravw.com\/blog\/2021\/04\/design-principles-design-patterns\/"},"author":{"name":"Gaurav Wadhwani","@id":"https:\/\/gauravw.com\/blog\/#\/schema\/person\/9a05a9c3487f35f6b4577c6956cf252e"},"headline":"Design Principles &#038; Design patterns","datePublished":"2021-04-10T10:10:46+00:00","dateModified":"2021-04-10T10:10:51+00:00","mainEntityOfPage":{"@id":"https:\/\/gauravw.com\/blog\/2021\/04\/design-principles-design-patterns\/"},"wordCount":2025,"commentCount":0,"publisher":{"@id":"https:\/\/gauravw.com\/blog\/#\/schema\/person\/9a05a9c3487f35f6b4577c6956cf252e"},"image":{"@id":"https:\/\/gauravw.com\/blog\/2021\/04\/design-principles-design-patterns\/#primaryimage"},"thumbnailUrl":"https:\/\/lh3.googleusercontent.com\/5tI-978FnMAuscZEFonU8gnqyNmLuIDCAVLIG3Y8VHdD152FfLLXGEDh8X5U_-BKPLVdN0xTmxOxg-4YIH23hEDDCuvCuKfhcjG-2MMDd0-S129omHFoGwfUO6VW5UoM5CFmx2pO","articleSection":["Tech Learnings"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/gauravw.com\/blog\/2021\/04\/design-principles-design-patterns\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/gauravw.com\/blog\/2021\/04\/design-principles-design-patterns\/","url":"https:\/\/gauravw.com\/blog\/2021\/04\/design-principles-design-patterns\/","name":"Design Principles & Design patterns &#187; Gaurav Wadhwani","isPartOf":{"@id":"https:\/\/gauravw.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/gauravw.com\/blog\/2021\/04\/design-principles-design-patterns\/#primaryimage"},"image":{"@id":"https:\/\/gauravw.com\/blog\/2021\/04\/design-principles-design-patterns\/#primaryimage"},"thumbnailUrl":"https:\/\/lh3.googleusercontent.com\/5tI-978FnMAuscZEFonU8gnqyNmLuIDCAVLIG3Y8VHdD152FfLLXGEDh8X5U_-BKPLVdN0xTmxOxg-4YIH23hEDDCuvCuKfhcjG-2MMDd0-S129omHFoGwfUO6VW5UoM5CFmx2pO","datePublished":"2021-04-10T10:10:46+00:00","dateModified":"2021-04-10T10:10:51+00:00","breadcrumb":{"@id":"https:\/\/gauravw.com\/blog\/2021\/04\/design-principles-design-patterns\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/gauravw.com\/blog\/2021\/04\/design-principles-design-patterns\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/gauravw.com\/blog\/2021\/04\/design-principles-design-patterns\/#primaryimage","url":"https:\/\/lh3.googleusercontent.com\/5tI-978FnMAuscZEFonU8gnqyNmLuIDCAVLIG3Y8VHdD152FfLLXGEDh8X5U_-BKPLVdN0xTmxOxg-4YIH23hEDDCuvCuKfhcjG-2MMDd0-S129omHFoGwfUO6VW5UoM5CFmx2pO","contentUrl":"https:\/\/lh3.googleusercontent.com\/5tI-978FnMAuscZEFonU8gnqyNmLuIDCAVLIG3Y8VHdD152FfLLXGEDh8X5U_-BKPLVdN0xTmxOxg-4YIH23hEDDCuvCuKfhcjG-2MMDd0-S129omHFoGwfUO6VW5UoM5CFmx2pO"},{"@type":"BreadcrumbList","@id":"https:\/\/gauravw.com\/blog\/2021\/04\/design-principles-design-patterns\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/gauravw.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Design Principles &#038; Design patterns"}]},{"@type":"WebSite","@id":"https:\/\/gauravw.com\/blog\/#website","url":"https:\/\/gauravw.com\/blog\/","name":"Gaurav Wadhwani","description":"Where I write \/ scribble","publisher":{"@id":"https:\/\/gauravw.com\/blog\/#\/schema\/person\/9a05a9c3487f35f6b4577c6956cf252e"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/gauravw.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/gauravw.com\/blog\/#\/schema\/person\/9a05a9c3487f35f6b4577c6956cf252e","name":"Gaurav Wadhwani","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/788ed9666a6c4e011516ae9c744df4be274dcf933161c99a4ec7e06311d2d416?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/788ed9666a6c4e011516ae9c744df4be274dcf933161c99a4ec7e06311d2d416?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/788ed9666a6c4e011516ae9c744df4be274dcf933161c99a4ec7e06311d2d416?s=96&d=mm&r=g","caption":"Gaurav Wadhwani"},"logo":{"@id":"https:\/\/secure.gravatar.com\/avatar\/788ed9666a6c4e011516ae9c744df4be274dcf933161c99a4ec7e06311d2d416?s=96&d=mm&r=g"},"sameAs":["http:\/\/gauravw.com"]}]}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/gauravw.com\/blog\/wp-json\/wp\/v2\/posts\/1787","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gauravw.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gauravw.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gauravw.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gauravw.com\/blog\/wp-json\/wp\/v2\/comments?post=1787"}],"version-history":[{"count":1,"href":"https:\/\/gauravw.com\/blog\/wp-json\/wp\/v2\/posts\/1787\/revisions"}],"predecessor-version":[{"id":1788,"href":"https:\/\/gauravw.com\/blog\/wp-json\/wp\/v2\/posts\/1787\/revisions\/1788"}],"wp:attachment":[{"href":"https:\/\/gauravw.com\/blog\/wp-json\/wp\/v2\/media?parent=1787"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gauravw.com\/blog\/wp-json\/wp\/v2\/categories?post=1787"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gauravw.com\/blog\/wp-json\/wp\/v2\/tags?post=1787"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}