Design Patterns

 Design Patterns 


👉 Design patterns are solution for solving common object-oriented design problem/task.
👉 Design pattern represent idea not particular implementations.
👉 Using design pattern code will be more flexible, reusable and maintainable.
👉 3 type of design patterns

💙 Behavioral Design Pattern- 
                This pattern is concerned with algorithm and assignment of responsibility between objects.
💙 Creational Design Pattern- 
                This pattern provides various object creation mechanism which increase flexibility and reusability of code.
💙 Structural Design Pattern- 
                This pattern explains how assemble object and classes into larger structure while keeping this structure flexible and efficient.


💙Creational Design Pattern

😀 Singleton Design Pattern:
        
👉 Singleton Pattern is creational design pattern
👉 Class has only one instance and provide global point of access to it.

Create Singleton class-
    ➤ Static member - Static instance of singleton class.
    ➤ Private Constructor - It prevent instantiate of singleton class from outside of class.
    ➤ Static factory method- It provide global point of access to singleton object and return instance to the caller.

We can create singleton class in multiple ways:
1. Singleton with Lazy initialization 

Public class Singleton{

    Private static Singleton singleton_instance= null;

    Private Singleton(){}

    Public static Singleton getInstance(){
        If(singleton_instance == null){
            singleton_instance = new Singleton();
        }
        Return singleton_instance;
    }
}
2. Singleton with Eager initializations

Public class Singleton{
    Private static Singleton singleton_instance= new Singleton; 

    Private Singleton(){}

    Public static Singleton getInstance(){
        If(singleton_instance == null){
            return singleton_instance;
        }
    }
}
3. Singleton class in multithreading environment 

Public class Singleton{
    Private static Singleton singleton_instance = null;

    Private Singleton(){}

    Public static Singleton getInstance(){
        if(singleton_instance == null){
            synchronized (Singleton.class){
                If(singleton_instance == null){
                    singleton_instance = new singleton_instance;
                }
            }

        }
    }
return singleton_instance;
}


💙 Factory Design Pattern

 
👉 Define interface or abstract class for creating object but let subclass decide which class to initiate.
👉 Subclass is responsible to create the instance of the class

public interface Notification{
    void notifyUser();
}


public class EmailNotification implements Notification{
@override
public void  notifyUser(){
sysout("email");
}
}

public class SMSNotification implements Notification{
@override
public void  notifyUser(){
sysout("sms");
}
}

public class pushNotification implements Notification{
@override
public void  notifyUser(){
sysout("push");
}
}

public class NotificationFactory{
public Notification createNotification(String channel){
if(channel == null || channel.isEmpty())
return null;

switch(channel){
case "SMS":
return new SMSNotification();
case "EMAIL":
return new EmailNotification();
case "PUSH":
return new PushNotification();
default"
throw new IllegalArgumentException("");
}
}
}


public class NotificationService{
public static void main(String[] args){
NotificationFactory nf = new NotificationFactory();
Notification n = nf.createNotification("EMAIL");
n.notifyUser():
}
}



Next patterns Will update soon!!!!

Comments

Popular posts from this blog

Java 8 Features

Welcome to Java study !!!

Internal Working