







































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An overview of various design patterns, including Singleton, Decorator, Facade, and Flyweight. Students will learn about the problem each pattern solves, how it works, and how to implement it. The document also includes examples and exercises to help reinforce the concepts.
Typology: Lecture notes
1 / 47
This page cannot be seen from the preview
Don't miss anything!








































CSE 403, Spring 2008, Alverson With material from Marty Stepp 403 lectures.
y
a good problem decomposition o^
a design with flexibility, modularity and elegance o^
a design with flexibility, modularity and elegance
y
y
y
two designs they are almost never identical o^
they exhibit some recurring characteristics
CSE 403, Spring 2008, Alverson
y
y
CSE 403, Spring 2008, Alverson
y
(abstract object instantiation)
y
(combine objects)
y
(communication btwn objects)
CSE 403, Spring 2008, Alverson
y
We'd like to make it illegal to have more than one o^
Examples: keyboard reader, printer spooler, gradebook
y
Creating lots of objects can take a lot of time o^
Creating lots of objects can take a lot of time o^
Extra objects take up memory o^
It is a maintenance headache to deal with differentobjects floating around if they are the same
CSE 403, Spring 2008, Alverson
y
Ensures that a class has at most one instance o^
Provides a global access point to that instance o^
Takes responsibility of managing that instance awayfrom the programmer (illegal to construct moreinstances)instances) o^
Provide accessor method that allows users to see the(one and only) instance(one and only) instance o^
Possibly the most known / popular design pattern!
CSE 403, Spring 2008, Alverson
Implementing singleton (oneinstantiation of the pattern…) y
instantiation
of the pattern…)
y
y
possibly protect / synchronize this method to ensure o^
possibly protect / synchronize this method to ensurethat it will work in a multi-threaded program
CSE 403, Spring 2008, Alverson
CSE 403, Spring 2008, Alverson
public class RandomGenerator {
private static RandomGenerator gen = null;public static RandomGenerator getInstance() {
if (gen == null) {
gen = new RandomGenerator(); }} return gen; } ... } y
y CSE 403, Spring 2008, Alverson
public class RandomGenerator {
private static RandomGenerator gen = null;public static synchronized RandomGenerator
getInstance() {
if (gen == null) {
gen = new RandomGenerator();gen = new RandomGenerator(); } return gen; } ... } y CSE 403, Spring 2008, Alverson
y
CSE 403, Spring 2008, Alverson
CSE 403, Spring 2008, Alverson
Client
Factory
Createme an
I’llbaseit on
Client
Factory
InputReader
thetypeof input
newobject 1
Cre
Us
input
newobject 2
eates
es
newobject 3
CSE 403, Spring 2008, Alverson
Client
Factory
Createme an
I’llbaseit on
Client
Factory
inputReader
thetypeof input
newobject 1
Cre
Us
input
newobject 2
eates
es
newobject 3
Objects should either make
CSE 403, Spring 2008, Alverson
Objects
should either make
other objects or use otherobjects but never both.