Docsity

Components of Pattern - Programming Language Technologies and Paradigms | CMSC 433, Study notes of Programming Languages

Partial preview of the text

Download Components of Pattern - Programming Language Technologies and Paradigms | CMSC 433 and more Study notes Programming Languages in PDF only on Docsity!

26

CMSC 433 – Programming Language

Technologies and Paradigms

Spring 2003

Design Patterns September 30, 2003

Components of a Pattern

  • Name(s)
  • Problem
    • Context
    • Real-world example
  • Solution
    • Design/structure
    • Implementation
  • Consequences
  • Variations, known uses

28

Review: Iterator Pattern

  • Name : Iterator ( aka Cursor)
  • Problem :
    • How to process the elements of an aggregate in an implementation-independent manner?
  • Solution :
    • Define an Iterator interface
      • next(), hasNext(), etc. methods
    • Aggregate returns an instance of an implementation of Iterator interface to control the iteration

Iterator Pattern

  • Consequences :
    • support different and simultaneous traversals
      • Multiple implementations of Iterator interface
      • one traversal per Iterator instance
    • requires coherent policy on aggregate updates
      • Invalidate Iterator by throwing an exception, or
      • Iterator only considers elements present at the time of its creation
  • Variations :
    • internal vs. external iteration
      • Java Iterator is external

32

Underlying Principles

  • Reduce implementation dependencies between elements of a software system
  • Sub-goals:
    • Program to an interface, not an implementation
    • Favor composition over inheritance
    • Use delegation

Program to Interface, Not Implementation

  • Rely on abstract classes and interfaces to hide differences between subclasses from clients - interface defines an object’s use (protocol) - implementation defines particular policy
  • Example : Iterator interface, compared to its implementation for a LinkedList

34

Rationale

  • Decouples clients from the implementations of the applications they use
  • When clients manipulate an interface, they remain unaware of the specific object types being used.
  • Therefore: clients are less dependent on an implementation, so it can be easily changed later.

Favor Composition over Class Inheritance

  • White box reuse:
    • Inheritance
  • Black box reuse:
    • Composition

AClass amethod

BClass amethod

BClass amethod

AClass amethod

aVar

38

Rationale

  • Object relationships dynamic
    • composes or re-composes behavior at run-time
  • But:
    • sometimes code harder to read and understand
    • efficiency (because of black-box reuse)

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.

40

Catalogue of Patterns: 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.

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

44

The Singleton solution

  • Class is responsible for tracking its sole instance
    • Make constructor private
    • Provide static method/field to allow access to the only instance of the class
  • Benefit:
    • Reuse implies better performance
    • Class encapsulates code to ensure reuse of the object; no need to burden client

Singleton pattern

46

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 static Singleton getInstance() { return instance; } }
  • Java semantics guarantee object is created immediately before first use

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)

50

Adapter Pattern

  • Problem:
    • You have some code you want to use for a program
    • You can’t incorporate the code directly (e.g. you just have the .class file, say as part of a library)
    • The code does not have the interface you want
      • Different method names
      • More or fewer methods than you need
  • To use this code, you must adapt it to your situation

Adapter Pattern

  • Solution: adapter class to implement client’s expected interface, forwarding methods

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.