Docsity

Case Study: Decorator & Strategy Patterns in OOP with Lexi Editor - Prof. Jeffrey S. Foste, Papers of Programming Languages

Toggle sidebar
bg1
77
CMSC 433 – Programming Language
Technologies and Paradigms
Spring 2003
Some Design Patterns
March 6, 2003
78
More OMT Notation
•Arrow beginning with diamond
–“part-of” or aggregation
–Only accessed by object pointing to it
•Arrow ending in filled circle
–More than one
79
Decorator Pattern
•Motivation
–Want to add responsibilities/capabilities to
individual objects, not to an entire class.
–Inheritance requires a compile-time choice of
parent class.
• Solution
–Enclose the component in another object that
adds the responsibility/capability
•The enclosing object is called a decorator.
80
Decorator Pattern: Example
81
Decorator Pattern: Features
•A decorator conforms to the interface of the
component it decorates
–so that its presence is transparent to the component's
clients.
•A decorator forwards requests to its encapsulated
component and may perform additional actions
before or after forwarding.
•Can nest decorators recursively, allowing
unlimited added responsibilities.
•Can add/remove responsibilities dynamically.
82
Structure
pf3
pf4
pf5
pf8

Partial preview of the text

Download Case Study: Decorator & Strategy Patterns in OOP with Lexi Editor - Prof. Jeffrey S. Foste and more Papers Programming Languages in PDF only on Docsity!

77

CMSC 433 – Programming Language

Technologies and Paradigms

Spring 2003

Some Design Patterns

March 6, 2003

78

More OMT Notation

• Arrow beginning with diamond

  • “part-of” or aggregation
  • Only accessed by object pointing to it

• Arrow ending in filled circle

  • More than one 79

Decorator Pattern

• Motivation

– Want to add responsibilities/capabilities to

individual objects, not to an entire class.

– Inheritance requires a compile-time choice of

parent class.

• Solution

– Enclose the component in another object that

adds the responsibility/capability

  • The enclosing object is called a decorator. 80

Decorator Pattern: Example

Decorator Pattern: Features

• A decorator conforms to the interface of the

component it decorates

  • so that its presence is transparent to the component's

clients.

• A decorator forwards requests to its encapsulated

component and may perform additional actions

before or after forwarding.

• Can nest decorators recursively, allowing

unlimited added responsibilities.

• Can add/remove responsibilities dynamically.

Structure

83

Decorator Pattern Analysis

  • Advantages
    • fewer classes than with static inheritance
    • dynamic addition/removal of decorators
    • keeps root classes simple
  • Disadvantages
    • proliferation of run-time instances
    • abstract Decorator must provide common interface
  • Tradeoffs:
    • useful when components are lightweight
    • otherwise use Strategy 84

Example: Java I/O

FileReader frdr = new FileReader(filename); LineNumberReader lrdr = new LineNumberReader(frdr); String line; while ((line = lrdr.readLine()) != null) { System.out.print(lrdr.getLineNumber() + ":\t" + line); } 85

Interaction diagram

86

Lexi: Simple GUI-Based Editor

• Lexi is a WYSIWYG editor

  • supports documents with textual and graphical objects
  • scroll bars to select portions of the document
  • be easy to port to another platform
  • support multiple look-and-feel interfaces

• Highlights several OO design issues

• Case study of design patterns in the design of Lexi

Lexi User Interface

Design Issues

• Representation and manipulation of document

• Formatting a document

• Adding scroll bars and borders to Lexi windows

• Support multiple look-and-feel standards

  • Motif and Presentation Manager (!)

• Handle multiple windowing systems

• Support user operations

• Advanced features

  • spell-checking and hyphenation

95

Composite Pattern Consequences

• Class hierarchy has both simple and composite

objects

• Simplifies clients

• Aids extensibility

  • clients do not have to be modified

• Too general a pattern?

  • difficult to to restrict functionality of concrete leaf

subclasses

96

Formatting Lexi Documents: Strategy

  • We know that documents are represented as Glyphs, but not how documents are constructed.
  • Formatting:
    • Document margins, line structure breaking, will etc. be determined based on rules for justification,
    • Many good algorithms exist;
      • different tradeoffs between quality and speed
  • Design decision: implement different algorithms, decide at run-time which algorithm to use - define root class that supports many algorithms - each algorithm implemented in a subclass 97

Strategy Pattern

• Name

  • Strategy (aka Policy)

• Applicability

  • many related classes differ only in their behavior
  • many different variants of an algorithm
  • need to encapsulate algorithmic information 98

Strategy Pattern: Structure

Strategy Pattern: Consequences

• Clear separation of algorithm definition and use

  • glyphs and formatting algorithms are independent
  • alternative (many subclasses) is unappealing
    • proliferation of classes
    • algorithms cannot be changed dynamically

• Elimination of conditional statements

  • Like State, Template, …
  • Typical in OO programming

Strategy Pattern Consequences (cont’d)

• 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

101

Lexi: Using Strategy

• Compositor and Composition classes

  • Compositor: class encapsulating formatting algorithm
    • pass Composition objects to be formatted as parameters to Compositor methods
  • Composition: things being formatted
    • Glyph subclass
    • Each Composition object refers to its Compositor object
    • When a Composition needs to format itself, it sends a message to its Compositor instance 102

Class Diagram

103

Object Structure after Formatting

104

Adding Scroll Bars and Borders: Decorator

• How to define classes for scrollbars and borders?

• Define as subclasses of Glyph

  • Scrollbars and borders are displayable objects
  • Will use 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” )

Transparent Enclosure

• Two features:

  • Single-child composition
    • Calls its child, then adds its own behavior
  • Compatible interfaces
    • Can use the enclosing object in place of the one it encloses

• Implemented by the Decorator pattern

  • Saw this earlier

Monoglyph class: a Decorator

Class Monoglyph { … void Draw (Window w) { component.Draw(w); } … } Class Border extends Monoglyph { .. void Draw (Window w) { super.Draw(w); DrawBorder(w); } … }

113

Structure of Abstract Factory

114

Abstract Factory: Consequences

• Isolate instance creation and handling from clients

• Can easily change look-and-feel standard

  • Reassign a global variable;
  • Recompute and redisplay the interface

• Enforce consistency among products in each

family

• Adding to family of products is difficult

  • Have to update factory abstract class and all concrete

classes

115

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

116

Implementation

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