Docsity

Design Patterns: Understanding Object-Oriented Solutions - CMSC 433, Fall 2002 - Prof. Ada, Study notes of Programming Languages

Partial preview of the text

Download Design Patterns: Understanding Object-Oriented Solutions - CMSC 433, Fall 2002 - Prof. Ada and more Study notes Programming Languages in PDF only on Docsity!

CMSC433, Fall 2002

Design Patterns

Adam Porter Sep 24, 2002

CMSC 433, Fall 2002 2

What is a pattern?

  • Patterns = problem/solution pairs in context
  • Patterns facilitate reuse of successful software architectures and design
  • Not code reuse
    • Instead, solution/strategy reuse
    • Sometimes, interface reuse

CMSC 433, Fall 2002 3

Gang of Four

  • The book that started it all
  • Community refers to authors as the “Gang of Four”
  • Figures and some text in these slides come from book
  • On reserve in CS library (3 rd^ floor AVW)

CMSC 433, Fall 2002 4

Object Modeling Technique (OMT)

  • Used to describe patterns in GO4 book
  • Graphical representation of OO relationships
    • Class diagrams show the static relationship between classes
    • Object diagrams represent the state of a program as series of related objects
    • Interaction diagrams illustrate execution of the program as an interaction among related objects

CMSC 433, Fall 2002 7

Subclassing and Abstract Classes

CMSC 433, Fall 2002 8

Pseudo-code and Containment

CMSC 433, Fall 2002 9

Object diagrams

CMSC 433, Fall 2002 10

Interaction diagrams

time

CMSC 433, Fall 2002 13

Components of a Pattern (cont’d)

  • Implementation
    • critical portion of plausible code for pattern
  • Known uses
    • often systems that inspired pattern
  • References - See also
    • related patterns that may be applied in similar cases

CMSC 433, Fall 2002 14

Design patterns taxonomy

  • Creational patterns
    • concern the process of object creation
  • Structural patterns
    • deal with the composition of classes or objects
  • Behavioral patterns
    • characterize the ways in which classes or objects interact and distribute responsibility.

CMSC 433, Fall 2002 15

Creation patterns

  • Singleton
    • Ensure a class only has one instance, and provide a global point of access to it.
  • Typesafe Enum
    • Generalizes Singleton: ensures a class has a fixed number of unique instances.
  • Abstract Factory
    • Provide an interface for creating families of related or dependent objects without specifying their concrete classes.

CMSC 433, Fall 2002 16

Structural patterns

  • Adapter
    • Convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn't otherwise because of incompatible interfaces
  • Proxy
    • Provide a surrogate or placeholder for another object to control access to it
  • Decorator
    • Attach additional responsibilities to an object dynamically

CMSC 433, Fall 2002 19

Principles (cont’d)

  • Black-box vs. white-box reuse
    • black-box relies on object references, usually through instance variables
    • white-box reuse by inheritance
    • black-box reuse preferred for information hiding, run- time flexibility, elimination of implementation dependencies
    • disadvantages: Run-time efficiency (high number of instances, and communication by message passing)
  • Favor composition over class inheritance

CMSC 433, Fall 2002 20

Principles (cont’d)

  • Delegation
    • powerful technique when coupled with black-box reuse
    • Allow delegation to different instances at run-time, as long as instances respond to similar messages
    • disadvantages:
      • sometimes code harder to read and understand
      • efficiency (because of black-box reuse)

Some Design Patterns

CMSC 433, Fall 2002 22

Singleton objects

  • Some classes have conceptually one instance
    • Many printers, but only one print spooler
    • One file system
    • One window manager
  • Naïve: create many objects that represent the same conceptual instance
  • Better: only create one object and reuse it
    • Encapsulate the code that manages the reuse

CMSC 433, Fall 2002 25

Implementing the Singleton method

  • In Java, just define a final static field public class Singleton { private Singleton() {…} final private static Singleton instance = new Singleton(); public Singleton getInstance() { return instance; } }
  • Java semantics guarantee object is created immediately before first use

CMSC 433, Fall 2002 26

Generalizing Singleton: Typesafe Enum

  • Problem:
    • Need a number of unique objects, not just one
    • Basically want a C-style enumerated type, but safe
  • Solution:
    • Generalize the Singleton Pattern to keep track of multiple, unique objects (rather than just one)

CMSC 433, Fall 2002 27

Typesafe Enum Pattern

Enum static Enum inst static Enum inst EnumOp () (^) Enum

Enum EnumOp ()

EnumOp ()

data

data

data

Note: constructor is private

CMSC 433, Fall 2002 28

Typesafe Enum: Example

public class Suit { private final String name; private Suit(String name) { this.name = name; } public String toString() { return name; } public static final Suit CLUBS = new Suit(“clubs”); public static final Suit DIAMONDS = new Suit(“diamonds”); public static final Suit HEARTS = new Suit(“hearts”); public static final Suit SPADES = new Suit(“spades”); }

CMSC 433, Fall 2002 31

Proxy Pattern Motivation

  • Goal:
    • Prevent an object from being accessed directly by its clients
  • Solution:
    • Use an additional object, called a proxy
    • Clients access to protected object only through proxy
    • Proxy keeps track of status and/or location of protected object

CMSC 433, Fall 2002 32

Uses of the Proxy Pattern

  • Virtual proxy : impose a lazy creation semantics, to avoid expensive object creations when strictly unnecessary.
  • Monitor proxy : impose security constraints on the original object, say by making some public fields inaccessible.
  • Remote proxy : hide the fact that an object resides on a remote location; e.g. the RemoteLogClient is essentially a remote proxy for a LocalLog.

CMSC 433, Fall 2002 33

Proxy Pattern Class Diagram

CMSC 433, Fall 2002 34

Object Diagram

CMSC 433, Fall 2002 37

Structure

CMSC 433, Fall 2002 38

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 39

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 40

Observer pattern