3 minutes read
The structural design patterns are ways to solve common problems related to the structure of your objects. Scaling an object or handling communication between objects may lead to complex object structures, which can affect the quality of your code. You can organize these complex structures using this category of patterns, which will simplify the relationship between objects.
Types of Structural Patterns
The following is a list of structural design patterns:
- Adapter Pattern
- Bridge Pattern
- Filter Pattern
- Composite Pattern
- Decorator Pattern
- Facade Pattern
- Flyweight Pattern
- Proxy Pattern
The Adapter Pattern
This pattern focuses on the idea of adapting an interface according to the client applcation’s needs. Think about having an HDMI to USB adapter. The same principle applies to this pattern as well, by adapting the client application request to the specification of a target object. This can still be a bit to abstract, so let’s go a step further.
One of the use cases for this design pattern is for legacy software. Let’s imagine we are developing software in a bank. Usually, banks have lots of legacy information systems, which are very stable and too expensive to change. Those systems went through lots of iterations where improvements and bug fixing were performed, making them very robust. As you may not have the budget to refactor an entire banking system with all its components, let’s suppose you want to get rid of the old system for creating bank accounts, therefore you start developing a new, more modern web service in Java for this purpose.
But how can you accept requests from the legacy app for creating the accounts? One solution would be to use the adapter pattern. According to the pattern, we would create an interface, called Account, for example. Inside, we code a createAccount() method. Another object, the AccountAdapter object, will implement the interface and will provide an implementation for the createAccount() method as well. Within this method, we will then call our new service.
So the final diagram would look like this:

Another typical use case for adapter pattern is when you want to convert an interface into another. The real-life example of converting HDMI or ethernet interface to USB is a good example in the hardware world. In software, we can think of the communication of two interfaces, and adapting one to another would simplify the cmmunication between them.
In the Java API, we have two powerful examples of adapter pattern usage:
- Arrays.asList() – this will take a generic parameter of type T and will convert it (adapt it) to a list.
- InputStreamReader(InputStream) and OutputStreamWriter(OutputStream) will convert the respective streams into Reader/Writer.
Code Example
Let’s see a real-world adapter pattern usage example in the Arrays.asList() method.
public class AdapterPatternDemo {
public static void main(String[] args) {
Integer[] integerArr = new Integer[]{0, 1, 2, 3, 4};
System.out.println(Arrays.asList(integerArr));
}
}
In the code above, we have a simple class with a main method. Inside the main method, we instantiate an integer array. Next, we call the Arrays.asList method, and we supply our newly-created integer array. We can also include the adaptation in a System.out.println() to see the contents of the new list.
Running this code will output the following: [0, 1, 2, 3, 4], which is the same content as inside our integerArr object.
Potential Issues?
As cool as it seem to be, there are also some pitfalls related to this design pattern. For example, it’s easy to overly-complicate things, by creating a huge adapter, which might be hard to maintain.
Also, it is a common mistake that developers add new code to the legacy application, in order to make it compatible with the adapter. Don’t do this! Instead, make the necessary adjustments in the adapter itself, as this is the reason why you use this pattern in the first place.
Conclusion
If you are interested to learn more about adapter pattern, and about design patterns in general, I have published this course on Udemy. You can see there all the known design patterns described in detail with relevant, real-world examples.
What do you think about the adapter pattern? Looking forward to reading your opinion in the comment section.