Java/J2EE and SOA (349 Blogs) Become a Certified Professional
AWS Global Infrastructure

Programming & Frameworks

Topics Covered
  • C Programming and Data Structures (16 Blogs)
  • Comprehensive Java Course (5 Blogs)
  • Java/J2EE and SOA (346 Blogs)
  • Spring Framework (8 Blogs)
SEE MORE

Important Java Design Patterns You Need to Know About

Last updated on Jul 05,2024 38.5K Views

Ravi Kiran
Tech Enthusiast working as a Research Analyst at Edureka. Curious about learning... Tech Enthusiast working as a Research Analyst at Edureka. Curious about learning more about Data Science and Big-Data Hadoop.
17 / 22 Blog from Advance Java

Design patterns are general solutions to routine problems in software design. Every pattern acts as a blueprint that allows customization to solve a given design problem in any code during the development of software modules. I have lined up the docket for this article as below:

Why do we need Design Patterns?

A design pattern systematically names, motivates and explains a general design that addresses a recurring design problem in object-oriented systems. Design patterns are needed to represent some of the best practices followed and adopted in software development.

Design-Patterns-in-Java-Edureka

What are Design Patterns?

Software Design Pattern can be defined as a software template or a description to solve a problem that occurs in multiple instances while designing a Software Application or a Software Framework. 

The credit of Design Patterns goes to the Gang of Four. Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides were the authors of the book on the Java Design Patterns.

Java-Design-Patterns-Edureka

 

Java Full Course – 10 Hours | Java Full Course for Beginners | Java Tutorial for Beginners | Edureka

🔥𝐄𝐝𝐮𝐫𝐞𝐤𝐚 𝐉𝐚𝐯𝐚 𝐂𝐨𝐮𝐫𝐬𝐞 𝐓𝐫𝐚𝐢𝐧𝐢𝐧𝐠: https://www.edureka.co/java-j2ee-training-course (Use code “𝐘𝐎𝐔𝐓𝐔𝐁𝐄𝟐𝟎”)
This Edureka Java Full Course will help you understand the various fundamentals of Java programm…

Structure of a Design Pattern

Structure of any Design Pattern is considered to be highly organised. They are often documented in the form of a template such that, the users can visually identify the problem and instantaneously find the solution to it, based on the relationship between classes and objects. The Design Patterns Template described by the original authors of the Design Patterns is as follows:

Term

Description

Pattern NameDescribes the essence of the pattern in a short, but expressive name.
IntentDescribes what the Pattern Does
Also, known asList any synonyms of the pattern
MotivationProvides an example of the problem and describes the solution to it
ApplicabilityLists the situations where the pattern is applicable
StructureSet of diagrams of the classes and objects that depict the pattern
ParticipantsDescribes the classes and objects that participate in the design
CollaborationsDescribes the Participants collaboration and their responsibilities.
ConsequencesDescribes the forces that exist with the pattern and the benefits

 

Types of Design Patterns

Typically, Java Design Patterns are divided into Four Categories and each of those are further classified as below:

Java-design-patterns-types-edureka

 

  • Creational Design Patterns are concerned with the method of creating Objects.
  • Structural Design Patterns deal with the composition of classes and objects which form larger structures.
  • Behaviour Design Patterns are concerned with the responsibility and interaction between the objects.
  • JEE Design Patterns are concerned with providing solutions to the Java EE-based software applications and frameworks.

 

Creational Design Patterns are preferred during the process of class initiation. The Creational Design Patterns are further classified as follows:

  1. Factory Pattern
  2. Abstract Factory Pattern
  3. Singleton Pattern
  4. Prototype Pattern
  5. Builder Pattern.
  6. Object Pool Pattern

Let us discuss some important Creational Java Design Patterns practically.

 

Factory Design Pattern: It follows the principle of “Define an interface or abstract class for creating an object but let the subclasses decide which class to instantiate. The Factory Method Pattern is also known as Virtual Constructor.

Advantages:

  • It is designed to allow all the sub-classes to choose the type of objects so as to create them.

Example: We have three Cellular Network Plan which describes the call cost per minute. Here we have three different networks namely, abcNetwork, pqrNetwork, and xyzNetwork along with their charges per minute. Let us find out the cost of a certain number of minutes on each network.

UML Diagram:

factory uml//cellular plan


package FactoryDesignPattern;

abstract class cellularplan {
         protected double rate;
         abstract void getRate();
         public void processBill(int minutes){
                System.out.println(minutes*rate);
         }
}

//abcNetwork


package FactoryDesignPattern;

