Docsity

Design Patterns: Understanding and Implementing Object-Oriented Solutions - Lecture Notes , Study notes of Computer Science

Toggle sidebar
bg1
Lecture 23: Design Patterns
Kenneth M. Anderson
Object-Oriented Analysis and Design
CSCI 6448 - Spring Semester, 2002
April 9, 2002 Ā© Kenneth M. Anderson, 2002 2
Last Lecture
•Database Design
–Data Models
•Relational, Object-Relational, Object
–Object Mapping
•OO concepts to (potentially) non-OO concepts
April 9, 2002 Ā© Kenneth M. Anderson, 2002 3
Quiz
•Is it possible to determine if an individual
class (developed during analysis and
extended during design) is a member of a
particular component?
•What ā€œrouteā€ through analysis and design
models would you have to take in order to
answer this question?
April 9, 2002 Ā© Kenneth M. Anderson, 2002 4
Goals of Lecture
•Cover Design Patterns
–Background and Core Concepts
–Examples
•Plus Implementations
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download Design Patterns: Understanding and Implementing Object-Oriented Solutions - Lecture Notes and more Study notes Computer Science in PDF only on Docsity!

Lecture 23: Design Patterns

Kenneth M. Anderson

CSCI 6448 - Spring Semester, 2002Object-Oriented Analysis and Design

April 9, 2002 Ā© Kenneth M. Anderson, 2002

Last Lecture

• Database Design

– Data Models

• Relational, Object-Relational, Object

– Object Mapping

• OO concepts to (potentially) non-OO concepts

April 9, 2002 Ā© Kenneth M. Anderson, 2002 3

Quiz

• Is it possible to determine if an individual

