JIYIK CN >

Current Location:Home > Learning > ALGORITHM >

Adapter vs Decorator vs Facade vs Proxy Design Pattern in Java

Author:JIYIK Last Updated:2025/03/19 Views:

There are some striking similarities between the Adapter, Decorator, Facade, and Proxy design patterns as they all use composition and delegation to solve problems. The Adapter pattern wraps an interface and delegates calls to it. Decorator wraps an object and implements behavior on it, Facade wraps one or more interfaces to provide an easy to use central interface, and the Proxy pattern also wraps a Subject and delegates calls to it. So the question is, why are there different patterns? What is the difference between the Adapter, Decorator, Facade, or Proxy patterns if their structure is the same. The answer is 目的. Yes, all of these Java design patterns have similar structures and class diagrams, but their purposes are completely different.

  • The main purpose of the Adapter pattern is to convert interfaces. The Adapter pattern lets two components work together that would not work because of incompatible interfaces.
  • The Decorator pattern adds new functionality at runtime. It allows us to enrich an object even after it is created.
  • The facade design pattern neither converts the interface nor adds new functions, but only provides a simpler interface. Therefore, instead of directly accessing the various components of the system, the client uses the facade. The facade design pattern allows the client to interact with a complex system with a simpler interface and less work. The facade will then call the various components.
  • The proxy pattern is also very similar to the adapter and decorator, but its purpose is to control access to an object. The proxy prevents the client from accessing the object directly, but instead acts as a real object that can provide alternative behavior or forward requests to the original object.

Proxy is the most general pattern among all these patterns and it can be used in different ways like remote proxy for communicating with remote objects, virtual proxy for controlling access to expensive objects, protection proxy for providing access to objects based on role , caching proxy which can return cached objects etc.


Similarities between Adapter, Facade, Proxy and Decorator patterns in Java

One of the main similarities is the structure, all these patterns wrap an object and delegate request processing or method calls to them. There is something else here, which is the commonality between all these patterns.

  1. In classic design patterns, they are all defined as structural patterns, such as GOF design patterns.
  1. They all use composition and delegation to achieve their intentions. Adapter uses composition to forward calls from target interface to adaptee interface, Decorator also uses the same technique before adding new behavior, Facade is composed of all subcomponents, Proxy also uses composition and delegation to forward requests.
  1. Both Decorator and Proxy patterns are meant to stay in place of the original object, that is why both Decorator and Proxy patterns implement the interface of the real object. Due to this nature, decorators and proxies can be passed to methods that accept the original or real object.
  1. Adapter and Facade patterns forward the request to a different interface which can be an adapter or any component interface from the subsystem.
  1. Another similarity between the Adapter and Facade patterns is that they can wrap multiple interfaces, which is also different from the Decorator and Proxy patterns because they tend to operate on one object.

Difference between Adapter vs Decorator vs Proxy vs Facade design pattern in Java

Now that we know there are a lot of similarities between all of these structural patterns, let's look at some of the differences between them. As I said before, they differ in their intent, the problems they solve, and where they are used.

  1. The Adapter pattern converts interfaces, the Decorator pattern does not convert interfaces, it just implements the interface of the original object so that it can be passed to a method that accepts the original object. The Facade and Proxy patterns also do not convert interfaces.
  1. One of the main differences between the Decorator and Proxy patterns is that a Decorator never creates an object, it always adds new functionality on an already existing object, a Proxy on the other hand can create an object that doesn't exist. It can stand in for a real object until it's ready and then start forwarding requests to it.
  1. The decorator design pattern also allows adding multiple functionalities and can do so in an ordered manner by chaining multiple decorators , whereas the proxy pattern does not suggest proxy chaining.
  1. If we compare Decorator and Facade patterns then you can see that unlike Decorator, Facade does not add any new behavior, it simply calls an existing method from the interface, which it provides as a facade.
  1. Unlike the Decorator, Proxy, and Adapter design patterns, the Facade does not need to implement any specific interface. In fact, the Facade can even be a class that simply holds the various subsystem components and provides simpler operations required by the client, and then calls the corresponding methods on the subsystem.

For example, we can think of Car as a facade that provides the start and stop start()methods stop(). When the client calls start()the method, it may start various subsystems by calling corresponding methods, such as engine.start(), wheels.move(), lights.on(), ac.on()and so on.


UML diagrams for Adapter, Decorator, Proxy, and Facade patterns

The similarities we have been talking about are even more evident in their structure. If you look at their UML diagrams, you can clearly see the similarities.

UML diagram of the adapter pattern

Java Adapter Pattern UML Diagram

UML diagram of the decorator pattern

UML diagram of decorator pattern in Java

UML diagram of the proxy pattern

UML diagram of proxy pattern in Java

UML diagram of the Facade pattern

Java facade pattern UML diagram

That’s the difference between Adapter , Decorator , Proxy , and Facade design patterns in Java . Knowing the similarities and differences between them will improve your knowledge and ability to spot the use of design patterns. In short, if you need to convert an interface, use the Adapter design pattern to make the two parties work together.

If you need to hide the real object due to various reasons, then you should use the Proxy design pattern in Java, such as security, performance, networking, etc. Use the Decorator pattern to add new behaviors on the existing objects at runtime, it provides the flexibility to mix behaviors and apply them in different orders as per the client requirements.

Finally, the facade pattern is used to provide clients with simplified access to complex systems. The facade provides a higher level of abstraction and should contain the operations required by the client.

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Java Decorator Design Pattern Example

Publish Date:2025/03/19 Views:115 Category:ALGORITHM

Hello everyone, if you want to learn about Decorator Design Pattern in Java , then you have come to the right place. As design patterns are very important while building software and equally important in any core Java interview, it is alway

Observer Design Pattern in Java

Publish Date:2025/03/19 Views:70 Category:ALGORITHM

Observer design pattern in Java is a basic core Java pattern where the Observer monitors any changes in the state or properties of the Subject . For example, a company updates all shareholders about any decision taken by them here the compa

How to use Composite Design Pattern in Java?

Publish Date:2025/03/19 Views:179 Category:ALGORITHM

Hello Java programmers, if you want to learn about Composite Design Pattern in Java , such as how to implement Composite Design Pattern and when to use it, then you have come to the right place. In this article, we will discuss Composite De

How to find the IP address of the local host in Java

Publish Date:2025/03/17 Views:98 Category:NETWORK

The Java Networking API provides a way to find the local host IP address from a Java program using the java.net InetAddress class. It is rare that you need the local host IP address in a Java program. Most of the time, I use the Unix command to find t

Why do you need to bind event handlers in React Class Components?

Publish Date:2025/03/16 Views:58 Category:React

When using React, we must have come across control components and event handlers. We need to use `.bind()` in the constructor of the custom component to bind these methods to the component instance. As shown in the following code:

Do you understand JavaScript closures?

Publish Date:2025/02/21 Views:111 Category:JavaScript

The function of a closure can be inferred from its name, suggesting that it is related to the concept of scope. A closure itself is a core concept in JavaScript, and being a core concept, it is naturally also a difficult one.

Do you know about the hidden traps in variables in JavaScript?

Publish Date:2025/02/21 Views:183 Category:JavaScript

Whether you're just starting to learn JavaScript or have been using it for a long time, I believe you'll encounter some traps related to JavaScript variable scope. The goal is to identify these traps before you fall into them, in order to av

How much do you know about the Prototype Chain?

Publish Date:2025/02/21 Views:156 Category:JavaScript

The prototype chain can be considered one of the core features of JavaScript, and certainly one of its more challenging aspects. If you've learned other object-oriented programming languages, you may find it somewhat confusing when you start

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial