×

iFour Logo

A Complete Guide on Structural Design Pattern in Java

Kapil Panchal - July 21, 2021

Listening is fun too.

Straighten your back and cherish with coffee - PLAY !

  • play
  • pause
  • pause
A Complete Guide on Structural Design Pattern in Java

Table of Content

In this blog, first, we’ll have some overview about Structural Design Pattern, why we use it, its types and we’ll understand about adapter pattern which is a type of structural design pattern. Let’s try to make things easier to understand with an example.

What is a Structural Design Pattern?


A Structural design pattern is a type of design pattern which we generally use in the case of large project structures where there are many different types of classes and various objects.

Structural design pattern deals with how different classes work together in a cohesive manner. One class uses properties of difference classes and vice-a- versa by using the OOPs i.e. Object-Oriented Programming Language concepts like inheritance or polymorphism.

It makes it easier for us to understand that what is the relationship of different classes related to one another.

What are the types of Structural Design Pattern?


There are 7 types of structural design patterns which are as below.

  1. Adapter Pattern: As the name suggests “adapter”, adapter pattern is used when we need to adapt to the client’s requirements. We’ll learn about adapter pattern more further in this blog.
  2. Bridge Pattern: We use bridge pattern when we want to separate our implementations by using abstraction. To do this we use another interface. So the other interface we created will act as a bridge between all other interfaces.
  3. Composite Pattern Composite design pattern is used when we want to use a group of objects as one single object.
  4. Decorator Pattern: A Decorator pattern is used when we might make some changes to an existing object without making any major change in the structure.
  5. Facade Pattern: A Facade pattern is used when we want to hide the complex structure of the code by wrapping it into another simple interface. So that the client can use only a simple structure.
  6. Flyweight Pattern: Flyweight pattern is used when we want to minimize the number of objects used in the structure. This improves memory utilization and improves performance.
  7. Proxy Pattern: Proxy pattern is used when we want to use an original object by wrapping it into another (i.e. proxy of an object).

Now let us focus on the Adapter Pattern.

What is an Adapter Pattern?


As from the above description, we had a brief idea about the adapter pattern that it is used when we require to adapt to our client’s requirements.

So we convert one interface into another as the client wanted it to be.

Due to this, the adapter pattern is also known as wrapper pattern because it wraps the structure interface into another which would be easier for the customer to use.

Advantages of Adapter Pattern


We can reuse our code or other functionality according to requirement.

We can have two different objects interact with each other.

UML Class diagram for the example


UMLClassdiagram

We can have two different objects interact with each other.

Each entity has its own attributes and methods like for credit cards it is bank details and card details. For bank details, attributes are bank name, account holder name, and methods like set bank name, get bank name, set account holder’s name, get account holder’s name, get account number, etc. For Banks, customer methods are to get bank details and get a credit card.

As we can see above in the diagram Bank details will extend bank customers and implement a credit card interface.

Specifications for the Adapter Pattern


The main Interface - So this is the interface that the client will actually use.

The client - The actual request will come from here and interact with the adapter class.

The adapter class - This class is a wrapper class it will implement the interface and modify the specifications according to needs. It will send the required specifications after modification according to the adapter class.

The adapter class - This is the class that will interact with the adapter class to use the functionality from it. It will tell the adapter class what kind of specification is needed.


Example:

// Interface Credit Card

public interface CreditCard
 {  
    public void giveBankDetails();  
    public String getCreditCard();  
}

//Class Bank Details

  public class BankDetails
{  
    private String bankName;  
private String accHolderName;  
    private long accNumber;  
      
public String getBankName() 
{  
        return bankName;  
    }  
public void setBankName(String bankName)
 {  
        this.bankName = bankName;  
    }  
public String getAccHolderName() 
{  
        return accHolderName;  
    }  
public void setAccHolderName(String accHolderName)
 {  
        this.accHolderName= accHolderName;  
    }  
public long getAccNumber()
 {  
        return accNumber;  
    }  
public void setAccNumber(long accNumber)
 {  
        this.accNumber = accNumber;
}
}

//Class Bank Details

