Docsity

Design Patterns: Bridge and Command in CMSC 433 Spring 2004, Papers of Programming Languages

Toggle sidebar
bg1
162
CMSC 433 – Programming Language
Technologies and Paradigms
Spring 2004
Design Patterns
March 4, 2004
163
Multiple Window Systems
•Want portability to different window systems
–Similar to multiple look-and-feel problem, but different
vendors will build widgets differently
•Solution:
–Define abstract class Window, with basic window
functionality (e.g., draw, iconify, move, resize, etc.)
–Define concrete subclasses for specific types of
windows (e.g., dialog, application, icon, etc.)
–Define WindowImp hierarchy to handle window
implementation by a vendor
164
Implementation
165
Bridge Pattern
• Name
–Bridge or Handle or Body
•Applicability
–Handles abstract concept with different implementations
–Implementation may be switched at run-time
–Implementation changes should not affect clients
–Hide a class’s interface from clients
•Structure: use two hierarchies
–Logical one for clients,
–Physical one for different implementations
166
Structure of Bridge Pattern
167
Bridge Pattern
•Consequences:
–Decouple interface from implementation and
representation
–Change implementation at run-time
–Improve extensibility
•Logical classes and physical classes change independently
•Hides implementation details from clients
–Sharing implementation objects and associated reference counts
pf3

Partial preview of the text

Download Design Patterns: Bridge and Command in CMSC 433 Spring 2004 and more Papers Programming Languages in PDF only on Docsity!

162

CMSC 433 – Programming Language

Technologies and Paradigms

Spring 2004

Design Patterns

March 4, 2004

163

Multiple Window Systems

• Want portability to different window systems

  • Similar to multiple look-and-feel problem, but different vendors will build widgets differently

• Solution:

  • Define abstract class Window, with basic window functionality (e.g., draw, iconify, move, resize, etc.)
  • Define concrete subclasses for specific types of windows (e.g., dialog, application, icon, etc.)
  • Define WindowImp hierarchy to handle window implementation by a vendor 164

Implementation

165

Bridge Pattern

• Name

  • Bridge or Handle or Body

• Applicability

  • Handles abstract concept with different implementations
  • Implementation may be switched at run-time
  • Implementation changes should not affect clients
  • Hide a class’s interface from clients

• Structure: use two hierarchies

  • Logical one for clients,
  • Physical one for different implementations

Structure of Bridge Pattern

Bridge Pattern

• Consequences:

  • Decouple interface from implementation and representation
  • Change implementation at run-time
  • Improve extensibility
    • Logical classes and physical classes change independently
    • Hides implementation details from clients
      • Sharing implementation objects and associated reference counts

168

Supporting User Commands

• Support execution of Lexi commands

  • GUI doesn’t know
    • Who command is sent to
    • Command interface

• Complications

  • Different commands have different interfaces
  • Same command can be invoked in different ways
  • Undo and Redo for some, but not all, commands (print) 169

Supporting User Commands (cont’d)

• An improved solution

  • Create abstract “command” class
  • Create action-performing glyph subclass
  • Delegate action to command

• Key ideas

  • Pass an object, not a function
  • Pass context to the command function
  • Store command history 170

Command Objects

171

Command Pattern

• Name

  • Command or Action or Transaction

• Applicability

  • Parameterize objects by actions they perform
  • Specify, queue, and execute requests at different times
  • Support undo by storing context information
  • Support change log for recovery purposes
  • Support high-level operations
    • Macros

Structure of Command Pattern

Command Pattern

• Consequences:

  • Decouple receiver and executor of requests
    • Lexi example: Different icons can be associated with the same command
  • Commands are first class objects
  • Easy to support undo and redo
    • Command must have method to check whether it’s reversible
    • Must add state information
  • Can create composite commands
    • Editor macros
  • Can extend commands more easily