Monday, May 30, 2022

Design Principles and Design Pattern

 Design Principles

  • Encapsulate what varies
  • Favor Composition over Inheritance
  • Loose Coupling
  • Program to interfaces
  • SOLID Principle
    • Single Responsibility Principle
    • Open/Closed Principle
    • Liskov's substitution principle
    • Interface Segregation Principle
    • Dependency Inversion Principle.

   Design Patterns

  • Creational Design Pattern
    • Factory Pattern
      Let the factory do the job of creating the objects rather than instantiating the object using New
      https://java-design-patterns.com/patterns/factory/ 
    • Abstract Factory Pattern -
      Creating a Factory from list of factories.
      https://java-design-patterns.com/patterns/abstract-factory/
    • Builder Pattern
      The builder pattern is an object creation software design pattern with the intentions of finding a solution to the telescoping constructor anti-pattern. Telescoping constructor anti-pattern means that the constructor is getting bigger and bigger due to addition of a lot of parameters.
      https://java-design-patterns.com/patterns/builder/
    • Prototype Pattern - Clone the existing object and then make modification to the object rather than creating the object from scratch.
      https://java-design-patterns.com/patterns/prototype/
      In java can be done by implementing the Cloneable  and overriding the Clone() method. But Clonable makes a shallow copy. We need to override the clone method for the child objects and the clonable method should be updated
    • Singleton Pattern

      public class SingletonPatternDemo {
      
      	public static void main(String args[]){
      
      		SingletonClass singletonObject = SingletonClass.getInstance();
      
      		singletonObject = SingletonClass.getInstance();
      
      		try {
      			singletonObject.Clone();
      		} catch (CloneNotSupportedException e) {
      			e.printStackTrace();
      		}
      	}
      
      }
      
      class SingletonClass{
      	private volatile static SingletonClass singletonObject;
      
      	private SingletonClass(){
      		System.out.println("Construting SingletonClass");
      	}
      
      	public static SingletonClass getInstance(){
      		if(singletonObject == null){
      			synchronized(SingletonClass.class){
      				if(singletonObject == null){
      					singletonObject = new SingletonClass();
      				}
      			}
      		}
      		return singletonObject;
      	}
      
      	public Object Clone() throws CloneNotSupportedException{
      		throw new CloneNotSupportedException();
      	} 

      }

       

  • Structural Design Pattern
    • Adapter Pattern
    • Bridge Pattern
    • Composite Pattern
      The composite pattern is used when single objects and multiple objects should be treated uniformly.
    • Decorator Pattern
      The decorator pattern allows you to alter behavior dynamically, without affecting other objects of the same type.
    • Facade Pattern
      A facade is used to allow the user to perform complex processes with simple actions.
    • Flyweight Pattern
      The flyweight pattern can be used to save memory by using a single object instead of many.
    • Proxy Pattern
      The proxy pattern allows you to put off expensive operations until they are used.
  • Behavioral Design Pattern
    • Publisher/subscriber pattern



Creational Design Pattern


Structural Design Pattern

Reference :

  • https://java-design-patterns.com/patterns
  • https://www.linkedin.com/learning/java-design-patterns-creational
  • https://www.linkedin.com/learning/java-design-patterns-structural

No comments:

Post a Comment