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.
2. Agenda Introduction Coding practices Is there room for improvement? Object Oriented Analysis and Design Design Patterns C an intro An orientation example C Strategy Pattern Inversion of Control C Dependency Injection Mediator Pattern Quick look at a practical example
3. 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
4. Coding practices Following naming conventions Writing commented code Wrapping code in regions Etc. etc. etc. What is beyond it?
5. Object Oriented Analysis and Design We all know what is OOP Figuring how good OOP will help our solution is OOAD
6. Design Patterns C 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
7. An Orientation Example C Strategy Pattern Duck quack() swim() abstract display() MallardDuck display() RedheadDuck display() Lots of other types of ducks
8. An Orientation Example C Strategy Pattern Duck quack() swim() abstract display() fly() MallardDuck display() RedheadDuck display() Lots of other types of ducks
9. An Orientation Example C Strategy Pattern Duck quack() swim() abstract display() fly() MallardDuck display() RedheadDuck display() Lots of other types of ducks
10. An Orientation Example C Strategy Pattern Duck quack() swim() abstract display() fly() MallardDuck display() RedheadDuck display() Yay!! I can fly too!!! RubberDuck display()
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? <<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 ?
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´ 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
17. Inversion of Control C 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(); } }
18. 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!
19. 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