CS 635 Advanced Object-Oriented Design & Programming Spring Semester, 2004 Design Pattern Intro |
||
---|---|---|
© 2004, All Rights Reserved, SDSU & Roger Whitney San Diego State University -- This page last updated 22-Jan-04 |
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"
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"
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"
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
Benefits of Software Patterns
By providing domain expertise patterns
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
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.
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
Design Principle 2
Favor object composition over class inheritance
Composition
class A { Foo x public int complexOperation() { blah } } class B extends A { public void bar() { blah} }
class B { A myA; public int complexOperation() { return myA.complexOperation() } public void bar() { blah} }
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
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
[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