Jump to Menu

Bridge Pattern Tutorial

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

Compatible edition(s): Enterprise, Professional, Standard

  • October 8, 2009
  • Views: 35,456
  • PDF

Modeling Design Pattern with Class Diagram

  1. Create a new project Design Patterns.
  2. Create a class diagram Bridge.
    new diagram
  3. Select Class from diagram toolbar. Click on the diagram to create a class. Name it as Abstraction.
    create abstraction class
  4. Right-click on Abstraction, and select Model Element Properties > Abstract to set it as abstract.
    set abstraction abstract
  5. Move the mouse cursor over the Abstraction class, and drag out Aggregation > Class to create an associated class Implementor.
    create implementor
  6. Right-click on Implementor, and select Model Element Properties > Abstract to set it as abstract.
  7. Right-click on the Abstract class, and select Add > Operation from the popup menu.
    add oper to abstraction
  8. Name the operation Operation().
  9. Right-click on the Implementor class, and select Add > Operation from the popup menu. Name the operation OperationImpl().
    create operation impl
  10. Right-click on Implementor, and select Model Element Properties > Abstract to set it as abstract.
    set operation impl abstract
  11. Move the mouse cursor over the Abstraction class, and drag out Generalization > Class to create a subclass RefinedAbstraction.
    create refined abstraction
  12. Repeat the previous step to create a subclass ConcreteImplementor from Implementor.
    create concrete implementor
  13. ConcreteImplementor will inherits the operations from Implementor. Right-click on ConcreteImplementor and select Related Elements > Realize all Interfaces from the popup menu.
    realize implementor
  14. In practice, there may be multiple refined abstractions and/or concrete implementors. To represent this, stereotype the class RefinedAbstraction and ConcreteImplementor as PTN Cloneable. Right-click on Abstraction and select Stereotypes > Stereotypes... from the popup menu.
    set abstract stereotype
  15. In the Stereotypes tab of the Class Specification dialog box, select PTN Cloneable and click > to assign it to RefinedAbstraction class. Click OK to confirm.
    set PTN cloneable
  16. Repeat steps 14 and 15 on concreteImplementor.
    pattern modeled
  17. In practice, there may be multiple operations and/or operationImpls. To represent this, stereotype the class Abstraction and Implementor as PTN Members Creatable. Repeat steps 14 and 15 to stereotype Abstraction and Implementor as PTN Members Creatable.

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 design pattern
  3. In the Define Design Pattern dialog box, specify the pattern name Bridge. Keep the file name as is. Click OK to proceed.
    name design pattern

Applying Design Pattern on Class Diagram

In this section, we are going to apply the bridge pattern to model a report generator for various report types.

  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 Bridge from the list of patterns.
    select bridge
  5. Click on Abstraction in the overview.
    select abstraction
  6. Rename Abstraction to ReportGenerator and operation Operation to generate at the bottom pane.
    rename abstraction
  7. Select RefinedAbstraction in the overview pane.
  8. Rename RefinedAbstraction to HTMLReportGenerator.
    rename generator
  9. Besides HTML report generator, we need also a Plain Text report generator. Click on the + button at the bottom pane, beside Abstraction, and select Clone...
    select clone
  10. Enter 1 to be the number of classes to clone. Click OK to confirm.
    enter no of classes to clone
  11. Rename RefinedAbstraction2, the cloned class, to PlainTextReportGenerator.
    plain text rpt generator
  12. Select Implementor in the overview pane.
  13. Rename Implementor to ReportGeneratorImpl, and OperationImpl to generateTOC.
    rename implementor
  14. We need 2 more operations for generating content and appendix. Click on the + button and select New Operation... from the popup meun.
    new oper
  15. In the Operation Specification dialog box, name the operation generateContent. Check Abstract at the bottom of the dialog box.
    new gen content
  16. Repeat the previous steps to create another abstract operation generateAppendix.
    opers defined in implementor
  17. Select ConcreteImplementor in overview. Rename ConcreteImplementor to SimpleReportGeneratorImpl, and operation OperationImpl to generateTOC.
    rename concrete implementor
  18. Similar to RefinedAbstraction, here we need to have another concrete implementor for generating complex report. Click on the + button and select Clone... from the popup menu.
    select clone
  19. Enter 1 to be the number of classes to clone. Click OK to confirm.
  20. Rename ConcreteImplmentor2, the cloned class, to ComplexReportGeneratorImpl, and operation OperationImpl to generateTOC.
    complex report generator created
  21. Click OK to confirm. Here is the diagram formed:
    diagram formed
  22. We want HTMLReportGenerator and PlainTextReportGenerator to implement their own way of generating repot. Select the generate operation is ReportGenerator.
    select generate in report generator
  23. Press the Ctrl key, and drag to HTMLReportGenerator. Replease the mouse button afterwards.
    duplicate generate
  24. Repeat the previous steps to create the generate method in PlainTextReportGenerator. Here is the completed diagram:
    complete diagram



创造美好 共同成长

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.

OK