SDSU CS 635 Advanced Object-Oriented Design & Programming
Spring Semester, 2004
Design Pattern Intro
Previous    Lecture Notes Index    Next    
© 2004, All Rights Reserved, SDSU & Roger Whitney
San Diego State University -- This page last updated 22-Jan-04


References

Patterns for Classroom Education, Dana Anthony, pp. 391-406, Pattern Languages of Program Design 2 , Addison Wesley, 1996

A Pattern Language , Christopher Alexander, 1977

Software Patterns, James Coplien, 1996, 2000, http://www1.bell-labs.com/user/cope/Patterns/WhitePaper/

Design Patterns: Elements of Reusable Object-Oriented Software, Gamma, Helm, Johnson, Vlissides, 1995

Reading

Design Patterns chapter 1.


Doc 2, Design Pattern Intro Slide # 2

Design Patterns

What is a Pattern?

"Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice"

"Each pattern is a three-part rule, which expresses a relation between a certain context, a problem, and a solution"

Christopher Alexander on architecture patterns


"Patterns are not a complete design method; they capture important practices of existing methods and practices uncodified by conventional methods"

James Coplien


Doc 2, Design Pattern Intro Slide # 3

Examples of Patterns

A Place To Wait [1]


The process of waiting has inherent conflicts in it.

Waiting for doctor, airplane etc. requires spending time hanging around doing nothing

Can not enjoy the time since you do not know when you must leave

Classic "waiting room"

Fundamental problem

Fuse the waiting with other activity that keeps them in earshot

Allow the person to become still meditative


Doc 2, Design Pattern Intro Slide # 4
A Place To Wait
Therefore:

"In places where people end up waiting create a situation which makes the waiting positive. Fuse the waiting with some other activity - newspaper, coffee, pool tables, horseshoes; something which draws people in who are not simple waiting. And also the opposite: make a place which can draw a person waiting into a reverie; quiet; a positive silence"

Doc 2, Design Pattern Intro Slide # 5

Chicken And Egg [2]


Problem

Two concepts are each a prerequisite of the other

To understand A one must understand B

To understand B one must understand A

A "chicken and egg" situation

Constraints and Forces

First explain A then B


Simplify each concept to the point of incorrectness to explain the other one


Solution

Explain A & B correctly by superficially

Iterate your explanations with more detail each iteration

Doc 2, Design Pattern Intro Slide # 6

Benefits of Software Patterns


By providing domain expertise patterns



Patterns reduce time to design applications



Patterns reduce the time needed to understand a design


Doc 2, Design Pattern Intro Slide # 7

Common Forms For Writing Design Patterns


Alexander - Originated pattern literature

GOF (Gang of Four) - Style used in Design Patterns text

Portland Form -Form used in on-line Portland Pattern Repository
http://c2.com/cgi/wiki?PortlandPatternRepository
Coplien


Doc 2, Design Pattern Intro Slide # 8

Design Principle 1


Program to an interface, not an implementation

Use abstract classes (and/or interfaces in Java) to define common interfaces for a set of classes

Declare variables to be instances of the abstract class not instances of particular classes

Benefits of programming to an interface

Client classes/objects remain unaware of the classes of objects they use, as long as the objects adhere to the interface the client expects

Client classes/objects remain unaware of the classes that implement these objects. Clients only know about the abstract classes (or interfaces) that define the interface.


Doc 2, Design Pattern Intro Slide # 9
Programming to an InterfaceJava Collections



Collection students = new XXX;
students.add( aStudent);


students can be any collection type

We can change our mind on what type to use

Doc 2, Design Pattern Intro Slide # 10

Design Principle 2


Favor object composition over class inheritance

Composition



Inheritance

class A {
   Foo x
   public int complexOperation() {   blah }
}
class B extends A {
   public void bar() { blah}
}
Composition

class B {
   A myA;
   public int complexOperation() {
      return myA.complexOperation()
   }
   public void bar() { blah}
}


Doc 2, Design Pattern Intro Slide # 11
Parameterized Types

Generics in Ada, Eiffel, Java (jdk 1.5)
Templates in C++

Allows you to make a type as a parameter to a method or class

template <class TypeX>
TypeX min(  TypeX a, Type b )
   {
   return a < b ? a  :  b;
   } 


Parameterized types give a third way to compose behavior in an object-oriented system


Doc 2, Design Pattern Intro Slide # 12
Designing for Change

Some common design problems that GoF patterns that address

• Creating an object by specifying a class explicitly

   Abstract factory, Factory Method, Prototype


   Chain of Responsibility, Command


   Abstract factory, Bridge


   Abstract factory, Bridge, Memento, Proxy


   Builder, Iterator, Strategy, Template Method, Visitor


   Abstract factory, Bridge, Chain of Responsibility, 
   Command, Facade, Mediator, Observer


   Bridge, Chain of Responsibility, Composite, 
   Decorator, Observer, Strategy


   Adapter, Decorator, Visitor

Doc 2, Design Pattern Intro Slide # 13
[1] Alexander 1977, pp. 707-711
[2] Anthony 1996

Copyright ©, All rights reserved.
2004 SDSU & Roger Whitney, 5500 Campanile Drive, San Diego, CA 92182-7700 USA.
OpenContent license defines the copyright on this document.

Previous    visitors since 22-Jan-04    Next