Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Design patterns

1,442 views

Published on

Published in: Technology
  • Be the first to comment

Design patterns

  1. 1. Design Patterns Abhishek Chatterjee
  2. 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. 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. 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. 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. 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. 7. An Orientation Example – Strategy Pattern Duck quack() swim() abstract display() MallardDuck display() RedheadDuck display() Lots of other types of ducks
  8. 8. An Orientation Example – Strategy Pattern Duck quack() swim() abstract display() fly() MallardDuck display() RedheadDuck display() Lots of other types of ducks
  9. 9. An Orientation Example – Strategy Pattern Duck quack() swim() abstract display() fly() MallardDuck display() RedheadDuck display() Lots of other types of ducks
  10. 10. An Orientation Example – Strategy Pattern Duck quack() swim() abstract display() fly() MallardDuck display() RedheadDuck display() Yay!! I can fly too!!! RubberDuck display()
  11. 12. Hey Abbey-Shack…. If you want to try Monster.com’s resume services, now is the time.
  12. 13. 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() RedheadDuck <<IFlyable>> display() RubberDuck //You can’t fly! display() They can’t see me happy 
  13. 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…
  14. 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>
  15. 16. Strategy Pattern in Action Duck quack() swim() abstract display() MallardDuck IFlyBehavior:FlyWithWings display() RubberDuck IFlyBehavior: DontFly display() IFlyBehavior fly() FlyWithWings FlyWithRocket DontFly
  16. 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>
  17. 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!
  18. 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>
  19. 20. Q & A
  20. 21. Thank You

×