public class abcNetwork extends cellularplan{
       public void getRate(){
              rate=1.50;
      }
}

//pqrNetwork


package FactoryDesignPattern;

public class pqrNetwork extends cellularplan{
        public void getRate(){
               rate=1.75;
        }
}

//xyzNetwork

package FactoryDesignPattern;

public class xyzNetwork extends cellularplan{
       public void getRate(){
            rate=1.50;
       }
}

//SelectNetworkFactory


package FactoryDesignPattern;

public class SelectNetworkFactory {
     public cellularplan getPlan(String planType){
          if(planType == null){
                  return null;
          }
          if(planType.equalsIgnoreCase("abcNetwork")) {
                  return new abcNetwork();
          }
          else if(planType.equalsIgnoreCase("xyzNetwork")){
                  return new xyzNetwork();
          }
          else if(planType.equalsIgnoreCase("pqrNetwork")) {
                  return new pqrNetwork();
          }
     return null;
     }
}

//PhoneBill

package FactoryDesignPattern;
import java.io.*;

public class phoneBill {
      public static void main(String args[])throws IOException{
            SelectNetworkFactory planFactory = new SelectNetworkFactory();
            System.out.print("Enter the name of network for which the bill will be generated: ");
            BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
            String networkName=br.readLine();
            System.out.print("Enter the number of minutes for bill will be calculated: ");
            int minutes=Integer.parseInt(br.readLine());
            cellularplan p = planFactory.getPlan(networkName);
            System.out.print("Bill amount for "+networkName+" of "+minutes+" units is: ");
            p.getRate();
            p.processBill(minutes);
      }
}

Output:

Enter the name of network for which the bill will be generated: abcNetwork
Enter the number of minutes for bill will be calculated: 35
Bill amount for abcNetwork of 35 units is: 52.5

Related Article: Top 30 Java Pattern Programs

Singleton Design Pattern: It follows “define a class that has only one instance and provides a global point of access to it“. The class must ensure that only a single instance should be created and a single object can be used by all other classes.

Advantages:

  • It is designed to save memory.
  • The object is not created on every request, instead, a single instance is reused.

Example: We have a MySQL Database. Let us insert data into the database using one single object instance. Here, we have 4 different operations that are performed onto the database and all of those operations use one single object instance.

UML Diagram:

singleton uml

 

package Singleton;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

