Docsity

Design Patterns: Strategy, Decorator, and Visitor in CMSC 433, Fall 2002 - Prof. Adam A. P, Study notes of Programming Languages

Toggle sidebar
bg1
1
CMSC 433, Fall 2002
77
Strategy Pattern (cont’d)
•
Consequences (continued)
–
clients must be aware of different strategies
•
when initializing objects
–
proliferation of instances at run
-
time
•
each glyph has a strategy object with formatting information
•
if strategy is stateless, share strategy objects
CMSC 433, Fall 2002
78
Adding scroll bars and borders
•
Where do we define classes for scrollbars and
borders?
•
Define as subclasses of Glyph
–
scrollbars and borders are displayable objects
–
supports notion of transparent enclosure
•
clients don’t need to know whether they are dealing with a
component or with an enclosure
•
Inheritance increases number of classes
–
use composition instead (ā€œhas aā€ )
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18

Partial preview of the text

Download Design Patterns: Strategy, Decorator, and Visitor in CMSC 433, Fall 2002 - Prof. Adam A. P and more Study notes Programming Languages in PDF only on Docsity!

CMSC 433, Fall 2002 77

Strategy Pattern (cont’d)

• Consequences (continued)

  • clients must be aware of different strategies
    • when initializing objects
  • proliferation of instances at run-time
    • each glyph has a strategy object with formatting information
    • if strategy is stateless, share strategy objects

CMSC 433, Fall 2002 78

Adding scroll bars and borders

• Where do we define classes for scrollbars and

borders?

• Define as subclasses of Glyph

  • scrollbars and borders are displayable objects
  • supports notion of transparent enclosure
    • clients don’t need to know whether they are dealing with a component or with an enclosure

• Inheritance increases number of classes

  • use composition instead (ā€œhas aā€ )

CMSC 433, Fall 2002 79

Monoglyph class

void MonoGlyph::Draw (Window* w) { component->Draw(w); }

void Border::Draw (Window* w) { MonoGlyph::Draw(w); DrawBorder(w); }

CMSC 433, Fall 2002 80

Decorator Pattern

• Name

  • Decorator (aka Wrapper)

• Applicability

  • add responsibilities to objects dynamically and transparently
  • handle responsibilities that can be withdrawn at run- time

CMSC 433, Fall 2002 83

Multiple look-and-feel standards

  • Change Lexi’s look-and-feel at run-time
  • Obvious solution has clear disadvantages
    • use distinct class for each widget and standard
    • let clients handle different instances for each standard
  • Problems:
    • proliferation of classes
    • can’t change standard at run-time
    • code for look-and-feel standard visible to clients
  • Code example:
    • Button* pb = new MotifButton; // bad
    • Button* pb = guiFactory->createButton();// better

CMSC 433, Fall 2002 84

Multiple look-and-feel standards (cont’d)

• Solution:

  • define abstract class GUIFactory with creation methods (deferred) for widgets
  • concrete subclasses of GUIFactory actually define creation methods for each look-and-feel standard - MotifFactory, MacFactory, etc.
  • must still specialize each widget into subclasses for each look-and-feel standard

CMSC 433, Fall 2002 85

Class diagram for GUIFactory

CMSC 433, Fall 2002 86

Diagram for product classes

CMSC 433, Fall 2002 89

Abstract Factory (cont’d)

• Consequences

  • isolate creation and handling of instances from clients
  • changing look-and-feel standard at run-time is easy
    • reassign a global variable;
    • recompute and redisplay the interface
  • enforces consistency among products in each family
  • adding new family of products is difficult
    • have to update all factory classes

CMSC 433, Fall 2002 90

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

CMSC 433, Fall 2002 91

Implementation

CMSC 433, Fall 2002 92

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

CMSC 433, Fall 2002 95

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)

CMSC 433, Fall 2002 96

Supporting User Commands (cont’d)

• An improved solution

  • create abstract ā€œcommandā€ class
    • command must have reversible method
  • 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

CMSC 433, Fall 2002 97

Command Objects

CMSC 433, Fall 2002 98

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

CMSC 433, Fall 2002 101

Command Pattern

• Implementation notes

  • how much should command do itself?
  • support undo and redo functionality
    • operations must be reversible
    • may need to copy command objects
    • don’t record commands that don’t change state
  • avoid error accumulation in undo process

CMSC 433, Fall 2002 102

Spell-Checking and Hyphenation

• Must do texual analysis

  • multiple operations and implementations

• Must add new functions and operations easily

• Must efficiently handle scattered information and

varied implementations

  • different traversal strategies for stored information

• Should separate traversal actions from traversal

CMSC 433, Fall 2002 103

Iterator Pattern

• Name

  • Iterator or Cursor

• Applicability

  • access aggregate objects without exposing internals
  • support multiple traversal strategies
  • uniform interface for traversing different objects

CMSC 433, Fall 2002 104

Iterator Pattern

  • Key ideas
    • separate aggregate structures from traversal protocols
    • support addition of traversal functionality
    • small interfaces for aggregate classes,
    • multiple simultaneous traversals
  • Pattern structure
    • abstract Iterator class defines traversal protocol
    • concrete Iterator subclasses for each aggregate class
    • aggregate instance creates instances of Iterator objects
    • aggregate instance keeps reference to Iterator object

CMSC 433, Fall 2002 107

Iterator Pattern (cont’d)

• Consequences

  • support different kinds of traversal strategies
    • just change Iterator instance
  • simplify aggregate’s interface
    • no traversal protocols
  • supports simultaneous traversals

CMSC 433, Fall 2002 108

Iterator Pattern (cont’d)

• Implementation issues

• Who controls iteration?

  • external vs. internal iterators
    • external:
      • client controls the iteration via ā€œnextā€ operation
      • very flexible
      • some operations are simplified - logical equality and set operations are difficult otherwise
    • internal:
      • Iterator applies operations to aggregate elements
      • easy to use
      • can be difficult to implement in some languages

CMSC 433, Fall 2002 109

Iterator Pattern (cont’d)

• Who defines the traversal algorithm?

  • Iterator itself
    • may violate encapsulation
  • aggregate
    • Iterator keeps only state of iteration

• How robust is the Iterator?

  • are updates or deletions handled?
  • don’t want to copy aggregates
  • register Iterators with aggregate and clean-up as needed
  • synchronization of multiple Iterators is difficult

CMSC 433, Fall 2002 110

Implementation of Operations

• Compiler represents prog. as abstract syntax tree

  • Different nodes for different components for a language
    • Different classes for assignment stmts, vars, expressions.
  • Operations on abstract syntax trees
    • Type checking, code generation, pretty-printing, etc

CMSC 433, Fall 2002 113

Visitor pattern

• Name

  • Visitor or double dispatching

• Applicability

  • related objects must support different operations and actual op depends on both the class and the op type
  • distinct and unrelated operations pollute class defs
  • object structure rarely changes, but ops changed often

CMSC 433, Fall 2002 114

Visitor Pattern (cont’d)

• Structure

  • define two class hierarchies,
    • one for object structure
    • one for each operation family (called visitors)
  • compiler example:
    • Object subclasses VariableRef, AssignmentNode.
    • Operation subclasses TypeCheck, GenerateCode, PrettyPrint

CMSC 433, Fall 2002 115

Visitor pattern

CMSC 433, Fall 2002 116

Structure of Visitor Pattern