×
iFour Logo

A Complete Guide on Structural Design Pattern in Java

Yash Shukla - July 21, 2021

Listening is fun too.

Straighten your back and cherish with coffee - PLAY !

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.”);
  }
  }

Searching for Reliable JAVA Development Company ? Your Search ends here.

// 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 ? Your Search ends here. See here // 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.

Author Bio

User Image

Author Name : Yash Shukla

 

Yash Shukla is an adroit Java professional. With technical acumen and a sharp mindset, he always focuses on learning opportunities to tune his growth. He believes in self-development and periodically shares his insights on various platforms.

 

Categories

Ensure your sustainable growth with our team

Talk to our experts
Sustainable
Sustainable
 

Blog Our insights

How Blockchain can impact the Financial industry in 2023?
How Blockchain can impact the Financial industry in 2023?

Table of Content 1.Boosts payment transparency, trust, and efficiency 2.Reduces operational risks and enables real-time verification 3.Eliminates central authority to...

Blockchain in the Legal sector: Redefining the business in 12 ways
Blockchain in the Legal sector: Redefining the business in 12 ways

Table of Content 1.Greater efficiency, better transparency, and cost savings 2.Attorneys can digitally sign and store law agreements 3.Blockchain helps with smart...

Blockchain in Construction: How will it change the Industry in 2022?
Blockchain in Construction: How will it change the Industry in 2022?

Table of Content 1.Blockchain keeps everyone on the same page 2.Secure, transparent, and tamper-proof record keeping 3.Retaliatory actions in accordance with agreed-upon...