class JDBCSingleton {
      private static JDBCSingleton jdbc;
      private JDBCSingleton() { }
      public static JDBCSingleton getInstance() {
             if (jdbc==null){
                    jdbc=new JDBCSingleton();
             }
             return jdbc;
      }
      private static Connection getConnection()throws ClassNotFoundException, SQLException{
             Connection con=null;
             Class.forName("com.mysql.cj.jdbc.Driver");
             con= DriverManager.getConnection("jdbc:mysql://localhost:3306/edureka?serverTimezone=Asia/Calcutta", "root", "");
             return con;
      }
      public int insert(String name, String pass, String id) throws SQLException{
              Connection c=null;
              PreparedStatement ps=null;
              int recordCounter=0;
      try {
                    c=this.getConnection();
                    ps=c.prepareStatement("insert into employee(uid,uname,upassword)values(?,?,?)");
                    ps.setString(1, id);
                    ps.setString(2, name);
                    ps.setString(3, pass);
                    recordCounter=ps.executeUpdate(); 
      } 
      catch (Exception e) { 
                    e.printStackTrace(); 
      } 
      finally{
                    if (ps!=null){
                             ps.close();
                    }
                    if(c!=null){
                             c.close();
                    }
      }
      return recordCounter;
}

public void view(String name) throws SQLException{
      Connection con = null;
      PreparedStatement ps = null;
      ResultSet rs = null;
      try {
                   con=this.getConnection();
                   ps=con.prepareStatement("select * from employee where uname=?");
                   ps.setString(1, name);
                   rs=ps.executeQuery();
                   while (rs.next()) {
                            System.out.println("Name= "+rs.getString(2)+"t"+"Paasword= "+rs.getString(3));
            }
      } 
      catch (Exception e) { 
             System.out.println(e);
            }
      finally{
            if(rs!=null){
                   rs.close();
            }
            if (ps!=null){
                   ps.close();
            }
            if(con!=null){
                   con.close();
            }
       }
}
public int update(String name, String password) throws SQLException {
       Connection c=null;
       PreparedStatement ps=null;
       int recordCounter=0;
       try {
                  c=this.getConnection();
                  ps=c.prepareStatement(" update employee set upassword=? where uname='"+name+"' ");
                  ps.setString(1, password);
                  recordCounter=ps.executeUpdate();
             } 
       catch (Exception e) { 
                  e.printStackTrace(); 
             } 
       finally{
                  if (ps!=null){
                         ps.close();
                   }
                  if(c!=null){
                         c.close();
                   }
             }
       return recordCounter;
}
public int delete(int userid) throws SQLException{
       Connection c=null;
       PreparedStatement ps=null;
       int recordCounter=0;
       try {
                   c=this.getConnection();
                   ps=c.prepareStatement(" delete from employee where uid='"+userid+"' ");
                   recordCounter=ps.executeUpdate();
          }
          catch (Exception e) { 
                   e.printStackTrace(); 
          }
         finally{
                  if (ps!=null){
                         ps.close();
                  }
                  if(c!=null){
                         c.close();
                  }
          }
        return recordCounter;
     }
}
package Singleton;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
class JDBCSingletonDemo{
static int count=1;
static int choice;
public static void main(String[] args) throws IOException {
          JDBCSingleton jdbc= JDBCSingleton.getInstance();
          BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
          do{
                   System.out.println("DATABASE OPERATIONS");
                   System.out.println(" --------------------- ");
                   System.out.println(" 1. Insertion ");
                   System.out.println(" 2. View ");
                   System.out.println(" 3. Delete ");
                   System.out.println(" 4. Update ");
                   System.out.println(" 5. Exit ");
                   System.out.print("n");
                   System.out.print("Please enter the choice what you want to perform in the database: ");
                   String input=br.readLine();
                   if(input != null) {
                           choice=Integer.parseInt(input);
                   }
          else{
                   return;
                 } 
          switch(choice) {
                   case 1:{ 
                                System.out.print("Enter the username you want to insert data into the database: ");
                                String username=br.readLine();
                                System.out.print("Enter the password you want to insert data into the database: ");
                                String password=br.readLine();
                                System.out.print("Enter the ID you want to insert data into the database: ");
                                String eid=br.readLine();
                                try {
                                       int i= jdbc.insert(username, password, eid);
                                       if (i>0) {
                                                System.out.println((count++) + " Data has been inserted successfully");
                                            }
                                       else{
                                                System.out.println("Data has not been inserted ");
                                            }
                                       } catch (Exception e) {
                                               System.out.println(e);
                                             }
                                       System.out.println("Press Enter key to continue...");
                                       br.readLine();
                              }
                              break;
                   case 2:{
                                System.out.print("Enter the username : ");
                                String username=br.readLine();
                                try {
                                       jdbc.view(username);
                                } 
                                catch (Exception e) {
                                       System.out.println(e);
                                }
                                System.out.println("Press Enter key to continue...");
                                br.readLine();
                              }
                              break;
                  case 3:{
                               System.out.print("Enter the userid, you want to delete: ");
                               int userid=Integer.parseInt(br.readLine());
                               try {
                                      int i= jdbc.delete(userid);
                                      if (i>0) {
                                                System.out.println((count++) + " Data has been deleted successfully");
                                      }
                                      else{
                                               System.out.println("Data has not been deleted");
                                             }
                                      } 
                                      catch (Exception e) {
                                               System.out.println(e);
                                      }
                                      System.out.println("Press Enter key to continue...");
                                      br.readLine();
                               }
                               break;
                   case 4:{
                                System.out.print("Enter the username, you want to update: ");
                                String username=br.readLine();
                                System.out.print("Enter the new password ");
                                String password=br.readLine();
                                try {
                                      int i= jdbc.update(username, password);
                                      if (i>0) {
                                               System.out.println((count++) + " Data has been updated successfully");
                                             }
                                } 
                                catch (Exception e) {
                                      System.out.println(e);
                                      }
                                       System.out.println("Press Enter key to continue...");
                                       br.readLine();
                                }
                                break;
                   default: return;
              } 
         } while (choice!=5);
     }
}

Output:

DATABASE OPERATIONS
---------------------
1. Insertion
2. View
3. Delete
4. Update
5. Exit

Please enter the choice what you want to perform in the database: 1
Enter the username you want to insert data into the database: Ravi
Enter the password you want to insert data into the database: 1234
Enter the ID you want to insert data into the database: 1011
1 Data has been inserted successfully
Press Enter key to continue...

 

Structural Design Patterns deal with the composition of classes and objects which form larger structures. They simplify the structure by identifying the relationship between the classes and objects. The Structural Design Patterns are further classified as follows:

  1. Facade Pattern
  2. Bridge Pattern
  3. Composite Pattern
  4. Decorator Pattern
  5. Adapter Pattern
  6. Flyweight Pattern
  7. Proxy Pattern
  8. Filter Pattern

Let us discuss some important Structural Java Design Patterns practically.

 

Facade Design Pattern: Describes a higher-level interface that makes the subsystem easier to use. Every Abstract Factory is a Facade Design Pattern.

Advantages:

  • It protects the end-users from the complex sub-system components.

Example: Now, let us use the Facade Design pattern to find out the cost of the franchise you wish to buy.

UML Diagram:

Java-Design-Pattern-Facade-Edureka//Franchise


package FacadeDesignPattern;

public interface Franchise {
      public void Option();
      public void Cost();
}

//KFC


public class KFC implements Franchise {
        public void Option() {
               System.out.println("KFC");
        }
public void Cost() {
        System.out.println("Rs 1,00,00,000");
        }
}

//McDonalds


package FacadeDesignPattern;

public class McDonalds implements Franchise {
       public void Option() {
             System.out.println("McDonalds");
       }
public void Cost() {
             System.out.println("Rs 90,00,000");
       }
}

//Dominos


package FacadeDesignPattern;

public class Dominos implements Franchise {
       public void Option() {
            System.out.println("Dominos");
       }
public void Cost() {
            System.out.println("Rs 80,00,000");
       }
}

//FranchiseServiceReg


package FacadeDesignPattern;

public class FranchiseServiceReg {
       private Franchise KFC;
       private Franchise McDonalds;
       private Franchise Dominos;
       public FranchiseServiceReg(){
              KFC = new KFC();
              McDonalds = new McDonalds();
              Dominos = new Dominos();
       }
       public void BuyKFCFranchise(){
              KFC.Option();
              KFC.Cost();
       } 
       public void BuyMcDonaldsFranchise(){
              McDonalds.Option();
              McDonalds.Cost();
       }
       public void BuyDominosFranchise(){
              Dominos.Option();
              Dominos.Cost();
       }
}

//Client


package FacadeDesignPattern;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Client {
         private static int choice;
         public static void main(String args[]) throws NumberFormatException, IOException{
                   do{
                              System.out.print("Welcome to Franchise Service Registration...!n");
                              System.out.print(" 1. KFC n");
                              System.out.print(" 2. McDonalds n");
                              System.out.print(" 3. Dominos n");
                              System.out.print(" 4. EXIT n");
                              System.out.print("Please Enter your Franchise Option Number: ");
                   BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
                   choice=Integer.parseInt(br.readLine());
                   FranchiseServiceReg sp=new FranchiseServiceReg();

         switch (choice) {
                   case 1: {
                           sp.BuyKFCFranchise();
                   }
                   break;
                  case 2: {
                          sp.BuyMcDonaldsFranchise();
                  }
                  break;
                   case 3: {
                          sp.BuyDominosFranchise();
                  }
                  break;
                  default: {
                          System.out.println("Please Check the input and try again");
                  }
          return;
          }

       }while(choice!=4);
    }
}

Output:

Welcome to Franchise Service Registration...!
1. KFC
2. McDonalds
3. Dominos
4. EXIT
Please Enter your Franchise Option Number: 1
KFC
Rs 1,00,00,000

 

Adapter Design Pattern: Provides the interface according to client requirement while using the services of a class with a different interface. The Adapter Pattern is also known as Wrapper.

Advantages:

  • It is designed to enable two or more previously incompatible objects to interact with each other.

Example: Here is a simple Library Card example which is designed to issue a Library card to a new user of the library which includes all the details like a book holder ID, Account number and many more. Let us execute this using Adapter Design Pattern.

Design-Patterns-in-Java-Edureka

UML Diagram:

Java-Design-Pattern-Adapter-Edureka//Adapter


package AdapterDesignPattern;

public class Adapter {
       public static void main(String args[]){
             LibraryCard targetInterface=new BookHolder();
             targetInterface.giveLibraryDetails();
             System.out.print(targetInterface.getLibraryCard());
        }
}

//BookHolder


package AdapterDesignPattern;
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class BookHolder extends LibraryDetails implements LibraryCard{
          public void giveLibraryDetails(){
          try{
                    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
                    System.out.print("Enter the Library account holder name :");
                    String readername=br.readLine();
                    System.out.print("n");
                    System.out.print("Enter the account number of the library:");
                    long accno=Long.parseLong(br.readLine());
                    System.out.print("n");
                    System.out.print("Enter the Library name :");
                    String libraryname=br.readLine();
                    setAccHolderName(readername);
                    setAccNumber(accno);
                    setLibraryName(libraryname);
          }
          catch(Exception e){
                    e.printStackTrace();
          }
     }
     public String getLibraryCard() {
          long accno=getAccNumber();
          String accholdername=getAccHolderName();
          String lname=getLibraryName();
          return ("The Account number "+accno+" of "+accholdername+" in "+lname+ " Library is valid and authenticated for issuing the Library card. ");
      }
}

