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

Strategy and Template Pattern
Strategy and Template PatternStrategy and Template Pattern
Strategy and Template PatternJonathan Simon
 
Agile and CMMI - a potential blend
Agile and CMMI - a potential blendAgile and CMMI - a potential blend
Agile and CMMI - a potential blendMosesraj R
 
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...Sigma Software
 
Esoteric LINQ and Structural Madness
Esoteric LINQ and Structural MadnessEsoteric LINQ and Structural Madness
Esoteric LINQ and Structural MadnessChris Eargle
 
[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 Fixeshuni7595
 
Evolution of Patterns
Evolution of PatternsEvolution of Patterns
Evolution of PatternsChris Eargle
 
Java J2EE Interview Questions Part-1
Java J2EE Interview Questions Part-1Java J2EE Interview Questions Part-1
Java J2EE Interview Questions Part-1javatrainingonline
 
Lesson11 more behavioural patterns
Lesson11 more behavioural patternsLesson11 more behavioural patterns
Lesson11 more behavioural patternsOktJona
 
Python: Common Design Patterns
Python: Common Design PatternsPython: Common Design Patterns
Python: Common Design PatternsDamian T. Gordon
 
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 2Todd Burgess
 
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...Docker, Inc.
 
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
 
[Djangocon2015] Demystifying mixins with Django
[Djangocon2015] Demystifying mixins with Django[Djangocon2015] Demystifying mixins with Django
[Djangocon2015] Demystifying mixins with DjangoAna Balica
 

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

Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 

Recently uploaded (20)

Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 

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