Article From:


Object adapter mode

Like the class adapter pattern, the object’s adapter pattern converts the adapted class’s API into the target class’s API. Unlike the class’s adapter pattern, the object’s adapter pattern connects to the Adaptee class not by inheritance but by delegation.. The static structure of the adapter mode of the object is shown in the following figure.


As you can see from the above diagram, the Adaptee class does not have a simpleOperation2 () method, which the client expects. To enable the client to use the Adaptee class, we need to provide a package (Wrapper) class Adapter. This bagThe wrapper class wraps an instance of the Adaptee so that the wrapper class can connect the API of the Adaptee to the API of the Target class. Adapter is a delegate relationship with Adaptee, which determines that the adapter pattern is object.

As can be seen from the above figure, the role of the model is as follows:

  • Target (Target) role: This is the desired interface, and the target can be concrete or abstract classes.
  • Source (Adaptee) role: existing interfaces that need to be adaptable.
  • Adapter (Adapter) role: adapter class is the core of this pattern. The adapter converts the source interface to the target interface. Obviously, this role must be a specific class.

Object adapter pattern code

public interface Target {
     * This is a source class method, simpleOperation1.* * /void simpleOperation1();

     * This is the source method simpleOperation2.* * /void simpleOperation2();

The above is the source code of the target role, which is implemented in the form of a Java interface. As you can see, this interface declares two methods: simpleOperation1 () and simpleOperation2 (). Source role AdapaTee is a concrete class that has a simpleOperation1 () method, but no simpleOperation2 () method, as shown in the listing below.

public class Adaptee {
     * Source class contains method simpleOperation1* * /public void simpleOperation1(){};

The source code of the adapter class is shown in the code list below.

public class Adapter implements Target {

    private Adaptee adaptee;

    public Adapter(Adaptee adaptee) {
        this.adaptee = adaptee;

     * Source class has method simpleOperation1* therefore, the adapter class can be delegated directly.* * /@Override
    public void simpleOperation1() {

     * Source class has no method simpleOperation2* so the adapter class supplements this method.* * /@Override
    public void simpleOperation2() {
        //write you code here

Effect of object adapter mode

  • An adapter can adapt a variety of sources to the same target. In other words, the same adapter can adapt the source class and its subclasses to the target interface.
  • Compared with the class adapter mode, it is not easy to replace the source class method. If you have to replace one or more methods of the source class, you have to make a subclass of the source class, replace the methods of the source class, and then adapt the subclass of the source class as the real source.
  • Although it’s not easy to replace methods of source classes, it’s convenient to add some new methods, and the new methods can be applied to all sources at the same time.


The adapter pattern transforms the existing interfaces into the interfaces expected by the client classes and realizes the reuse of the existing classes. It is a design pattern with very high usage frequency and is widely used in software development. It is used in open source frameworks such as Spring and driver design (such as database driver in JDBC).Adapter mode is also used.

** 1. Main advantages * *

Both the object adapter mode and the class adapter pattern have the following advantages:

  • Decoupling the target class from the adapter class,By introducing an adapter class to reuse the existing adapter class, there is no need to modify the original structure.

  • Increased transparency and reusability of classes.,Encapsulating the specific business implementation process in the adapter class is transparent to the client class and improves the reusability of the adapter. The same adapter class can be reused in many different systems.

  • Flexibility and expansibility are excellent.,By using configuration files, adapters can be easily replaced, or new adapter classes can be added without modifying the original code, in full compliance with the “open-close principle”.

Specifically, the class adapter mode has the following advantages:

  • Because adapter classes are subclasses of adapter classes,You can replace some adapters in the adapter class.,Makes the adapter more flexible.

The object adapter pattern also has the following advantages:

  • An object adapterIt is possible to adapt multiple adaptor to the same target.

  • A subclass of adaptor can be adapted.,Because the adapter is related to the adapter, subclasses of the adapter can also be adapted through the adapter, according to the “Rieselberg substitution principle”.

** 2. Main disadvantages * *

Class adapter modeThe disadvantages are as follows:

  • For Java, C# and other languages that do not support multiple class inheritance, only one adapter class can be adapted at most.Multiple adaptor can not be adapted at the same time.

  • The adapter class cannot be the final class.,If you can’t be a final class in Java, C# can’t be a sealed class.
  • In Java, C# and other languages,The target abstract class in the class adapter mode can only be an interface, but not a class.,Its use has certain limitations.

Object Adapter PatternThe disadvantages are as follows:

  • Compared with the class adapter mode,Some ways to replace adapter classes in adapters are more troublesome.。If you must replace one or more methods of the adapter class, you can first make a subclass of the adapter class, replace the methods of the adapter class, and then adapt the subclass of the adapter class as a real adapter. The implementation process is more complex.

** 3. Applicable scenario * *

The adapter mode can be considered in the following cases:

  • The system needs to use some existing classes, and the interfaces of these classes (such as method names) do not meet the needs of the system, or even do not have the source code for these classes.
  • You want to create a reusable class that works with classes that are not too closely related to each other, including classes that may be introduced in the future.
  • (In design, you need to change the interface of multiple existing subclasses, and if you use the class’s adapter pattern, you need to make an adapter class for each subclass, which is not practical.
Link of this Article: Object adapter for Java

Leave a Reply

Your email address will not be published. Required fields are marked *