//LibraryCard


package AdapterDesignPattern;

public interface LibraryCard {
      public void giveLibraryDetails();
      public String getLibraryCard();
}

//library details


package AdapterDesignPattern;

public class LibraryDetails {
      private String LibraryName;
      private String BookHolderName;
      private long BookHolderID;
      public String getLibraryName() {
             return LibraryName;
       }
       public void setLibraryName(String LibraryName) {
             this.LibraryName = LibraryName;
       }
       public String getAccHolderName() {
             return BookHolderName;
       }
       public void setAccHolderName(String BookHolderName) {
             this.BookHolderName = BookHolderName;
       }
       public long getAccNumber() {
             return BookHolderID;
       }
       public void setAccNumber(long BookHolderID) {
             this.BookHolderID = BookHolderID;
       }
}

Output:

Enter the Library account holder name :Ravi

Enter the account number of the library:12012

Enter the Library name :MG Library
The Account number 12012 of Ravi in MG Library Library is valid and authenticated for issuing the Library card.

 

Behavioural Design Patterns are concerned with the responsibility and interaction between the objects. These Design Patterns make sure the objects are loosely coupled, yet, can easily communicate with each other. Behavioural Design Patterns are further classified as follows:

  1. Chain Of Responsibility PatternDEsign-Patterns-in-Java-edureka-picture4
  2. Strategy Pattern
  3. Interpreter Pattern
  4. Iterator Pattern
  5. Mediator Pattern
  6. Memento Pattern
  7. Command Pattern
  8. State Pattern
  9. Observer Pattern
  10. Template Pattern
  11. Visitor Pattern

Let us discuss some important Behavioural Design Patterns practically.

 

Strategy Design Pattern: It defines a family of functionality and encapsulates each one, and make them interchangeable. The Strategy Pattern is also known as Policy.

Advantages:

  • It is designed to provide a substitute for sub-classes.

Example: we are going to consider a simple example of a calculator. We need to perform five different operations. We shall encapsulate all those five operations into Calculator class and execute the program using the Strategy Design Pattern.

Design-Patterns-in-Java-Edureka

UML Diagram:

strategy//Calculator


package StrategyDesignPattern;

public interface Calculator {
       public float calculation(float a, float b);
}

//Add


package StrategyDesignPattern;

public class add implements Calculator{
       public float calculation(float a, float b) {
              return a+b;
       }
}

//Sub


package StrategyDesignPattern;

public class sub implements Calculator{
       public float calculation(float a, float b) {
             return a-b;
      }
}

//Mul


package StrategyDesignPattern;

public class mul implements Calculator{
       public float calculation(float a, float b) {
             return a*b;
       }
}

//Div


package StrategyDesignPattern;

public class div implements Calculator{
      public float calculation(float a, float b) {
            return a/b;
      }
}

//Mod


package StrategyDesignPattern;

public class mod implements Calculator{
        public float calculation(float a, float b) {
              return a%b;
        }
}

//Query


package StrategyDesignPattern;

public class Query {
      private Calculator calci;
      public Query(Calculator calci){
               this.calci = calci;
      }
      public float executeStrategy(float num1, float num2){
               return calci.calculation(num1, num2);
      }
}

//Strategy


package StrategyDesignPattern;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Strategy {
         public static void main(String[] args) throws NumberFormatException, IOException {
                  BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
                  System.out.print("Enter the first value: ");
                  float value1=Float.parseFloat(br.readLine());
                  System.out.print("Enter the second value: ");
                  float value2=Float.parseFloat(br.readLine());
                  Query query = new Query(new add());
                  System.out.println("Addition = " + query.executeStrategy(value1, value2));
                  query = new Query(new sub());
                  System.out.println("Subtraction = " + query.executeStrategy(value1, value2));
                  query = new Query(new mul());
                  System.out.println("Multiplication = " + query.executeStrategy(value1, value2));
                  query = new Query(new div());
                  System.out.println("Multiplication = " + query.executeStrategy(value1, value2));
                  query = new Query(new mod());
                  System.out.println("Multiplication = " + query.executeStrategy(value1, value2));
        }
}

Output:

Enter the first value: 10
Enter the second value: 2
Addition = 12.0
Subtraction = 8.0
Multiplication = 20.0
Multiplication = 5.0
Multiplication = 0.0

 

