Jump to Menu

Adapter Pattern Tutorial

This tutorial is aimed to guide the definition and application of Gang of Four (GoF) adapter design pattern. By reading this tutorial, you will know how to develop a model for the adapter pattern, and how to apply it in practice.

Compatible edition(s): Enterprise, Professional, Standard

  • October 7, 2009
  • Views: 26,125
  • PDF

Modeling Design Pattern with Class Diagram

  1. Create a new project Design Patterns.
  2. Create a class diagram Adapter.
    new diagram
  3. Select Class from diagram toolbar. Click on the diagram to create a class. Name it as Client.
    create client class
  4. Move the mouse cursor over the Client class, and drag out Association > Class to create an associated class Target.
    create target
  5. Right-click on Target, and select Model Element Properties > Abstract to set it as abstract.
    set target abstract
  6. Right-click on the Target class, and select Add > Operation from the popup menu.
    add oper
  7. Name the operation Request().
  8. Right-click on Request, and select Model Element Properties > Abstract to set it as abstract.
    set request abstract
  9. Move the mouse cursor over the Target class, and drag out Generalization > Class to create a subclass Adapter.
    create adapter
  10. Adapter will inherits the operations from Target. Right-click on Adapter and select Related Elements > Realize all Interfaces from the popup menu.
    realize adapter
  11. Move the mouse cursor over the Adapter class, and drag out Association > Class to create an associated class Adaptee.
    create adaptee
  12. In practice, there may be multiple requests. To represent this, stereotype the class Target as PTN Members Creatable. Right-click on Target and select Stereotypes > Stereotypes... from the popup menu.
    stereotype target
  13. In the Stereotype tab of the Class Specification dialog box, select PTN Members Creatable and click > to assign it to Target class. Click OK to confirm.
    stereotype ptn members creatable
    Up to now, the diagram should look like this:
    pattern modeled

Defining Pattern

  1. Select all classes on the class diagram.
    select all classes
  2. Right-click on the selection and select Define Design Pattern... from the popup menu.
    define pattern
  3. In the Define Design Pattern dialog box, specify the pattern name Adapter. Keep the file name as is. Click OK to proceed.
    name adapter pattern

Applying Design Pattern on Class Diagram

In this section, we are going to apply the adapter pattern to wrap a legacy Shape class.

  1. Create a new project Diagram Editor.
  2. Create a class diagram Domain Model.
  3. Right-click on the class diagram and select Utilities > Apply Design Pattern... from the popup menu.
    apply design pattern
  4. In the Design Pattern dialog box, select Adapter from the list of patterns.
    select adapter
  5. Click on Target in the overview.
    select target
  6. Rename Target to Shape, and operation Request to getColor at the bottom pane.
    rename target
  7. Besides the operation getColor, we also need two more operations for getPosition and draw. Keep Target selected, click on the + button at the bottom pane, and select New Operation... from the popup menu.
    new oper
  8. In the Operation Specification dialog box, name the operation getPosition. Check Abstract at the bottom of dialog box.
    create get position
  9. Repeat steps 7 and 8 to create operation draw.
    create shape opers
  10. Select Adapter in overview, and rename it as NewShape at the bottom pane. Rename also the operation Request to getColor. Note that if the option Auto Rename is on, rename of operation is not needed as this will be done automatically.
    rename adapter
  11. Select Adaptee in overview, and rename it as LegacyShape at the bottom pane. Click OK to apply the pattern to diagram.
    rename adaptee
  12. We need to create the specific requests in LegacyShape. Select the operations in NewShape.
    select opers
  13. Press on the Ctrl key, and drag to LegacyShape to copy them.
    This is the result:

Turn every software project into a successful one.

We use cookies to offer you a better experience. By visiting our website, you agree to the use of cookies as described in our Cookie Policy.