Your SlideShare is downloading. ×
0
Introduction to Design Patterns
Introduction to Design Patterns
Introduction to Design Patterns
Introduction to Design Patterns
Introduction to Design Patterns
Introduction to Design Patterns
Introduction to Design Patterns
Introduction to Design Patterns
Introduction to Design Patterns
Introduction to Design Patterns
Introduction to Design Patterns
Introduction to Design Patterns
Introduction to Design Patterns
Introduction to Design Patterns
Introduction to Design Patterns
Introduction to Design Patterns
Introduction to Design Patterns
Introduction to Design Patterns
Introduction to Design Patterns
Introduction to Design Patterns
Introduction to Design Patterns
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Introduction to Design Patterns

5,290

Published on

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.

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.

Published in: Technology, Business
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
5,290
On Slideshare
0
From Embeds
0
Number of Embeds
14
Actions
Shares
0
Downloads
192
Comments
0
Likes
2
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Design Patterns Abhishek Chatterjee
  • 2. Agenda <ul><li>Introduction </li></ul><ul><li>Coding practices </li></ul><ul><li>Is there room for improvement? </li></ul><ul><li>Object Oriented Analysis and Design </li></ul><ul><li>Design Patterns – an intro </li></ul><ul><li>An orientation example – Strategy Pattern </li></ul><ul><li>Inversion of Control – Dependency Injection </li></ul><ul><li>Mediator Pattern </li></ul><ul><li>Quick look at a practical example </li></ul>
  • 3. Introduction <ul><li>This is nothing about a new technology or a framework </li></ul><ul><li>This is all about how and where to write code so that </li></ul><ul><ul><li>The solution is scalable </li></ul></ul><ul><ul><li>The solution is flexible </li></ul></ul><ul><ul><li>Chances of errors are reduced </li></ul></ul><ul><ul><li>The solution is imagined in a more real-world way </li></ul></ul><ul><li>We identify some good practices and call them names </li></ul>
  • 4. Coding practices <ul><li>Following naming conventions </li></ul><ul><li>Writing commented code </li></ul><ul><li>Wrapping code in regions </li></ul><ul><li>Etc. etc. etc. </li></ul>What is beyond it?
  • 5. Object Oriented Analysis and Design <ul><li>We all know what is OOP </li></ul><ul><li>Figuring how good OOP will help our solution is OOAD </li></ul>
  • 6. Design Patterns – an intro <ul><li>What are design patterns? </li></ul><ul><li>A pattern of OOAD principle which can be applied to multiple problem scenarios is called a Design Pattern </li></ul><ul><li>Design Pattern is nothing but a good OOAD design and given a name </li></ul>
  • 7. An Orientation Example – Strategy Pattern Duck quack() swim() abstract display() MallardDuck display() RedheadDuck display() Lots of other types of ducks
  • 8. An Orientation Example – Strategy Pattern Duck quack() swim() abstract display() fly() MallardDuck display() RedheadDuck display() Lots of other types of ducks
  • 9. An Orientation Example – Strategy Pattern Duck quack() swim() abstract display() fly() MallardDuck display() RedheadDuck display() Lots of other types of ducks
  • 10. An Orientation Example – Strategy Pattern Duck quack() swim() abstract display() fly() MallardDuck display() RedheadDuck display() Yay!! I can fly too!!! RubberDuck display()
  • 11. &nbsp;
  • 12. Hey Abbey-Shack…. If you want to try Monster.com’s resume services, now is the time.
  • 13. Who is going to rescue me now? &lt;&lt;Interfaces&gt;&gt; !!! Yes!!! That’s it! I make an IFlyable interface and RubberDuck doesn’t get to implement it… MallardDuck &lt;&lt;IFlyable&gt;&gt; display() RedheadDuck &lt;&lt;IFlyable&gt;&gt; display() RubberDuck //You can’t fly! display() They can’t see me happy 
  • 14. 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…
  • 15. I thought she was non-technical… <ul><li>Well here is my situation </li></ul><ul><li>I can’t put the fly() method in the base class </li></ul><ul><li>If I use interface, I can’t reuse code </li></ul><ul><li>Alright, so this calls for a dependency split </li></ul><ul><li>Flying is a behavior and should be separate from the Duck object </li></ul><ul><li>Flying behaviors could be reused on different objects </li></ul><ul><li>Different ducks could fly in different ways </li></ul>
  • 16. Strategy Pattern in Action Duck quack() swim() abstract display() MallardDuck IFlyBehavior:FlyWithWings display() RubberDuck IFlyBehavior: DontFly display() IFlyBehavior fly() FlyWithWings FlyWithRocket DontFly
  • 17. Inversion of Control – Dependency Injection <ul><li>Creating a dependency between two objects </li></ul><ul><li>Catering to that dependency without affecting scalability of the solution </li></ul><ul><li>How does MallardDuck know if it should use FlyWithWings behavior? Simple: </li></ul><ul><li>public class MallardDuck : Duck </li></ul><ul><li>{ </li></ul><ul><ul><li>IFlyBehavior flyBehavior; </li></ul></ul><ul><ul><li>public MallardDuck() </li></ul></ul><ul><ul><li>{ </li></ul></ul><ul><ul><ul><li>this.flyBehavior = new FlyWithWings(); </li></ul></ul></ul><ul><ul><li>} </li></ul></ul><ul><li>} </li></ul>
  • 18. The problem with that is… <ul><li>When there is a defaulter… </li></ul><ul><li>For example, let’s take RubberDucks </li></ul><ul><li>public class RubberDuck : Duck </li></ul><ul><li>{ </li></ul><ul><ul><li>IFlyBehavior flyBehavior; </li></ul></ul><ul><ul><li>public RubberDuck() </li></ul></ul><ul><ul><li>{ </li></ul></ul><ul><ul><ul><li>this.flyBehavior = new DontFly(); </li></ul></ul></ul><ul><ul><li>} </li></ul></ul><ul><li>} </li></ul>So you thought I couldn’t fly? Wheeee… Look at my rocket boosters!
  • 19. Constructor Injection <ul><li>public class RubberDuck : Duck </li></ul><ul><li>{ </li></ul><ul><ul><li>IFlyBehavior flyBehavior; </li></ul></ul><ul><ul><li>public RubberDuck(IFlyBehavior flyBehavior) </li></ul></ul><ul><ul><li>{ </li></ul></ul><ul><ul><ul><li>this.flyBehavior = flyBehavior; </li></ul></ul></ul><ul><ul><li>} </li></ul></ul><ul><li>} </li></ul><ul><li>Now anyone instantiating a Duck object needs to inject the dependent class, solving the design problem of the Rubber duck with boosters </li></ul>
  • 20. Q &amp; A
  • 21. Thank You

×