Command Design Pattern: It encapsulates a request under an object as a command and passes it to the invoker object. Invoker object looks for the appropriate object which can handle this command and pass the command to the corresponding object and that object executes the command. It is also known as Action or Transaction.

Advantages:

  • It separates the object that invokes the operation with the object that actually performs the operation.

Example: This example demonstrates a simple command execution cycle where the user requires to exhibit switching on and off the various electronic devices in his houses like a bulb and stereo player. He invokes the command through an invoker object called a simple remote control.

Design-Patterns-in-Java-Edureka

UML Diagram:

command//Command


package CommandDesignPattern;

interface Command {
       public void execute();
}

//Light


package CommandDesignPattern;

class Light {
       public void on() {
             System.out.println("Light is on");
       }
public void off() {
       System.out.println("Light is off");
       }
}

//LightOnCommand


package CommandDesignPattern;

class LightOnCommand implements Command {
         Light light;
         public LightOnCommand(Light light) {
                 this.light = light;
         }
         public void execute() {
                 light.on();
         }
}

//LightOffCommand


package CommandDesignPattern;

class LightOffCommand implements Command {
       Light light;
       public LightOffCommand(Light light) {
              this.light = light;
       }
       public void execute() {
              light.off();
       }
}

//Stereo


package CommandDesignPattern;

class Stereo {
       public void on() {
              System.out.println("Stereo is on");
       }
       public void off() {
              System.out.println("Stereo is off");
       }
       public void setCD() {
              System.out.println("Stereo is set " + "for CD input");
       }
       public void setDVD() {
             System.out.println("Stereo is set" + " for DVD input");
       }
       public void setRadio() {
             System.out.println("Stereo is set" + " for Radio");
       }
       public void setVolume(int volume) {
             System.out.println("Stereo volume set" + " to " + volume);
       }
}

//StereoOffCommand


package CommandDesignPattern;

class StereoOffCommand implements Command {
      Stereo stereo;
      public StereoOffCommand(Stereo stereo) {
             this.stereo = stereo;
      }
      public void execute() {
             stereo.off();
      }
}

//StereoOnWithCDCommand


package CommandDesignPattern;

class StereoOnWithCDCommand implements Command {
         Stereo stereo;
         public StereoOnWithCDCommand(Stereo stereo) {
               this.stereo = stereo;
         }
         public void execute() {
               stereo.on();
               stereo.setCD();
               stereo.setVolume(11);
         }
}

//SimpleRemoteControl


package CommandDesignPattern;

class SimpleRemoteControl {
       Command slot;
       public SimpleRemoteControl() {
       }
       public void setCommand(Command command) {
               slot = command;
       }
       public void buttonWasPressed() {
               slot.execute();
       }
}

//RemoteControlTest


package CommandDesignPattern;

class RemoteControlTest {
        public static void main(String[] args) {
               SimpleRemoteControl remote = new SimpleRemoteControl();
               Light light = new Light();
               Stereo stereo = new Stereo();
               remote.setCommand(new LightOnCommand(light));
               remote.buttonWasPressed();
               remote.setCommand(new StereoOnWithCDCommand(stereo));
               remote.buttonWasPressed();
               remote.setCommand(new StereoOffCommand(stereo));
               remote.buttonWasPressed();
       }
}

Output:

Light is on
Stereo is on
Stereo is set for CD input
Stereo volume set to 11
Stereo is off

 

Observer Design Pattern: It defines a one-to-one dependency so that when one object changes state, all its dependents are notified and updated automatically. The Memento pattern is also known as Dependents or Publish-Subscribe.

Advantages:

  • It illustrates the coupling between the objects and the observer.
  • It provides support for broadcast-type communication.

Example: We are going to execute a program using the Observer Design Pattern to display the current average score and the current predictable score of a cricket match.

Design-Patterns-in-Java

UML Diagram:

observer//Cricket Data


package ObserverDesignPattern;

class CricketData{
       int runs, wickets;
       float overs;
       CurrentScoreDisplay currentScoreDisplay;
       AverageScoreDisplay averageScoreDisplay;
       public CricketData(CurrentScoreDisplay currentScoreDisplay, AverageScoreDisplay averageScoreDisplay){
              this.currentScoreDisplay = currentScoreDisplay;
              this.averageScoreDisplay = averageScoreDisplay;
       }
       private int getLatestRuns(){
              return 90;
       }
       private int getLatestWickets(){
              return 2;
       }
       private float getLatestOvers(){
             return (float)10.2;
       }
       public void dataChanged(){
             runs = getLatestRuns();
             wickets = getLatestWickets();
             overs = getLatestOvers();
             currentScoreDisplay.update(runs,wickets,overs);
             averageScoreDisplay.update(runs,wickets,overs);
       }
}

