21
Design Patterns Abhishek Chatterjee

Introduction to Design Patterns

Embed Size (px)

DESCRIPTION

This presentation intends to be an introduction to Design Patterns, and elaborate their importance with a small example. Its intention is to orient the audience to start exploring more on the topic.

Citation preview

Page 1: Introduction to Design Patterns

Design Patterns

Abhishek Chatterjee

Page 2: Introduction to Design Patterns

Agenda

Introduction Coding practices Is there room for improvement? Object Oriented Analysis and Design Design Patterns – an intro An orientation example – Strategy Pattern Inversion of Control – Dependency Injection Mediator Pattern Quick look at a practical example

Page 3: Introduction to Design Patterns

Introduction

This is nothing about a new technology or a framework This is all about how and where to write code so that

The solution is scalable The solution is flexible Chances of errors are reduced The solution is imagined in a more real-world way

We identify some good practices and call them names

Page 4: Introduction to Design Patterns

Coding practices

Following naming conventions Writing commented code Wrapping code in regions Etc. etc. etc.

What is beyond it?

Page 5: Introduction to Design Patterns

Object Oriented Analysis and Design

We all know what is OOP

Figuring how good OOP will help our solution is OOAD

Page 6: Introduction to Design Patterns

Design Patterns – an intro

What are design patterns? A pattern of OOAD principle which can be applied to multiple

problem scenarios is called a Design Pattern Design Pattern is nothing but a good OOAD design and given a

name

Page 7: Introduction to Design Patterns

An Orientation Example – Strategy Pattern

Duckquack()swim()abstract display()

Duckquack()swim()abstract display()

MallardDuck

display()

MallardDuck

display()

RedheadDuck

display()

RedheadDuck

display()

Lots of other types of ducks

Page 8: Introduction to Design Patterns

An Orientation Example – Strategy Pattern

Duckquack()swim()abstract display()fly()

Duckquack()swim()abstract display()fly()

MallardDuck

display()

MallardDuck

display()

RedheadDuck

display()

RedheadDuck

display()

Lots of other types of ducks

Page 9: Introduction to Design Patterns

An Orientation Example – Strategy Pattern

Duckquack()swim()abstract display()fly()

Duckquack()swim()abstract display()fly()

MallardDuck

display()

MallardDuck

display()

RedheadDuck

display()

RedheadDuck

display()

Lots of other types of ducks

Page 10: Introduction to Design Patterns

An Orientation Example – Strategy Pattern

Duckquack()swim()abstract display()fly()

Duckquack()swim()abstract display()fly()

MallardDuck

display()

MallardDuck

display()

RedheadDuck

display()

RedheadDuck

display()

Yay!! I can fly too!!!

Yay!! I can fly too!!!

RubberDuck

display()

RubberDuck

display()

Page 11: Introduction to Design Patterns
Page 12: Introduction to Design Patterns

Hey Abbey-Shack…. If you want to try

Monster.com’s resume services, now is the time.

Hey Abbey-Shack…. If you want to try

Monster.com’s resume services, now is the time.

Page 13: Introduction to Design Patterns

Who is going to rescue me now?

<<Interfaces>> !!!

Yes!!! That’s it! I make an IFlyable interface and RubberDuck doesn’t get to implement it…

MallardDuck <<IFlyable>>display()

MallardDuck <<IFlyable>>display()

RedheadDuck <<IFlyable>>display()

RedheadDuck <<IFlyable>>display()

RubberDuck//You can’t fly!display()

RubberDuck//You can’t fly!display()

They can’t see me happy

Page 14: Introduction to Design Patterns

I… I… I…I have a Question!

SimCorp simulates 50 ducks… are you saying you are going to write 50 fly methods? What if there is a change in flying style and it effects

20 ducks… will all 20 ducks change?

I am losing money you know…

Page 15: Introduction to Design Patterns

I thought she was non-technical…

• Well here is my situation• I can’t put the fly() method in the base class• If I use interface, I can’t reuse code• Alright, so this calls for a dependency split• Flying is a behavior and should be separate from the Duck object• Flying behaviors could be reused on different objects• Different ducks could fly in different ways

Page 16: Introduction to Design Patterns

Strategy Pattern in Action

Duckquack()swim()abstract display()

Duckquack()swim()abstract display()

MallardDuckIFlyBehavior:FlyWithWingsdisplay()

MallardDuckIFlyBehavior:FlyWithWingsdisplay()

RubberDuckIFlyBehavior: DontFly display()

RubberDuckIFlyBehavior: DontFly display()

IFlyBehaviorfly()

FlyWithWings

FlyWithRocket

DontFly

Page 17: Introduction to Design Patterns

Inversion of Control – Dependency Injection

• Creating a dependency between two objects• Catering to that dependency without affecting scalability of the

solution• How does MallardDuck know if it should use FlyWithWings

behavior? Simple:• public class MallardDuck : Duck• {

– IFlyBehavior flyBehavior;– public MallardDuck()– {

• this.flyBehavior = new FlyWithWings();– }

• }

Page 18: Introduction to Design Patterns

The problem with that is…

• When there is a defaulter…• For example, let’s take RubberDucks• public class RubberDuck : Duck• {

– IFlyBehavior flyBehavior;– public RubberDuck()– {

• this.flyBehavior = new DontFly();– }

• } So you thought I couldn’t fly?

Wheeee… Look at my rocket boosters!

Page 19: Introduction to Design Patterns

Constructor Injection

• public class RubberDuck : Duck• {

– IFlyBehavior flyBehavior;– public RubberDuck(IFlyBehavior flyBehavior)– {

• this.flyBehavior = flyBehavior;– }

• }

• Now anyone instantiating a Duck object needs to inject the dependent class, solving the design problem of the Rubber duck with boosters

Page 20: Introduction to Design Patterns

Q & A

Page 21: Introduction to Design Patterns

Thank You