Design Patterns Learning Goals Understand what are design patterns, - - PowerPoint PPT Presentation

▶
design patterns learning goals
SMART_READER_LITE
LIVE PREVIEW

Design Patterns Learning Goals Understand what are design patterns, - - PowerPoint PPT Presentation

CPSC 310 Software Engineering Lecture 11 Design Patterns Learning Goals Understand what are design patterns, their benefits and their drawbacks For at least the following design patterns: Singleton, Observer, Adapter, you will be


slide-1
SLIDE 1

CPSC 310 – Software Engineering

Lecture 11

Design Patterns

slide-2
SLIDE 2

2/26

Learning Goals

  • Understand what are design patterns, their

benefits and their drawbacks

  • For at least the following design patterns:

Singleton, Observer, Adapter, you will be able to describe them, know when to use them or not and give examples of situations where you could use them.

slide-3
SLIDE 3

3/26

A bit of history

”Each pattern describes a problem which

  • ccurs over and over again in our

environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice” “ A pattern expresses a relation between a certain context, a problem and a solution”

Christopher Alexander

slide-4
SLIDE 4

4/26

A bit of history, continued

The “Gang of Four” - GoF

image designed by Jessica Lock from the Noun Project

Gamma, E., Helm, R., Johnson, R., & Vlissides, J. (1994). Design patterns: elements of reusable object-oriented software OOP World

slide-5
SLIDE 5

5/26

T

  • be, or not to be

A Design Pattern IS:

– a way to benefit from the collective experience of skilled

software developers

– an easy way to communicate about common problems

A Design Pattern IS NOT:

– the complete solution to your problem – the only solution to your problem (but it's a proven one) – something you should use if you do not understand it

slide-6
SLIDE 6

6/26

Creational Patterns How an object can be created Structural Patterns How objects can be composed Behavioral Patterns How objects communicate

Pattern Classification

slide-7
SLIDE 7

7/26

Singleton pattern

Intent: Make sure a class has only one instance, and provide a global point of access to it Participants & Structure:

Singleton

  • instance : Singleton
  • Singleton()

+ getInstance() : Singleton

slide-8
SLIDE 8

8/26

Singleton pattern

public class Singleton { private static Singleton instance = null; private Singleton() { } public static synchronized Singleton getInstance() { if (instance == null) { instance = new Singleton(); } return instance; } }

slide-9
SLIDE 9

9/26

Singleton pattern

public class Singleton { private static Singleton instance = null; private Singleton() { } public static synchronized Singleton getInstance() { if (instance == null) { instance = new Singleton(); } return instance; } }

slide-10
SLIDE 10

10/26

Creational Patterns How an object can be created Structural Patterns How objects can be composed Behavioral Patterns How objects communicate

Singleton pattern

T

  • which

category belongs this pattern ?

slide-11
SLIDE 11

11/26

Observer pattern

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

slide-12
SLIDE 12

12/26

Observer pattern [example]

4 8 15 16 Auctioneer (Subject) Bidders (Observers)

slide-13
SLIDE 13

13/26

Observer pattern [example]

4 8 15 16 Auctioneer (Subject) Bidders (Observers) regist ster bidders

slide-14
SLIDE 14

14/26

Observer pattern [example]

4 8 15 16 Auctioneer (Subject) Bidders (Observers)

  • 2. notify new bid
  • 1. accept new bid
slide-15
SLIDE 15

15/26

Observer pattern [example]

4 8 15 16 Auctioneer (Subject) Bidders (Observers) retrieve new price

slide-16
SLIDE 16

16/26

Creational Patterns How an object can be created Structural Patterns How objects can be composed Behavioral Patterns How objects communicate

Observer pattern

T

  • which

category belongs this pattern ?

slide-17
SLIDE 17

17/26

Adapter pattern

Intent: Convert the interface of a class into another interface that clients expect. Participants & Structure:

slide-18
SLIDE 18

18/26

Adapter pattern

Power supply adapter analogy

slide-19
SLIDE 19

19/26

Adapter pattern

Concrete example

slide-20
SLIDE 20

20/26

Creational Patterns How an object can be created Structural Patterns How objects can be composed Behavioral Patterns How objects communicate

Adapter pattern

T

  • which

category belongs this pattern ?

slide-21
SLIDE 21

21/26

Design Pattern Collection

http://sourcemaking.com/design_patterns

Factory Singleton Decorator Proxy Template Composite Adapter Observer

slide-22
SLIDE 22

22/26

Anti Pattern

  • A bad solution to a

recurring problem

  • "Strong" code smell
  • A good pattern in the

wrong context can lead to an anti-pattern

slide-23
SLIDE 23

23/26

Anti Pattern

  • Several categories

– Development – Architecture – Management

http://c2.com/cgi/wiki?AntiPatternsCatalog http://sourcemaking.com/antipatterns

slide-24
SLIDE 24

24/26

Eg: Golden Hammer

  • Problem: You need to choose technologies for your development, and you are of the belief that you must choose

exactly one technology to dominate the architecture.

  • Context: You need to develop some new system or piece of software that doesn't fit very well the technology that

the development team are familiar with.

  • Forces:

–

The development team are committed to the technology they know

–

The development team are not familiar with other technologies

–

Other, unfamiliar, technologies are seen as risky

–

It is easy to plan and estimate for development in the familiar technology

  • Suppo

posed Solu lutio ion: Use the familiar technology anyway. The technology (or concept) is applied obsessively to many problems, including where it is clearly inappropriate.

  • Refactor
  • red

d Solu lution

  • n: Expanding the knowledge of developers through education, training, and book study groups

exposes developers to new solutions.

http://sourcemaking.com/antipatterns/golden-hammer

slide-25
SLIDE 25

25/26

  • Eg. Singleton Overuse
  • Problems

– violate information hiding since dependencies are

hidden in the code and not expressed in the interface

– can cause high coupling

  • You must have a good damn reason to use it

– The fact that you know it is not enough

public void someMethod() ... Profile.getInstance().getUserLevel() public void someMethod(Profile profile) ... profile.getUserLevel()

slide-26
SLIDE 26

26/26

Design Pattern Drawbacks

  • Can make the design more complex if not needed

– Start simple and then refactor by using a design

pattern if it is justified

– Do not try to anticipate future needs too much

  • Can lead to bad design if not applied in the right

context