Showing posts with label UML. Show all posts
Showing posts with label UML. Show all posts

Saturday, 2 November 2013

Adapter Design pattern - Sequence diagram

🚀 Enjoying Java Tutorials?

Subscribe to Ram N Java for clean, beginner-friendly core Java tutorials and interview prep!

🔔 Subscribe to Ram N Java

Introduction

While class diagrams show the static structure and relationships of classes, a Sequence Diagram illustrates dynamic interactions over time. In the Adapter Design Pattern, the sequence diagram clearly demonstrates how method invocations flow from the client through the adapter layer to the adaptee, and how responses flow back.

Key Participants in the Flow

  • Client: The initiating object that needs a specific operation executed using standard interface methods.
  • Target Interface / Adapter: The intermediate entity implementing the client's expected interface. It intercepts method invocations and translates parameters/data formats.
  • Adaptee: The existing component or third-party service providing the actual functionality under an incompatible method signature.

Step-by-Step Message Flow

  1. Client Invocation: The Client calls a method defined by the Target interface on the Adapter (e.g., request()).
  2. Data / Protocol Translation: The Adapter receives the request, transforms the input parameters, and adapts them into the format expected by the Adaptee.
  3. Adaptee Delegation: The Adapter invokes the specific method on the Adaptee (e.g., specificRequest()).
  4. Execution & Result Return: The Adaptee executes the underlying business logic and returns the raw result back to the Adapter.
  5. Response Transformation & Hand-off: The Adapter translates the result into the expected target format and returns it back to the Client.

Sequence Overview Table

Step Sender Receiver Action / Message
1 Client Adapter Calls request()
2 Adapter Adaptee Translates and calls specificRequest()
3 Adaptee Adapter Returns result
4 Adapter Client Converts and returns response to Client

Related Tutorials from Ram N Java


Click here to watch in Youtube : https://www.youtube.com/watch?v=RHpEqWjHd04

Click the below Image to Enlarge

Adapter Design pattern - Sequence diagram

Friday, 1 November 2013

Adapter Design pattern - Class diagram

🚀 Enjoying Java Tutorials?

Subscribe to Ram N Java for clean, beginner-friendly core Java tutorials and interview prep!

🔔 Subscribe to Ram N Java

Introduction

The Adapter Design Pattern is a structural design pattern that bridges two incompatible interfaces, allowing them to collaborate seamlessly. To understand how the pattern is modeled in software architecture, we look at its UML Class Diagrams. Depending on whether you use object composition or class inheritance, the pattern takes the form of an Object Adapter or a Class Adapter.

Key Participants in the Class Diagram

  • Client: The class containing the business logic that invokes operations through the Target interface.
  • Target Interface: Defines the domain-specific contract or method signatures that the Client expects to interact with (e.g., request()).
  • Adaptee: The existing class or legacy/third-party component that contains useful functionality but provides an incompatible interface (e.g., specificRequest()).
  • Adapter: The intermediary class that implements the Target interface and maps calls from the Client to the Adaptee.

Object Adapter vs. Class Adapter Diagram

1. Object Adapter (Composition)

In the Object Adapter design:

  • The Adapter class implements the Target interface.
  • The Adapter maintains an instance variable reference to the Adaptee (has-a relationship / composition).
  • When request() is called, the Adapter delegates the call to adaptee.specificRequest().

2. Class Adapter (Inheritance)

In the Class Adapter design:

  • The Adapter class implements the Target interface.
  • The Adapter directly extends (subclasses) the Adaptee class (is-a relationship / inheritance).
  • When request() is called, the Adapter calls the inherited method specificRequest() directly.

Structural Comparison

Feature Object Adapter Class Adapter
Relationship with Adaptee Composition (Holds a reference) Inheritance (Extends Adaptee)
Flexibility High (Works with Adaptee and all its subclasses) Rigid (Tied to a specific Adaptee class)
Java Support Fully supported and preferred Requires Target to be an interface due to single inheritance

Related Tutorials from Ram N Java


Click here to watch in Youtube : https://www.youtube.com/watch?v=AFNWK3Wo1DA

Click the below Image to Enlarge
Adapter Design pattern - Class diagram

Tutorials