Adapter Method example in JavaScript Design Patterns

Problem Statement:

Think about your electronic devices, like charging cables. You have an old charger (let’s call it “Old Plug”) with a big, round shape, and your new device (let’s call it “New Jack”) has a small, rectangular port.Now, you buy an adapter (Adapter in this case is a physical object, but it helps explain the concept). This adapter has a big round end that fits your old charger and a small rectangular end that fits your new device.

In this example:

  • Your old charger represents one system with its own way of doing things.
  • Your new device represents another system with its own way of doing things.
  • The adapter (the physical object) acts as the bridge between them, making the old charger work with the new device.

In JavaScript, the Adapter Pattern works similarly. It allows one piece of code to work with another piece of code, even if they have different interfaces, just like your adapter makes your old charger work with your new device.

Below is the implementation of the above example:

Javascript




// Existing system with an incompatible interface
class OldSystem {
  specificMethod() {
    console.log('Old system specific method');
  }
}
 
// Target interface that the client code expects
class TargetInterface {
  requiredMethod() {}
}
 
// Adapter class that adapts OldSystem to TargetInterface
class Adapter extends TargetInterface {
  constructor(oldSystem) {
    super();
    this.oldSystem = oldSystem;
  }
 
  requiredMethod() {
    this.oldSystem.specificMethod();
  }
}
 
// Client code that uses the adapter
const oldSystem = new OldSystem();
const adapter = new Adapter(oldSystem);
 
// Now, you can use the adapter as if it were an instance of TargetInterface
adapter.requiredMethod();


Output

Old system specific method





Below is the explanation of the above code:

Adapter class allows you to use the OldSystem with the interface expected by the client code TargetInterface, and calling adapter.requiredMethod() correctly invokes the specificMethod of the OldSystem. This demonstrates how the Adapter pattern bridges the gap between two incompatible interfaces.

Diagrammatic Explanation of the Adapter Method in Javascript

Diagram of the Adapter Method in JavaScript

In this diagram:

  • “Existing System” represents the OldSystem class with its specificMethod().
  • “Target Interface” represents the interface that the client code expects with its requiredMethod().
  • “Adapter” is the class that adapts the OldSystem to the Target Interface and implements requiredMethod() by invoking specificMethod() from OldSystem.

The flow of the method call is from the client code through the Adapter to the OldSystem, allowing the OldSystem to work seamlessly with the client’s expected interface.

Diagram explaining Adapter Method in Javascript

In this diagram:

  • “Client Code” is the code that expects to use the Target Interface.
  • “Target Interface” is the interface that the client code expects to interact with.
  • “Adapter” is the class that adapts the Adaptee (Old System) to the Target Interface.
  • “Adaptee (Old System)” is the existing class with an incompatible interface.

The relationships and interactions:

  • The “Target Interface” is implemented by the “Adapter.”
  • The “Adapter” adapts the “Adaptee” by encapsulating its methods and providing a compatible interface to the client code.
  • The client code interacts with the “Adapter,” which, in turn, interacts with the “Adaptee” to make the two interfaces compatible.

Adapter Method | JavaScript Design Patterns

Adapter Pattern in JavaScript is a structural design pattern that allows you to make one interface or object work with another that has a different interface. It acts as a bridge, enabling the compatibility of two systems that would not naturally work together.

Important Topics for the Adapter Method in JavaScript Design Patterns

  • Use Cases of the Adapter Method in JavaScript Design Patterns
  • Adapter Method example in JavaScript Design Patterns
  • Advantages of the Adapter Method in JavaScript Design Patterns
  • Disadvantages of the Adapter Method in JavaScript Design Patterns
  • Conclusion

Similar Reads

Use Cases of the Adapter Method in JavaScript Design Patterns

The Adapter Method in JavaScript is a useful design pattern that can be applied in various scenarios such as:...

Adapter Method example in JavaScript Design Patterns

Problem Statement:...

Advantages of the Adapter Method in JavaScript Design Patterns

...

Disadvantages of the Adapter Method in JavaScript Design Patterns

Here are the advantages of using the Adapter Method:...

Conclusion

Here are the disadvantages of using the Adapter Method:...