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

7 Design Patterns

The document provides an overview of design patterns, including their definition, components, classifications, and examples. Specifically, it discusses the Observer pattern in more detail, describing its intent to notify dependents when an object changes state, the participating classes of subject and observers, how observers attach and update from the subject, and examples of its use.

Uploaded by

morphyjack
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
97 views

7 Design Patterns

The document provides an overview of design patterns, including their definition, components, classifications, and examples. Specifically, it discusses the Observer pattern in more detail, describing its intent to notify dependents when an object changes state, the participating classes of subject and observers, how observers attach and update from the subject, and examples of its use.

Uploaded by

morphyjack
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Design Patterns

Meghan Allen

Design Patterns Overview


Introduction to design patterns How to use design patterns Components of a pattern Various patterns Creational Structural Behavioral Integrating Patterns
2

Learning Goals
By the end of this unit, you will be able to:

Explain why design patterns are useful and some caveats to consider when using design patterns Clearly and concisely describe, give examples of software situations in which youd use, explain the key benefit of, and drawbacks or special considerations for the following patterns: abstract factory, singleton, faade, composite, decorator, observer
3

Software Updates

Design Challenges
Designing software with good modularity is hard! Designs often emerge from a lot of trial and error

Are there best solutions to common recurring problems?


5

Design Patterns
In software engineering, a design pattern is a general repeatable solution to a commonly occurring problem in software design.
A design pattern is a description or template for how to solve a problem (not a finished design) Patterns capture design expertise and allow that expertise to be transferred and reused Patterns provide common design vocabulary, improving communication, easing implementation & documentation

Non-software Pattern Examples

Problem: sink stinks Pattern: S-trap

Problem: highway crossing Pattern: clover leaf

Updates Observer Design Pattern


Name: Observer Intent: Ensure that, when an object changes state, all its dependents are notified and updated automatically. Participants & Structure:

Updates Observer Design Pattern

Client A Attach() Client B Software (subject) Client C Clients (Observers)

Updates Observer Design Pattern

Client A Update() Client B Software (subject) Client C Clients (Observers)

10

Updates Observer Design Pattern


O B S E R V E R

Client A

Update() Software (subject)

Client B

Client C Clients (Observers)

Use common Observer interface

11

Observer DP (contd)
I need the professor to be notified when a student joins his/her class I want the display to update when the size of a window is changed I need the schedule view to update when the database is changed Design patterns are reusable!
12

How to use Design Patterns?


Part Craft

Know the patterns Know the problem they can solve Recognize when a problem is solvable by a pattern Look up the pattern Correctly integrate it into your code
13

Part Art

Part Science

Knowing the patterns helps in understanding code


The pattern sometimes convey a lot of information Try understanding this code:

Key is to know the Abstract Factory and Decorator patterns!


14

Design patterns also provide a shared vocabulary.


Dev 1: I made a Broadcast class. It keeps track of all of its listeners and anytime it has new data it sends a message to each listener. The listeners can join the Broadcast at any time or remove themselves from the Broadcast. Its really dynamic and loosely-coupled! Dev 2: Why didnt you just say you were using the Observer pattern? (from Head First Design Patterns)
15

Components of a pattern
1. 2.

3.

4.

Pattern Name Intent What problem does it solve? Participants What classes participate These classes usually have very general names, the pattern is meant to be used in many situations! Structure How are the classes organized? How do they collaborate?
16

A Menagerie of Patterns!

17

Pattern Classifications
Creational Patterns Makes a system independent of how its objects are created Useful as system evolves: the classes that will be used in the future may not be known now Structural Patterns Techniques to compose objects to form larger structures Behavioral Patterns Concerned with communication between objects Describe complex control flow

18

Discussion Question
What is the Classification of the Observer pattern? A) Creational, B) Structural, C) Behavioural?

19

Behavioral Patterns
Mediator Observer Chain of Responsibility Command Interpreter Iterator Memento State Strategy Template Method Visitor
20

Observer Pattern
Consider your projects. What classes could be an observer or a subject?

21

You might also like