• Home
  • LLMs
  • Docker
  • Kubernetes
  • Java
  • All
  • About
Design Patterns | Adapter
  1. References
  2. Example
    • The Adaptee
    • The Adapter
    • A simple class to test the Adapter design pattern

  1. References
    • Definition: (source: http://en.wikipedia.org/wiki/Adapter_pattern)

      An adapter helps two incompatible interfaces to work together.

    • Class diagram: (source: http://en.wikipedia.org/wiki/Adapter_pattern)

      Adapter Pattern
  2. Example
    • The Adaptee:

      public class CustomOperation {
          public int customAdd(final String a, final String b) {
              return Integer.valueOf(a) + Integer.valueOf(b);
          }
      }

    • The Adapter:

      public class Operation {
          private CustomOperation adaptee;
      
          Operation(final CustomOperation adaptee) {
              this.adaptee = adaptee;
          }
      
          public int add(final int a, final int b) {
              return adaptee.customAdd(String.valueOf(a), String.valueOf(b));
          }
      }

    • A simple class to test the Adapter design pattern:

      public class AdapterPatternTest {
          public static void main(String[] args) {
              final CustomOperation adaptee = new CustomOperation();
              final Operation adapter = new Operation(adaptee);
              System.out.println(adapter.add(2, 3));
          }
      }

© 2025  mtitek