0% found this document useful (0 votes)
30 views

Design Patterns

The document discusses several design patterns including Strategy, Observer, Decorator, Factory, Singleton, Command, Facade, Template Method, and Iterator. It provides examples of when and how each pattern can be used, often using code snippets from the Java library, to make classes more flexible and reusable. The goals of design patterns are to solve common programming problems and provide proven, reusable solutions so that code can be written in a clear and structured way that facilitates change and reuse.

Uploaded by

test
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

Design Patterns

The document discusses several design patterns including Strategy, Observer, Decorator, Factory, Singleton, Command, Facade, Template Method, and Iterator. It provides examples of when and how each pattern can be used, often using code snippets from the Java library, to make classes more flexible and reusable. The goals of design patterns are to solve common programming problems and provide proven, reusable solutions so that code can be written in a clear and structured way that facilitates change and reuse.

Uploaded by

test
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 57

Design Patterns -

Introduction
The Why
Software
A

//if (int x = 0;..)


Software
… programming Languages
B

Software
C
How do I make
“Way of Thinking” my classes more
flexible?
About Software Design (Code)

GamePlay

Scoreboard

Balloon Clown SquareBalloon


Design Patterns
Proven and Tested “ways of thinking” about Code Design

Facade
Adapter
Factory
Decorator

Observer
Strategy
Singleton
The Gang of Four
The Creators of Design Patterns
The Strategy Design
Pattern
Algorithms for Different Cases…

ScoreBoard
calculateScore()
showScore()

Balloon Clown SquareBalloon


calculateScore() calculateScore() calculateScore()

+20 points +30 points +45 points


Class Hierarchy

JDK
Example # 2

JDK
The Observer Design
Pattern
Observer Design Pattern

… defines a 1-to-many
dependency between objects
so that when one object changes
state, all of its dependents are
notified and updated automatically
Observer Pattern Example

JDK
Problem with Current State…
Project grows - more JAR Files
JDK
More JAR files - more class path
issues

JAR Files

Classpath JAR File


Java —*packages
Project —Classes
— Interfaces
— Metadata
The Decorator
Pattern
Ice-cream Shop

JDK
Ice-cream Shop

JDK
Problem…

JDK
Solution

JDK
The Result…

Chocolate
cost() cost() will give us the final total cost

Vanilla
cost()

We wrap each ice-cream type with other


Ice-Cream toppings or types
cost()
The Factory Pattern*

*Introducing the Simple Factory “Pattern”


HamburgerStore

JDK
We need more than 1 type of burger…
Solution - Simple Factory

JDK
Problem solved… (For now)

JDK
Solution - Class Diagram

JDK
The Factory Pattern*

*The real thing!


Franchise our Store!

MozHamburgerFactory
JDK

HamburgerStore

JamHamburgerFactory
Create our Franchises…

MozHamburgerFactory

JDK

HamburgerStore

JamHamburgerFactory
The Singleton Pattern
Object Creation

JDK

CheeseBurger cheeseBurger = new CheeseBurger();

Calling
CheeseBurger()
public constructor
object
1 object object3
2

object object
4 …
Motivation: Too Many Object…

JDK

Sometimes we only need ONE instance of an object.

Solution

“Ensures a class has only one instance,


Singleton and provide a global point of access to it.”
The Command
Pattern
Deconstructing the System

Game Console Controllers


Game card
JDK

Game Console
The Command Pattern System

Commands (jump, run, left, right…)


Request
JDK

Invoker

Receivers - game characters will jump, run… etc


The Command Pattern Diagram

JDK
Definition…

JDK

The Command Pattern:


encapsulates a request as an object ( the game card), thereby letting you parameterize other
objects with different requests, queues or log requests.
The Facade Pattern
Real-World Example

Computer

CPU Hardrive Memory

Monitor Mouse ….

Too complex….
UML Facade Diagram
Definition…

The Facade Pattern:


provides a unified interface
to a set of interfaces in a subsystem.
Facade defines a higher-level interface
that makes the subsystem easier to use.
Facade - Definition
Motivation…

JDK

Redundant…..NOT Good!
Solution…

JDK
The Template Method
Pattern
Definition…

The Template Method Pattern:


defines the skeleton of an algorithm in a method,
deferring some steps to subclasses. It lets subclasses
redefine certain steps of an algorithm without changing
the algorithms’s structure.
Real-World Example

Computer

CPU Hardrive Memory

Monitor Mouse ….

Too complex….
UML Facade Diagram
Facade - Definition
Definition…

JDK

The Command Pattern:


encapsulates a request as an object ( the game card), thereby letting you parameterize other
objects with different requests, queues or log requests.
The Iterator Pattern
Motivation

Geeky Store Dev Store

Inventory/Products Inventory/Products

ArrayList Array
Problem

Geeky Store Dev Store

Inventory/Products
Inventory/Products

ArrayList Array
Iterating through Products…

Geeky Store Dev Store

Inventory/Products
Inventory/Products

ArrayList Array
Obvious Solution

Two different catalogs …

Geeky Inventory/ Dev Inventory/


Products Products

for loop 1 … for loop 2 …

for loop 3 … etc


The Solution

Iterator

… to encapsulate the way we iterate through a collection of objects


Definition…

JDK

The Iterator Pattern:


provides a way to access the elements of an aggregate object sequentially without exposing
its underlying representation.
Create our Franchises…

JDK
The Result…

Chocolate
cost() cost() will give us the final total cost

Vanilla
cost()

We wrap each ice-cream type with other


Ice-Cream toppings or types
cost()

You might also like