import java.io.BufferedReader;  
  import java.io.InputStreamReader;  
  public class BankCustomer extends BankDetails implements CreditCard 
  {  
   public void giveBankDetails()
  {  
    Try {  
     BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  System.out.println(“Enter the account holder name :");  
     String customername = br.readLine();  
     System.out.print("\n");  
        
     System.out.print("Enter the account number:");  
     long accno = Long.parseLong(br.readLine());  
     System.out.print("\n");  
     System.out.print("Enter the bank name :");  
     String bankname = br.readLine();   
     setAccHolderName(customername);  
     setAccNumber(accno);  
  }
  catch(Exception e)
  {  
          e.printStackTrace();  
     }  
    @Override  
    public String getCreditCard() {  
     long accno=getAccNumber();  
     String accholdername=getAccHolderName();  
     String bname=getBankName();  
            
     return ("The Account number "+accno+" of "+accholdername+" in "+bname+ "  
                          bank is valid and authenticated for issuing the credit card.”);
  }
  }

// Main class

        Public class AdapterPatternDemo 
{  
 public static void main(String args[])
{  
  CreditCard targetInterface=new BankCustomer();  
  targetInterface.giveBankDetails();  
  System.out.print(targetInterface.getCreditCard());
}
}
      
Output

Enter the account holder name: Raj Khurana

Enter the account number: 100401580002

Enter the bank name: State Bank of India

The Account number 100401580002 of Raj Khurana in State Bank of India bank is valid and authenticated for issuing the credit card.

Conclusion


In this blog, we learned various design patterns provided by Java and also in other languages which follow OOPC (Object Oriented Programming Language) concepts. We majorly focused on structural design patterns and in particular the adapter pattern.

A Complete Guide on Structural Design Pattern in Java Table of Content 1. What is a Structural Design Pattern? 2. What are the types of Structural Design Pattern? 3. What is an Adapter Pattern? 4. Advantages of Adapter Pattern 5. UML Class diagram for the example 6. Specifications for the Adapter Pattern 7. Conclusion In this blog, first, we’ll have some overview about Structural Design Pattern, why we use it, its types and we’ll understand about adapter pattern which is a type of structural design pattern. Let’s try to make things easier to understand with an example. What is a Structural Design Pattern? A Structural design pattern is a type of design pattern which we generally use in the case of large project structures where there are many different types of classes and various objects. Structural design pattern deals with how different classes work together in a cohesive manner. One class uses properties of difference classes and vice-a- versa by using the OOPs i.e. Object-Oriented Programming Language concepts like inheritance or polymorphism. It makes it easier for us to understand that what is the relationship of different classes related to one another. What are the types of Structural Design Pattern? There are 7 types of structural design patterns which are as below. Adapter Pattern: As the name suggests “adapter”, adapter pattern is used when we need to adapt to the client’s requirements. We’ll learn about adapter pattern more further in this blog. Bridge Pattern: We use bridge pattern when we want to separate our implementations by using abstraction. To do this we use another interface. So the other interface we created will act as a bridge between all other interfaces. Composite Pattern Composite design pattern is used when we want to use a group of objects as one single object. Decorator Pattern: A Decorator pattern is used when we might make some changes to an existing object without making any major change in the structure. Facade Pattern: A Facade pattern is used when we want to hide the complex structure of the code by wrapping it into another simple interface. So that the client can use only a simple structure. Flyweight Pattern: Flyweight pattern is used when we want to minimize the number of objects used in the structure. This improves memory utilization and improves performance. Proxy Pattern: Proxy pattern is used when we want to use an original object by wrapping it into another (i.e. proxy of an object). Now let us focus on the Adapter Pattern. What is an Adapter Pattern? As from the above description, we had a brief idea about the adapter pattern that it is used when we require to adapt to our client’s requirements. So we convert one interface into another as the client wanted it to be. Due to this, the adapter pattern is also known as wrapper pattern because it wraps the structure interface into another which would be easier for the customer to use. Read More: A Complete Guide About Junit 5 Advantages of Adapter Pattern We can reuse our code or other functionality according to requirement. We can have two different objects interact with each other. UML Class diagram for the example We can have two different objects interact with each other. Each entity has its own attributes and methods like for credit cards it is bank details and card details. For bank details, attributes are bank name, account holder name, and methods like set bank name, get bank name, set account holder’s name, get account holder’s name, get account number, etc. For Banks, customer methods are to get bank details and get a credit card. As we can see above in the diagram Bank details will extend bank customers and implement a credit card interface. Specifications for the Adapter Pattern The main Interface - So this is the interface that the client will actually use. The client - The actual request will come from here and interact with the adapter class. The adapter class - This class is a wrapper class it will implement the interface and modify the specifications according to needs. It will send the required specifications after modification according to the adapter class. The adapter class - This is the class that will interact with the adapter class to use the functionality from it. It will tell the adapter class what kind of specification is needed. Example: // Interface Credit Card public interface CreditCard { public void giveBankDetails(); public String getCreditCard(); } //Class Bank Details public class BankDetails { private String bankName; private String accHolderName; private long accNumber; public String getBankName() { return bankName; } public void setBankName(String bankName) { this.bankName = bankName; } public String getAccHolderName() { return accHolderName; } public void setAccHolderName(String accHolderName) { this.accHolderName= accHolderName; } public long getAccNumber() { return accNumber; } public void setAccNumber(long accNumber) { this.accNumber = accNumber; } } //Class Bank Details import java.io.BufferedReader; import java.io.InputStreamReader; public class BankCustomer extends BankDetails implements CreditCard { public void giveBankDetails() { Try { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.println(“Enter the account holder name :"); String customername = br.readLine(); System.out.print("\n"); System.out.print("Enter the account number:"); long accno = Long.parseLong(br.readLine()); System.out.print("\n"); System.out.print("Enter the bank name :"); String bankname = br.readLine(); setAccHolderName(customername); setAccNumber(accno); } catch(Exception e) { e.printStackTrace(); } @Override public String getCreditCard() { long accno=getAccNumber(); String accholdername=getAccHolderName(); String bname=getBankName(); return ("The Account number "+accno+" of "+accholdername+" in "+bname+ " bank is valid and authenticated for issuing the credit card.”); } } Searching for Reliable JAVA Development Company ? CONTACT US // Main class Public class AdapterPatternDemo { public static void main(String args[]) { CreditCard targetInterface=new BankCustomer(); targetInterface.giveBankDetails(); System.out.print(targetInterface.getCreditCard()); } } Output Enter the account holder name: Raj Khurana Enter the account number: 100401580002 Enter the bank name: State Bank of India The Account number 100401580002 of Raj Khurana in State Bank of India bank is valid and authenticated for issuing the credit card. Conclusion In this blog, we learned various design patterns provided by Java and also in other languages which follow OOPC (Object Oriented Programming Language) concepts. We majorly focused on structural design patterns and in particular the adapter pattern.
Kapil Panchal

Kapil Panchal

A passionate Technical writer and an SEO freak working as a Content Development Manager at iFour Technolab, USA. With extensive experience in IT, Services, and Product sectors, I relish writing about technology and love sharing exceptional insights on various platforms. I believe in constant learning and am passionate about being better every day.

Build Your Agile Team

Enter your e-mail address Please enter valid e-mail

Categories

Ensure your sustainable growth with our team

Talk to our experts
Sustainable
Sustainable
 

Blog Our insights

Power Apps Solving 13 Legal Challenges
Power Apps Solving 13 Legal Challenges

There is a common misconception that bringing technology into the legal field won't be effective as this profession relies heavily on evidence and facts. However, if you look at it...

How to Automate Power BI Reports Using Power Automate
How to Automate Power BI Reports Using Power Automate

"Does Power Automate work with Power BI?”. If you think the same, you’re at the right place. Manual reporting plays an important role in business decision-making but requires more...

What’s New in .NET 9?
What’s New in .NET 9?

Today, we’re thrilled to present you with the first glimpse of .NET 9 release and let you know what features and updates you can anticipate in this new version. Various professionals believe that it’s the right time to explore and adopt the latest version of .NET for your upcoming projects. It is even recommended for projects built using .NET 6 or .NET 8, due to the framework updates made in this version.