SlideShare a Scribd company logo
1 of 32
Design Patterns



Welcome to Design Patterns

         2011.11.18


                      Ngo Doan Lap
Contents
I. Introduce to design pattern through example
     1. Problem
     2. Issues
     3. Solutions
II. Design patterns
III. Design patterns classification
Problem
1. Simulate Duck application

    SimUDuck                              Initial design
                                                           Supper class
                            1                              (abstract class)

                                                               2




                            3                                 4
1. show a large variety
of duck species swimming

2. making quacking sounds


                                      Inherited classes
                                      (concrete classes)
Problem
1. Upgrade the system
   Duck can fly


                        1




                            2
Issues

    1




             3
2
Solution is …inheritance?
Override fly() method to do nothing
                    1
                                                    No fly, no quack

                                                         2




                        3

     Disadvantage
     of inheritance?
1. Code is duplicated across subclasses
2. Changes can unintentionally affect others                 Difficult to
3. Hard to gain knowledge of all object behaviors
                                                             maintain
4. Runtime behavior change are difficult
Solution is …interface?
1. Flyable() interface. 1             What happen if you need
2. Spec will keep changing               to make a little
                                      2
3. 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
Solution is …CHANGE
No matter how well you design an application, over time an
application must grow and change or it will die. 1
  2
                                                       4




  3
             Change
                                                   5




      Stable
Solution
             1       fly() and quack() are the parts of the
                     Duck class that vary across ducks




                 2
    Separate change and stable




    How to design
3
    Duck behaviors?
Solution
What 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
Program to an interface vs Program to an implementation


Program to an implementation             Have demo as quick as possible
                                         Don’t care about extend/upgrade/maintain




Program to an interface



assign the concrete implementation
object at runtime




Take time to design but it is flexible
Implementing the Duck Behaviors
two interfaces: FlyBehavior
and QuackBehavior


           1                  2
Implementing the Duck Behaviors

     1   Easy to reuse for other objects (not duck)




2   Easy to extend/modify that make no side effect
Integrating the Duck Behavior
1   Add two instance variables to the Duck class




2   Implement performQuack()

                                                   don’t care
                                                   what kind of
                                                   object it is.
                                                   Care about
                                                   how to quack()
Integrating the Duck Behavior
3    flyBehavior and quackBehavior instance variables are set
                                              3.2




    3.1


                                                    3.3
Testing
Testing
Testing
Testing
Setting behavior dynamically
Testing
The Big Picture
                                    2
                                               (*)


  1
                                    3




             4


                      5         Strategy for
                                upgrading
(*)Change behavior at runtime
Strategy Pattern
Example 2
Example 3
Design patterns
1. General reusable solution to a commonly occurring problem
within a given context in software design
2. Type of patterns
   Creational patterns
   Structural patterns
   Behavioral patterns
Design patterns classification
Creational design patterns
This design patterns is all about class instantiation
Design patterns classification
Structural design patterns
This design patterns is all about Class and Object composition
Design patterns classification
Behavioral
design patterns:
Class's objects
communication
How to use design patterns

              1
           Load to your brain



                                2
                       Recognize places to app
QA
Investigate more about design patterns?


     Thank you for your attention!

More Related Content

Similar to Welcome to Design patterns

Esoteric LINQ and Structural Madness
Esoteric LINQ and Structural MadnessEsoteric LINQ and Structural Madness
Esoteric LINQ and Structural Madness
Chris Eargle
 
Are Java Programmers Transitioning to Multicore?
Are Java Programmers Transitioning to Multicore? Are Java Programmers Transitioning to Multicore?
Are Java Programmers Transitioning to Multicore?
UFPA
 

Similar to Welcome to Design patterns (20)

Strategy and Template Pattern
Strategy and Template PatternStrategy and Template Pattern
Strategy and Template Pattern
 
A Hazelnut Presentation
A Hazelnut PresentationA Hazelnut Presentation
A Hazelnut Presentation
 
Agile and CMMI - a potential blend
Agile and CMMI - a potential blendAgile and CMMI - a potential blend
Agile and CMMI - a potential blend
 
Android App Architecture with modern libs in practice. Our way in R.I.D., Ser...
Android App Architecture with modern libs in practice. Our way in R.I.D., Ser...Android App Architecture with modern libs in practice. Our way in R.I.D., Ser...
Android App Architecture with modern libs in practice. Our way in R.I.D., Ser...
 
Esoteric LINQ and Structural Madness
Esoteric LINQ and Structural MadnessEsoteric LINQ and Structural Madness
Esoteric LINQ and Structural Madness
 
[MSR2012] An Empirical Study of Supplementary Bug Fixes
[MSR2012] An Empirical Study of Supplementary Bug Fixes[MSR2012] An Empirical Study of Supplementary Bug Fixes
[MSR2012] An Empirical Study of Supplementary Bug Fixes
 
