Top Banner
Welcome to Design Patterns 2011.11.18 Design Patterns Ngo Doan Lap
32

Welcome to Design patterns

Dec 14, 2014

Download

Education

Lap Doan

Short introduction about design patterns
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Welcome to Design patterns

Welcome to Design PatternsWelcome to Design Patterns

2011.11.18

Design Patterns

Ngo Doan Lap

Page 2: Welcome to Design patterns

ContentsContentsI. Introduce to design pattern through example

1. Problem

2. Issues

3. Solutions

II. Design patterns

III. Design patterns classification

Page 3: Welcome to Design patterns

ProblemProblem1. Simulate Duck application

SimUDuck

1. show a large variety of duck species swimming

2. making quacking sounds

Initial designSupper class

(abstract class)

Inherited classes

(concrete classes)

1

2

3 4

Page 4: Welcome to Design patterns

ProblemProblem

1

2

1. Upgrade the system

Duck can fly

Page 5: Welcome to Design patterns

IssuesIssues

1

23

Page 6: Welcome to Design patterns

Solution is …inheritance?Solution is …inheritance?Override fly() method to do nothing

1

2

No fly, no quack

Disadvantage of inheritance?

3

1. Code is duplicated across subclasses

2. Changes can unintentionally affect others

3. Hard to gain knowledge of all object behaviors

4. Runtime behavior change are difficult

Difficult to maintain

Difficult to maintain

Page 7: Welcome to Design patterns

Solution is …interface?Solution is …interface?1. Flyable() interface.

2. Spec will keep changing

3. Override fly() and quack()

What happen if you need to make a little change to the flying

Behavior….in all of 100 the flying Duck subclasses?

1

2

31. Interface solves part of the problem

2. Completely destroys code reuse for those behaviors

Page 8: Welcome to Design patterns

Solution is …CHANGESolution is …CHANGENo matter how well you design an application, over time an

application must grow and change or it will die.

StableChange

1

2

3

4

5

Page 9: Welcome to Design patterns

SolutionSolution1 fly() and quack() are the parts of the

Duck class that vary across ducks

Separate change and stable

2

How to design Duck behaviors?

3

Page 10: Welcome to Design patterns

What we want:1. Instantiate a particular object and initialize it with specific type of flying behavior2. Can change flying behavior at runtime.

1

2

~ API

use an interface to represent each behavior

3

4

Any behaviors changed will not affected to Duck class and it’s child classes

5

SolutionSolution

Page 11: Welcome to Design patterns

Program to an interface vs Program to an implementationProgram to an interface vs Program to an implementation

Program to an implementation

Program to an interface

assign the concrete implementation object at runtime

Have demo as quick as possibleDon’t care about extend/upgrade/maintain

Take time to design but it is flexible

Page 12: Welcome to Design patterns

Implementing the Duck BehaviorsImplementing the Duck Behaviorstwo interfaces: FlyBehavior and QuackBehavior

1 2

Page 13: Welcome to Design patterns

Implementing the Duck BehaviorsImplementing the Duck Behaviors

1

2

Easy to reuse for other objects (not duck)

Easy to extend/modify that make no side effect

Page 14: Welcome to Design patterns

Integrating the Duck BehaviorIntegrating the Duck BehaviorAdd two instance variables to the Duck class1

2 Implement performQuack()

don’t care what kind of object it is.Care abouthow to quack()!

Page 15: Welcome to Design patterns

flyBehavior and quackBehavior instance variables are set

Integrating the Duck BehaviorIntegrating the Duck Behavior3

3.1

3.2

3.3

Page 16: Welcome to Design patterns

TestingTesting

Page 17: Welcome to Design patterns

TestingTesting

Page 18: Welcome to Design patterns

TestingTesting

Page 19: Welcome to Design patterns

TestingTesting

Page 20: Welcome to Design patterns

Setting behavior dynamicallySetting behavior dynamically

Page 21: Welcome to Design patterns

TestingTesting

Page 22: Welcome to Design patterns
Page 23: Welcome to Design patterns

The Big PictureThe Big Picture

1

2

3

4

Strategy for upgrading

has a

has a

is a

(*)Change behavior at runtime

5

“better than”(*)

(*)

Page 24: Welcome to Design patterns

Strategy PatternStrategy Pattern

Page 25: Welcome to Design patterns

Example 2Example 2

Page 26: Welcome to Design patterns

Example 3Example 3

Page 27: Welcome to Design patterns

Design patternsDesign patterns

1. General reusable solution to a commonly occurring problem

within a given context in software design

2. Type of patterns

Creational patterns

Structural patterns

Behavioral patterns

Page 28: Welcome to Design patterns

Design patterns classificationDesign patterns classification

Creational design patterns

This design patterns is all about class instantiation

Page 29: Welcome to Design patterns

Design patterns classificationDesign patterns classification

Structural design patterns

This design patterns is all about Class and Object composition

Page 30: Welcome to Design patterns

Design patterns classificationDesign patterns classificationBehavioral

design patterns:

Class's objects

communication

Page 31: Welcome to Design patterns

How to use design patternsHow to use design patterns

Load to your brain

1

2

Recognize places to apply

Page 32: Welcome to Design patterns

QAQA

Investigate more about design patterns?

Thank you for your attention!