Welcome to Design patterns

554 views

Published on

Short introduction about design patterns

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
554
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
15
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • the Observer Pattern
  • Welcome to Design patterns

    1. 1. Design PatternsWelcome to Design Patterns 2011.11.18 Ngo Doan Lap
    2. 2. ContentsI. Introduce to design pattern through example 1. Problem 2. Issues 3. SolutionsII. Design patternsIII. Design patterns classification
    3. 3. Problem1. Simulate Duck application SimUDuck Initial design Supper class 1 (abstract class) 2 3 41. show a large varietyof duck species swimming2. making quacking sounds Inherited classes (concrete classes)
    4. 4. Problem1. Upgrade the system Duck can fly 1 2
    5. 5. Issues 1 32
    6. 6. Solution is …inheritance?Override fly() method to do nothing 1 No fly, no quack 2 3 Disadvantage of inheritance?1. Code is duplicated across subclasses2. Changes can unintentionally affect others Difficult to3. Hard to gain knowledge of all object behaviors maintain4. Runtime behavior change are difficult
    7. 7. Solution is …interface?1. Flyable() interface. 1 What happen if you need2. Spec will keep changing to make a little 23. Override fly() and quack() change to the flying Behavior….in all of 100 the flying Duck subclasses? 1. Interface solves part of the problem 3 2. Completely destroys code reuse for those behaviors
    8. 8. Solution is …CHANGENo matter how well you design an application, over time anapplication must grow and change or it will die. 1 2 4 3 Change 5 Stable
    9. 9. Solution 1 fly() and quack() are the parts of the Duck class that vary across ducks 2 Separate change and stable How to design3 Duck behaviors?
    10. 10. SolutionWhat we want: 1 1. Instantiate a particular object and initialize it with specific type of flying behavior 2. Can change flying behavior at runtime. 2 use an interface to ~ API represent each behavior 3 4 Any behaviors changed will not 5 affected to Duck class and it’s child classes
    11. 11. Program to an interface vs Program to an implementationProgram to an implementation Have demo as quick as possible Don’t care about extend/upgrade/maintainProgram to an interfaceassign the concrete implementationobject at runtimeTake time to design but it is flexible
    12. 12. Implementing the Duck Behaviorstwo interfaces: FlyBehaviorand QuackBehavior 1 2
    13. 13. Implementing the Duck Behaviors 1 Easy to reuse for other objects (not duck)2 Easy to extend/modify that make no side effect
    14. 14. Integrating the Duck Behavior1 Add two instance variables to the Duck class2 Implement performQuack() don’t care what kind of object it is. Care about how to quack()
    15. 15. Integrating the Duck Behavior3 flyBehavior and quackBehavior instance variables are set 3.2 3.1 3.3
    16. 16. Testing
    17. 17. Testing
    18. 18. Testing
    19. 19. Testing
    20. 20. Setting behavior dynamically
    21. 21. Testing
    22. 22. The Big Picture 2 (*) 1 3 4 5 Strategy for upgrading(*)Change behavior at runtime
    23. 23. Strategy Pattern
    24. 24. Example 2
    25. 25. Example 3
    26. 26. Design patterns1. General reusable solution to a commonly occurring problemwithin a given context in software design2. Type of patterns Creational patterns Structural patterns Behavioral patterns
    27. 27. Design patterns classificationCreational design patternsThis design patterns is all about class instantiation
    28. 28. Design patterns classificationStructural design patternsThis design patterns is all about Class and Object composition
    29. 29. Design patterns classificationBehavioraldesign patterns:Classs objectscommunication
    30. 30. How to use design patterns 1 Load to your brain 2 Recognize places to app
    31. 31. QAInvestigate more about design patterns? Thank you for your attention!

    ×