//Average Score Display


package ObserverDesignPattern;

class AverageScoreDisplay{
        private float runRate;
        private int predictedScore;
        public void update(int runs, int wickets, float overs){
                this.runRate = (float)runs/overs;
                this.predictedScore = (int) (this.runRate * 50);
                display();
        }
        public void display(){
                System.out.println("nAverage Score Display:n" + "Average Run Rate: " + runRate + "nPredictable Score: " + predictedScore);
        }
}

//Current Score Display


package ObserverDesignPattern;

class CurrentScoreDisplay{
     private int runs, wickets;
     private float overs;
     public void update(int runs,int wickets,float overs){
            this.runs = runs;
            this.wickets = wickets;
            this.overs = overs;
            display();
     }
public void display(){
            System.out.println("nCurrent Score Display: n" + "Current Runs: " + runs +"nWickets Lost:" + wickets + "nOvers Played: " + overs );
     }
}

//Main


package ObserverDesignPattern;

class Main{
        public static void main(String args[]){
               AverageScoreDisplay averageScoreDisplay = new AverageScoreDisplay();
               CurrentScoreDisplay currentScoreDisplay = new CurrentScoreDisplay();
               CricketData cricketData = new CricketData(currentScoreDisplay, averageScoreDisplay);
               cricketData.dataChanged();
        }
}

Output:

Current Score Display:
Current Runs: 90
Wickets Lost:2
Overs Played: 10.2

Average Score Display:
Average Run Rate: 8.823529
Predictable Score: 441

 

 

JEE Design Patterns are concerned with providing solutions to the Java EE-based software applications and frameworks. These patterns are widely used in Spring. JEE Design Patterns are further classified as follows:

  1. MVC Design Pattern
  2. Dependency Injection Pattern
  3. DAO Design Pattern
  4. Business Delegate Pattern
  5. Intercepting Filter Pattern
  6. Service Locator Pattern
  7. Transfer Object Pattern

Let us discuss some important JEE Design Patterns practically.

 

MVC Design Pattern: MVC Design Pattern is defined as follows:

  • Models are basically objects used as blueprints for all of the objects that will be used in the application.

  • Views are used to represent the presentational aspect of the information and data located in the models.

  • Controllers control and act as both of the Models as well as Views. They serve as a connection between the Models and Views. Controllers are capable to instantiate, update and delete models. They load them with information and then send the data to the views to present to the end-user.

Advantages:

  • It support multiple views for a model. 

Example: We are going to use MVC Design Pattern to set and print the data of the school students.

Design-Patterns-in-Java-EdurekaUML Diagram:

Java-Design-Pattern-MVC-Edureka//MVC pattern


package MVCDesignPattern;

public class MVCPattern {
public static void main(String[] args){
              Student model = retriveStudentFromDatabase();
              StudentView view = new StudentView();
              StudentController controller = new StudentController(model, view);
              controller.updateView();
              controller.setStudentName("Sandeep Shukla");
              controller.updateView();
       }
       private static Student retriveStudentFromDatabase(){
              Student student = new Student();
              student.setName("Karan Kumar");
              student.setRollNo("21CSE987");
              return student;
       }
}

//Student


package MVCDesignPattern;

public class Student {
      private String rollNo;
      private String name;
      public String getRollNo(){
            return rollNo;
      }
      public void setRollNo(String rollNo){
            this.rollNo = rollNo;
      }
      public String getName(){
            return name;
      }
      public void setName(String name){
           this.name = name;
      }
}

//StudentController

package MVCDesignPattern;

public class StudentController {
       private Student model;
       private StudentView view;
       public StudentController(Student model, StudentView view){
              this.model = model;
              this.view = view;
       }
       public void setStudentName(String name){
              model.setName(name);
       }
       public String getStudentName(){
              return model.getName();
       }
       public void setStudentRollNo(String rollNo){
             model.setRollNo(rollNo);
       }
       public String getStudentRollNo(){
             return model.getRollNo();
       }
       public void updateView(){
             view.printStudentDetails(model.getName(), model.getRollNo());
       }
}

//StudentView


package MVCDesignPattern;

public class StudentView {
        public void printStudentDetails(String studentName, String studentRollNo){
               System.out.println("Student: ");
               System.out.println("Name: " + studentName);
               System.out.println("Roll No: " + studentRollNo);
        }
}

Output:

Student:
Name: Karan Kumar
Roll No: 21CSE987
Student:
Name: Sandeep Shukla
Roll No: 21CSE987

 

DAO Design Pattern: DAO is a pattern in which objects are dedicated to the communication with the Data Layer. These objects instantiate “SessionFactories” and handle all of the logic behind communicating with the database.

Advantages:

  • It uses common calls to retrieve objects.

Example: We are going display the Developer details of a certain IT company using the DAO Design Pattern.

Design-Patterns-in-Java-EdurekaUML Diagram:

Java-Design-Pattern-DAO-Edureka//Developer


package Dao;

import java.util.List;
import java.util.ArrayList;
import java.util.List;

class Developer{
       private String name;
       private int DeveloperId;
       Developer(String name, int DeveloperId){
               this.name = name;
               this.DeveloperId = DeveloperId;
       }
       public String getName(){
               return name;
       }
       public void setName(String name){
               this.name = name;
       }
       public int getDeveloperId(){
               return DeveloperId;
       }
       public void setDeveloperId(int DeveloperId){
               this.DeveloperId = DeveloperId;
       }
}

//DeveloperDAO


package Dao;

interface DeveloperDao{
          public List<Developer> getAllDevelopers();
          public Developer getDeveloper(int DeveloperId);
          public void updateDeveloper(Developer Developer);
          public void deleteDeveloper(Developer Developer);
}

//DeveloperDaoImpl


package Dao;

class DeveloperDaoImpl implements DeveloperDao{
       List<Developer> Developers;
       public DeveloperDaoImpl(){
              Developers = new ArrayList<Developer>();
              Developer Developer1 = new Developer("Kiran",101011);
              Developer Developer2 = new Developer("Vikrant",124122);
              Developers.add(Developer1);
              Developers.add(Developer2);
        }
        public void deleteDeveloper(Developer Developer){
              Developers.remove(Developer.getDeveloperId());
              System.out.println("DeveloperId " + Developer.getDeveloperId() + ", deleted from database");
        }
        public List<Developer> getAllDevelopers(){
              return Developers;
        }
        public Developer getDeveloper(int DeveloperId){
              return Developers.get(DeveloperId);
        }
        public void updateDeveloper(Developer Developer){
              Developers.get(Developer.getDeveloperId()).setName(Developer.getName());
              System.out.println("DeveloperId " + Developer.getDeveloperId() + ", updated in the database");
        }
}

//DaoPatternDemo


package Dao;

class DaoPatternDemo{
          public static void main(String[] args){
                 DeveloperDao DeveloperDao = new DeveloperDaoImpl();
                 for (Developer Developer : DeveloperDao.getAllDevelopers()){
                       System.out.println("DeveloperId : " + Developer.getDeveloperId() + ", Name : " + Developer.getName());
                 }
                 Developer Developer =DeveloperDao.getAllDevelopers().get(0);
                 Developer.setName("Lokesh");
                 DeveloperDao.updateDeveloper(Developer);
                 DeveloperDao.getDeveloper(0);
                 System.out.println("DeveloperId : " + Developer.getDeveloperId() + ", Name : " + Developer.getName());
          }
}

Output:

DeveloperId : 0, Name : Kushagra
DeveloperId : 1, Name : Vikram
DeveloperId 0, updated in the database
DeveloperId : 0, Name : Lokesh

 

Overview of Design Patterns

  • Java Design Patterns are designed to be reusable in multiple projects.
  • They furnish solutions that help to define system architecture with ease.
  • Design Patterns are capable to capture the software engineering experiences.
  • They provide transparency to the design of an application.
  • Design Patterns are provided by Expert Developers which prove that these are testified and well-proven solutions.
  • Java Design Patterns provide clarity to the System Architecture and provide the possibility of building a better system.

With this, we come to an end of this article. I hope you have understood the Java Design Patterns, their types, importance and their implementation through some real-time examples.

Now that you have understood the basics of Java Design Patterns, check out the Java Online Training by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe. Edureka’s Java J2EE and SOA training and certification course is designed for students and professionals who want to be a Java Developer. The course is designed to give you a head start into Java programming and train you for both core and advanced Java concepts along with various Java frameworks like Hibernate & Spring.

Got a question for us? Mention it in the comments section of this “Java Design Patterns” blog and we will get back to you as soon as possible.

Upcoming Batches For Java Course Online
Course NameDateDetails
Java Course Online

Class Starts on 21st December,2024

21st December

SAT&SUN (Weekend Batch)
View Details
Comments
0 Comments

Join the discussion

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.

image not found!
image not found!

Important Java Design Patterns You Need to Know About

edureka.co