particular component?extended during design) is a member of aclass (developed during analysis and

• What ā€œrouteā€ through analysis and design

answer this question?models would you have to take in order to

April 9, 2002 Ā© Kenneth M. Anderson, 2002

Goals of Lecture

• Cover Design Patterns

– Examples– Background and Core Concepts

• Plus Implementations

April 9, 2002 Ā© Kenneth M. Anderson, 2002 5

Pattern Resources

• Pattern Languages of Programming

– Technical conference on Patterns

• The Portland Pattern Repository

– http://c2.com/ppr/

• Patterns Homepage

– Go to page then click on ā€œPatterns tabā€ā€“ http://hillside.net/

April 9, 2002 Ā© Kenneth M. Anderson, 2002

Design Patterns

– Addison-Wesley book published in 1995

Erich Gamma

Richard Helm

Ralph Johnson

John Vlissides

ISBN 0-201-63361-

Known as ā€œThe Gang of Fourā€

Presents 23 Design Patterns

Publishing Companybook, and is thus copyright Ā© 1995 by Addison-WesleyMaterial in this lecture and lecture 24 is drawn from this

April 9, 2002 Ā© Kenneth M. Anderson, 2002 7

What are Patterns?

– Christopher Alexander talking about buildings and towns

twiceā€solution a million times over, without ever doing it the same waysolution to that problem, in such a way that you can use thisagain in our environment, and then describes the core of theā€œEach pattern describes a problem which occurs over and over

1977 Alexander, et al., A Pattern Language. Oxford University Press,

April 9, 2002 Ā© Kenneth M. Anderson, 2002

Patterns, continued

Patterns can have different levels of abstraction

– Instead, Patterns are descriptions of communicating– Patterns are not frameworks– Patterns are not classesIn Design Patterns (the book),

general design problem in a particular contextobjects and classes that are customized to solve a

April 9, 2002 Ā© Kenneth M. Anderson, 2002 13

Design Pattern Template

– ClassificationPattern Name and

Creational

Structural

Behavioral

Intent

Also Known As

Motivation

Applicability

Structure

Participants

Collaborations

Consequences

Implementation

Sample Code

Known Uses

Related Patterns

April 9, 2002 Ā© Kenneth M. Anderson, 2002

Examples

• Adapter• Factory Method• Singleton

April 9, 2002 Ā© Kenneth M. Anderson, 2002 15

Singleton

• Intent

– Ensure a class has only one instance, and

provide a global point of access to it

• Motivation

– Some classes represent objects where multiple

security risk (e.g. Java security managers)instances do not make sense or can lead to a

April 9, 2002 Ā© Kenneth M. Anderson, 2002

Singleton, continued

• Applicability

– Use the Singleton pattern when

• there must be exactly one instance of a class, and it

access pointmust be accessible to clients from a well-known

• when the sole instance should be extensible by

extended instance without modifying their codesubclassing, and clients should be able to use an

April 9, 2002 Ā© Kenneth M. Anderson, 2002 17

Singleton Structure

static Instance() Singleton

{return uniqueInstance}

private singletonDataprivate static uniqueInstancepublic GetSingletonData() public SingletonOperation()

April 9, 2002 Ā© Kenneth M. Anderson, 2002

Singleton, continued

– Just the Singleton classParticipants

– Clients access a Singleton instance solely throughCollaborations

Singleton’s Instance operation

– Permits a variable number of instances (if desired)– Reduced name space (versus global variables)– Controlled access to sole instanceConsequences

April 9, 2002 Ā© Kenneth M. Anderson, 2002 19

Implementation

import (^) java.util.Date; public (^) class (^) Singleton (^) { private (^) static (^) Singleton (^) theOnlyOne; private (^) Date (^) d (^) = (^) new (^) Date(); public} private Singleton() { (^) static (^) Singleton (^) instance() { if (^) (theOnlyOne == (^) null) (^) { theOnlyOne = (^) new (^) Singleton(); return} (^) theOnlyOne; public} (^) Date (^) getDate() { return d;

April 9, 2002 Ā© Kenneth M. Anderson, 2002

Using our Singleton Class

public (^) class (^) useSingleton { public (^) static (^) void (^) main(String[] args) (^) { Singleton a (^) = (^) Singleton.instance(); Singleton b (^) = (^) Singleton.instance(); System.out.println("" + (^) a.getDate()); System.out.println("" + (^) b.getDate()); System.out.println("" + (^) a); System.out.println("" + (^) b);

Singleton@136646Singleton@136646Sun Apr 07 13:03:34 MDT 2002Sun Apr 07 13:03:34 MDT 2002Output:}

April 9, 2002 Ā© Kenneth M. Anderson, 2002 25

Factory Method, continued

– ProductParticipants

  • Defines the interface of objects the factory method creates

– Concrete Product

  • Implements the Product Interface

– Creator

  • declares the Factory method which returns an object of type

Product

– Concrete Creator

  • overrides the factory method to return an instance of a

Concrete Product

April 9, 2002 Ā© Kenneth M. Anderson, 2002

Factory Method Structure

Product

ConcreteProduct

Creator

FactoryMethod()^ ConcreteCreator

AnOperation() FactoryMethod()

«instantiate»

product = FactoryMethod()

return new ConcreteProduct()

April 9, 2002 Ā© Kenneth M. Anderson, 2002 27

Factory Method Consequences

application-specific classes into your codeFactory methods eliminate the need to bind

– In single-inherited systems, this constrains yourConcreteProductsubclassing in order to create a particularPotential disadvantage is that clients must use

partitioning choices

Provides hooks for subclasses

– See page 110 of the design patterns bookConnects parallel class hierarchies

April 9, 2002 Ā© Kenneth M. Anderson, 2002

Implementation

• See code example (available on class

website)

• A factory can return balloons of different

– The factory hides several specific creators andcolors

cycles among them to create balloons

• A client retrieves multiple balloons and

displays their colors

April 9, 2002 Ā© Kenneth M. Anderson, 2002 29

Adapter

– Intent

because of incompatible interfacesexpect. Adapter lets classes work together that could not otherwiseConvert the interface of a class into another interface clients

– Also Known As

Wrapper

– Motivation

an application requiresbecause its interface does not match the domain-specific interfaceSometimes a toolkit class that is designed for reuse is not reusable

Page 139-140 of Design Patterns provides an example

April 9, 2002 Ā© Kenneth M. Anderson, 2002

Adapter, continued

• Applicability

– Use the Adapter pattern when

• you want to use an existing class, and its interface

does not match the one you need

• you want to create a reusable class that cooperates

with unrelated or unforeseen classes

April 9, 2002 Ā© Kenneth M. Anderson, 2002 31

Adapter, continued

– TargetParticipants

  • defines the domain-specific interface that Client uses

– Client

  • collaborates with objects conforming to the Target interface

– Adaptee

  • defines an existing interface that needs adapting

– Adapter

  • adapts the interface of Adaptee to the Target interface April 9, 2002 Ā© Kenneth M. Anderson, 2002

Adapter Structure

Client

Target

Request()

Adaptee

SpecificRequest()

Request() Adapter

Class Adapter

SpecificRequest()