Understanding and Implementing Design Patterns: Singleton, Decorator, Facade, Flyweight, Lecture notes of Design Patterns

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

2021/2022

Uploaded on 08/05/2022

char_s67
char_s67 🇱🇺

4.5

(116)

1.9K documents

1 / 47

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Design Patterns
CSE 403, Spring 2008, Alverson
With material from Marty Stepp 403 lectures.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f

Partial preview of the text

Download Understanding and Implementing Design Patterns: Singleton, Decorator, Facade, Flyweight and more Lecture notes Design Patterns in PDF only on Docsity!

Design Patterns

CSE 403, Spring 2008, Alverson With material from Marty Stepp 403 lectures.

Design challenges

g

g

y

Designing software is hard! One must find:o

a good problem decomposition o^

a design with flexibility, modularity and elegance o^

a design with flexibility, modularity and elegance

y

Designs often emerge from trial and error

y

Successful designs do exist

y

Successful designs do existo

two designs they are almost never identical o^

they exhibit some recurring characteristics

CSE 403, Spring 2008, Alverson

Online Readings

g

My latest favorite survey of common patterns:

y

My

latest favorite survey of common patterns:

http://sourcemaking.com/design_patterns

y

[Optional] See the “References” link on the classweb page for a number of others

CSE 403, Spring 2008, Alverson

Gang of Four (GoF) patterns

g

) p

y

Creational Patterns

(abstract object instantiation)

Ab t

t F

t^

F

t^

B ild

P

t t

Abstract Factory, Factory, Builder, PrototypeSingleton

y

Structural Patterns

(combine objects)

Adapter, Bridge, Composite, Decorator, Façade,Flyweight ProxyFlyweight, Proxy

y

Behavioral Patterns

(communication btwn objects)

Chain of responsibility, Command, Interpreter,Iterator, Mediator, Memento, Observer, State,Strategy Template Method Visitor

CSE 403, Spring 2008, Alverson

Strategy

, Template Method, Visitor

Restricting object creation

g

j

y

Problem: Sometimes we will really only ever needone instance of a particular classone instance of a particular class.o

We'd like to make it illegal to have more than one o^

Examples: keyboard reader, printer spooler, gradebook

y

Why we care:

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

Singleton pattern

g

p

y

singleton: an object that is the only object of itstypetypeo

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

Make constructor(s)

private

so that they can

not be called from outside

instantiation

of the pattern…)

not

be called from outside

y

Declare a single

s

tatic private

instance of

g

p

the classWrite a public

tI

t

or similar

y

Write

a public

g

etInstance()

or

similar

method that allows access to the single instanceo

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

Singleton sequence diagram

g

q

g

CSE 403, Spring 2008, Alverson

Singleton example 2 y

Variation: don't create the instance until needed

g

p

public class RandomGenerator {

private static RandomGenerator gen = null;public static RandomGenerator getInstance() {

if (gen == null) {

gen = new RandomGenerator(); }} return gen; } ... } y

What could go wrong with this version?

y CSE 403, Spring 2008, Alverson

What

could go wrong with this version?

Singleton example 3 y

Variation: solve concurrency issue by locking

g

p

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

Is anything wrong with this version?

Singleton exercise

g

y

Consider your projects. What classes could bea singleton in this system?a singleton in this system?

CSE 403, Spring 2008, Alverson

Pattern: Factory

(a variation of Factory Method Abstract(a variation of Factory Method, Abstract

Factory)

a class or method used to

create objects easily

CSE 403, Spring 2008, Alverson

Separate creation from use

p

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

Separate creation from use

p

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.