Evolution of Patterns
Evolution of PatternsEvolution of Patterns
Evolution of Patterns
 
Java J2EE Interview Questions Part-1
Java J2EE Interview Questions Part-1Java J2EE Interview Questions Part-1
Java J2EE Interview Questions Part-1
 
Java J2EE Interview Questions Part-1
Java J2EE Interview Questions Part-1Java J2EE Interview Questions Part-1
Java J2EE Interview Questions Part-1
 
10 strategy pattern
10 strategy pattern10 strategy pattern
10 strategy pattern
 
策略模式
策略模式 策略模式
策略模式
 
Lesson11 more behavioural patterns
Lesson11 more behavioural patternsLesson11 more behavioural patterns
Lesson11 more behavioural patterns
 
Breaking the monolith
Breaking the monolithBreaking the monolith
Breaking the monolith
 
Apex design patterns
Apex design patternsApex design patterns
Apex design patterns
 
Python: Common Design Patterns
Python: Common Design PatternsPython: Common Design Patterns
Python: Common Design Patterns
 
How To Dependency Inject a Kitten: An Introduction to Dagger 2
How To Dependency Inject a Kitten: An Introduction to Dagger 2How To Dependency Inject a Kitten: An Introduction to Dagger 2
How To Dependency Inject a Kitten: An Introduction to Dagger 2
 
Escape From Your VMs with Image2Docker Jeff Nickoloff, All in Geek Consulting...
Escape From Your VMs with Image2Docker Jeff Nickoloff, All in Geek Consulting...Escape From Your VMs with Image2Docker Jeff Nickoloff, All in Geek Consulting...
Escape From Your VMs with Image2Docker Jeff Nickoloff, All in Geek Consulting...
 
Are Java Programmers Transitioning to Multicore?
Are Java Programmers Transitioning to Multicore? Are Java Programmers Transitioning to Multicore?
Are Java Programmers Transitioning to Multicore?
 
Chapter 02
Chapter 02Chapter 02
Chapter 02
 
[Djangocon2015] Demystifying mixins with Django
[Djangocon2015] Demystifying mixins with Django[Djangocon2015] Demystifying mixins with Django
[Djangocon2015] Demystifying mixins with Django
 

Recently uploaded

Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
AnaAcapella
 

Recently uploaded (20)

Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 

Welcome to Design patterns

  • 1. Design Patterns Welcome to Design Patterns 2011.11.18 Ngo Doan Lap
  • 2. Contents I. Introduce to design pattern through example 1. Problem 2. Issues 3. Solutions II. Design patterns III. Design patterns classification
  • 3. Problem 1. Simulate Duck application SimUDuck Initial design Supper class 1 (abstract class) 2 3 4 1. show a large variety of duck species swimming 2. making quacking sounds Inherited classes (concrete classes)
  • 4. Problem 1. Upgrade the system Duck can fly 1 2
  • 5. Issues 1 3 2
  • 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 subclasses 2. Changes can unintentionally affect others Difficult to 3. Hard to gain knowledge of all object behaviors maintain 4. Runtime behavior change are difficult
  • 7. Solution is …interface? 1. Flyable() interface. 1 What happen if you need 2. Spec will keep changing to make a little 2 3. 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. Solution is …CHANGE No matter how well you design an application, over time an application must grow and change or it will die. 1 2 4 3 Change 5 Stable
  • 9. Solution 1 fly() and quack() are the parts of the Duck class that vary across ducks 2 Separate change and stable How to design 3 Duck behaviors?
  • 10. Solution What 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. Program to an interface vs Program to an implementation Program to an implementation Have demo as quick as possible Don’t care about extend/upgrade/maintain Program to an interface assign the concrete implementation object at runtime Take time to design but it is flexible
  • 12. Implementing the Duck Behaviors two interfaces: FlyBehavior and QuackBehavior 1 2
  • 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. Integrating the Duck Behavior 1 Add two instance variables to the Duck class 2 Implement performQuack() don’t care what kind of object it is. Care about how to quack()
  • 15. Integrating the Duck Behavior 3 flyBehavior and quackBehavior instance variables are set 3.2 3.1 3.3
  • 22.
  • 23. The Big Picture 2 (*) 1 3 4 5 Strategy for upgrading (*)Change behavior at runtime
  • 27. Design patterns 1. General reusable solution to a commonly occurring problem within a given context in software design 2. Type of patterns Creational patterns Structural patterns Behavioral patterns
  • 28. Design patterns classification Creational design patterns This design patterns is all about class instantiation
  • 29. Design patterns classification Structural design patterns This design patterns is all about Class and Object composition
  • 30. Design patterns classification Behavioral design patterns: Class's objects communication
  • 31. How to use design patterns 1 Load to your brain 2 Recognize places to app
  • 32. QA Investigate more about design patterns? Thank you for your attention!

Editor's Notes

  1. the Observer Pattern