Docsity

Design Patterns: Template Method and Observer - Prof. Alan L. Sussman, Study notes of Programming Languages

Partial preview of the text

Download Design Patterns: Template Method and Observer - Prof. Alan L. Sussman and more Study notes Programming Languages in PDF only on Docsity!

CMSC 433, Fall 2002 37

Template Method pattern

• Problem

  • You’re building a reusable class
  • You have a general approach to solving a problem,
  • But each subclass will do things differently

• Solution

  • Invariant parts of an algorithm in parent class
  • Encapsulate variant parts in template methods
  • Subclasses override template methods
  • At runtime template method invokes subclass ops

CMSC 433, Fall 2002 38

Structure

CMSC 433, Fall 2002 39

Example: JUnit

• Junit uses template methods pattern

Junit.framework.TestCase.run() { setUp(); runTest(); tearDown() }

• In class example, subclass (LogRecordTest)

overrides runTest() and setUp()

CMSC 433, Fall 2002 40

Observer pattern

  • Problem
    • dependent’s state must be consistent with master’s state
  • Solution structure
    • define four kinds of objects:
      • abstract subject
        • maintain list of dependents; notifies them when master changes
      • abstract observer
        • define protocol for updating dependents
      • concrete subject
        • manage data for dependents; notifies them when master changes
      • concrete observers
        • get new subject state upon receiving update message

CMSC 433, Fall 2002 43

Observer Pattern (cont’d)

• Consequences

  • low coupling between subject and observers
    • subject unaware of dependents
  • support for broadcasting
    • dynamic addition and removal of observers
  • unexpected updates
    • no control by the subject on computations by observers

CMSC 433, Fall 2002 44

Observer pattern (cont’d)

• Implementation issues

  • storing list of observers
    • typically in subject
  • observing multiple subjects
    • typically add parameters to update()
  • who triggers update?
    • State-setting operations of subject
      • Possibly too many updates
    • client
      • Error-prone if an observer forgets to send notification message

CMSC 433, Fall 2002 45

Observer pattern (cont’d)

• Implementation issues (cont’d)

  • possibility of dangling references when subject is deleted - easier in garbage-collected languages - subject notifies observers before dying
  • possibility of premature notifications
    • typically, method in Subject subclass calls inherited method which does notification
    • solve by using Template method pattern
      • method in abstract class calls deferred methods, which is defined by concrete subclasses

CMSC 433, Fall 2002 46

Observer pattern (cont’d)

• Implementation issues (cont’d)

  • how much information should subject send with update() messages? - Push model: Subject sends all information that observers may require - May couple subject with observers (by forcing a given observer interface) - Pull model: Subject sends no information - Can be inefficient
  • registering observers for certain events only
    • use notion of an aspect in subject
    • Observer registers for one or more aspects

CMSC 433, Fall 2002 49

More implementation issues

  • Implementations of the Observer pattern often have the subject broadcast additional information about the change. - At one extreme, the subject sends observers detailed information about the change, whether they want it or not. At the other extreme the subject sends nothing but the most minimal notification, and observers ask for details explicitly thereafter
  • You can extend the subject's registration interface to allow registering observers only for specific events of interest.

CMSC 433, Fall 2002 50

Examples

• The standard Java and JavaBean event model is an

example of an observer pattern

This document was created with Win2PDF available at http://www.daneprairie.com. The unregistered version of Win2PDF is for evaluation or non-commercial use only.