SlideShare una empresa de Scribd logo
1 de 38
Descargar para leer sin conexión
Building your application
         SOLID




                    Paul de Raaij | @pderaaij
                                     2-2-2012
Student HHS
Afstudeerde
r
Korte introductie
• Afstuderen bij Eleven
Toolbox
Afstudeer presentatie: Solid design
Formulierenmodul
e
DESIG
N
SOLID software
    design
SOLID Design
• Vijf essentiële principes
• Geïntroduceerd door Robert C. Martin



Pr i n c i p l e s o f   O b j e c t -O r i e n t e d d
Waarom SOLID
design?
Loosely coupled
High cohesion
Reusable
Maintainable
Testable
Waar staat het voor
•   Single Responsibility Principle
•   Open/Closed Principle
•   Liskov Substitution Principle
•   Interface Segregation Principle
•   Dependency Inversion Principle
Single Responsibility Principle



Een klasse heeft maar één reden om te wijzigen
Single Responsibility Principle


1. public class Employee {
2.     public BigDecimal pay();
3.     public String getFullName();
4.     public float getWorkedHours();
5. }
Single Responsibility Principle

1. public class Employee {
2.     public String getFullName();
3. }
4.
5. public class Cashier {
6.     public void pay(Employee e);
7. }
8.
9. public class EmployeeReporter {
10.    public float getWorkedHours(Employee e);
11.}
Open/Closed principle



   Entiteiten moeten gesloten zijn voor
wijzigingen, maar open voor uitbreidingen
Open/Closed principle


1. public class Musician {
2.         public void playGuitar();
3. }
Open/Closed principle


1. public class Musician {
2.         public void playGuitar();
3.         public void playPiano();
4. }
Open/Closed principle
1. public class Musician {
2.         public void playInstrument(Instrument i);
3. }
4.
5. interface Instrument {
6.         public void play();
7. }
8.
9. public class Guitar implements Instrument {
10.        public void play();
11.}
12.
13.public class Piano implements Instrument {
14.        public void play();
15.}
Liskov Substitution Principle



Functies die gebruik maken van een basis klasse
moeten feilloos werken als een afgeleide klasse
                 wordt gebruikt
Liskov Substitution Principle
1.    class Rectangle {
2.            public int width;
3.            public int height;
4.
5.            public void setWidth(int w) {}
6.            public void setHeight(int h) {}
7.            public int getArea() { return h*w; }
8.    }
9.
10.   class Square extends Rectangle {
11.           public void setWidth( int w ) { super.setWidth(w); super.setHeight(w); }
12.           public void setHeight( int h ) { super.setWidth(h); super.setHeight(h); }
13.   }
14.
15.   class TestProgram {
16.           Rectangle r = new Rectangle();
17.           r.setWidth(5); r.setHeight(4);
18.           r.getArea(); // returns 20
19.
20.           Square s = new Square();
21.           s.setWidth(5); s.setHeight(4);
22.           s.getArea(); // returns 16;
23.   }
Interface Segregation Principle



 Functie-specifieke interfaces zijn beter dan
          één generieke interface
Interface Segregation Principle
1. interface Animal {
2.          public void bark();
3.          public void fly();
4.          public void run();
5.          public void eatPlant();
6.          public void eatMeat();
7. }
8.
9. public Dog implements Animal {
10.         public void bark();
11.         public void run();
12.         public void eatPlant();
13.         public void eatMeat();
14. }
15.
16. public Duck implements Animal {
17.         public void fly();
18.         public void eatPlant();
19. }
1.    interface Barkable {
2.            public void bark();
3.    }
4.
5.    interface Flyable {
6.            public void fly();
7.    }
8.
9.    interface Runnable {
10.           public void run();
11.   }
12.
13.   interface Herbivore() {
14.           public void eatPlant();
15.   }
16.
17.   interface Carnivore() {
18.           public void eatMeat();
19.   }
20.
21.   public Dog implements Barkable, Runnable, Herbivore, Carnivore {
22.           public void bark();
23.           public void run();
24.           public void eatPlant();
25.           public void eatMeat();
26.   }
27.
28.   public Duck implements Flyable, Herbivore {
29.           public void fly();
30.           public void eatPlant();
31.   }
Dependency Inversion Principle



 A)   High level modules mogen niet afhankelijk zijn van low level
           modules, beide moeten afhangen van abstracties

 B) Abstracties mogen niet afhangen van details, details moeten
                   afhangen van abstracties
Dependency Inversion Principle

1. class Band {
2.         private Singer = new Singer();
3.         private Musician = new Musician();
4.
5.         public void playSong();
6. }
Dependency Inversion Principle
1. interface MusicalTalent {
2.         public void useTalent();
3. }
4.
5. class Musician implements MusicalTalent() {}
6. class Singer implements MusicalTalent() {}
7. class SingingGuitarist implements MusicalTalent() {}
8.
9. class Band {
10.        public void playSong( MusicalTalent mt ) {
11.               mt.useTalent();
12.        }
13. }
Dependency Inversion Principle
1. interface MusicalTalent {
2.         public void useTalent();
3. }
4.
5. class Musician implements MusicalTalent() {}
6. class Singer implements MusicalTalent() {}
7. class SingingGuitarist implements MusicalTalent() {}
8.
9. class Band {
10.        private List<MusicalTalent> members = new ArrayList<MusicalTalent>();
11.        public Band() {}
12.        public void addMember( MusicalTalent mt ) {
13.                members.add( mt );
14.        }
15.
16.        public void playSong() {}
17. }
SOLID
Nooit alleen…
… Sterk aan elkaar
gekoppeld
Perfect ontwerp
     bestaat niet




                    SOLID helpt de
                        balans te vinden
Afstudeer presentatie: Solid design

Más contenido relacionado

Destacado

5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Applitools
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at WorkGetSmarter
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...DevGAMM Conference
 
Barbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationBarbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationErica Santiago
 
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them wellGood Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them wellSaba Software
 
Introduction to C Programming Language
Introduction to C Programming LanguageIntroduction to C Programming Language
Introduction to C Programming LanguageSimplilearn
 
The Pixar Way: 37 Quotes on Developing and Maintaining a Creative Company (fr...
The Pixar Way: 37 Quotes on Developing and Maintaining a Creative Company (fr...The Pixar Way: 37 Quotes on Developing and Maintaining a Creative Company (fr...
The Pixar Way: 37 Quotes on Developing and Maintaining a Creative Company (fr...Palo Alto Software
 
9 Tips for a Work-free Vacation
9 Tips for a Work-free Vacation9 Tips for a Work-free Vacation
9 Tips for a Work-free VacationWeekdone.com
 
I Rock Therefore I Am. 20 Legendary Quotes from Prince
I Rock Therefore I Am. 20 Legendary Quotes from PrinceI Rock Therefore I Am. 20 Legendary Quotes from Prince
I Rock Therefore I Am. 20 Legendary Quotes from PrinceEmpowered Presentations
 

Destacado (20)

5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
 
More than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike RoutesMore than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike Routes
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
 
Barbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationBarbie - Brand Strategy Presentation
Barbie - Brand Strategy Presentation
 
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them wellGood Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
 
Introduction to C Programming Language
Introduction to C Programming LanguageIntroduction to C Programming Language
Introduction to C Programming Language
 
The Pixar Way: 37 Quotes on Developing and Maintaining a Creative Company (fr...
The Pixar Way: 37 Quotes on Developing and Maintaining a Creative Company (fr...The Pixar Way: 37 Quotes on Developing and Maintaining a Creative Company (fr...
The Pixar Way: 37 Quotes on Developing and Maintaining a Creative Company (fr...
 
9 Tips for a Work-free Vacation
9 Tips for a Work-free Vacation9 Tips for a Work-free Vacation
9 Tips for a Work-free Vacation
 
I Rock Therefore I Am. 20 Legendary Quotes from Prince
I Rock Therefore I Am. 20 Legendary Quotes from PrinceI Rock Therefore I Am. 20 Legendary Quotes from Prince
I Rock Therefore I Am. 20 Legendary Quotes from Prince
 

Afstudeer presentatie: Solid design

  • 1. Building your application SOLID Paul de Raaij | @pderaaij 2-2-2012
  • 9. SOLID software design
  • 10. SOLID Design • Vijf essentiële principes • Geïntroduceerd door Robert C. Martin Pr i n c i p l e s o f O b j e c t -O r i e n t e d d
  • 17. Waar staat het voor • Single Responsibility Principle • Open/Closed Principle • Liskov Substitution Principle • Interface Segregation Principle • Dependency Inversion Principle
  • 18. Single Responsibility Principle Een klasse heeft maar één reden om te wijzigen
  • 19. Single Responsibility Principle 1. public class Employee { 2. public BigDecimal pay(); 3. public String getFullName(); 4. public float getWorkedHours(); 5. }
  • 20. Single Responsibility Principle 1. public class Employee { 2. public String getFullName(); 3. } 4. 5. public class Cashier { 6. public void pay(Employee e); 7. } 8. 9. public class EmployeeReporter { 10. public float getWorkedHours(Employee e); 11.}
  • 21. Open/Closed principle Entiteiten moeten gesloten zijn voor wijzigingen, maar open voor uitbreidingen
  • 22. Open/Closed principle 1. public class Musician { 2. public void playGuitar(); 3. }
  • 23. Open/Closed principle 1. public class Musician { 2. public void playGuitar(); 3. public void playPiano(); 4. }
  • 24. Open/Closed principle 1. public class Musician { 2. public void playInstrument(Instrument i); 3. } 4. 5. interface Instrument { 6. public void play(); 7. } 8. 9. public class Guitar implements Instrument { 10. public void play(); 11.} 12. 13.public class Piano implements Instrument { 14. public void play(); 15.}
  • 25. Liskov Substitution Principle Functies die gebruik maken van een basis klasse moeten feilloos werken als een afgeleide klasse wordt gebruikt
  • 26. Liskov Substitution Principle 1. class Rectangle { 2. public int width; 3. public int height; 4. 5. public void setWidth(int w) {} 6. public void setHeight(int h) {} 7. public int getArea() { return h*w; } 8. } 9. 10. class Square extends Rectangle { 11. public void setWidth( int w ) { super.setWidth(w); super.setHeight(w); } 12. public void setHeight( int h ) { super.setWidth(h); super.setHeight(h); } 13. } 14. 15. class TestProgram { 16. Rectangle r = new Rectangle(); 17. r.setWidth(5); r.setHeight(4); 18. r.getArea(); // returns 20 19. 20. Square s = new Square(); 21. s.setWidth(5); s.setHeight(4); 22. s.getArea(); // returns 16; 23. }
  • 27. Interface Segregation Principle Functie-specifieke interfaces zijn beter dan één generieke interface
  • 28. Interface Segregation Principle 1. interface Animal { 2. public void bark(); 3. public void fly(); 4. public void run(); 5. public void eatPlant(); 6. public void eatMeat(); 7. } 8. 9. public Dog implements Animal { 10. public void bark(); 11. public void run(); 12. public void eatPlant(); 13. public void eatMeat(); 14. } 15. 16. public Duck implements Animal { 17. public void fly(); 18. public void eatPlant(); 19. }
  • 29. 1. interface Barkable { 2. public void bark(); 3. } 4. 5. interface Flyable { 6. public void fly(); 7. } 8. 9. interface Runnable { 10. public void run(); 11. } 12. 13. interface Herbivore() { 14. public void eatPlant(); 15. } 16. 17. interface Carnivore() { 18. public void eatMeat(); 19. } 20. 21. public Dog implements Barkable, Runnable, Herbivore, Carnivore { 22. public void bark(); 23. public void run(); 24. public void eatPlant(); 25. public void eatMeat(); 26. } 27. 28. public Duck implements Flyable, Herbivore { 29. public void fly(); 30. public void eatPlant(); 31. }
  • 30. Dependency Inversion Principle A) High level modules mogen niet afhankelijk zijn van low level modules, beide moeten afhangen van abstracties B) Abstracties mogen niet afhangen van details, details moeten afhangen van abstracties
  • 31. Dependency Inversion Principle 1. class Band { 2. private Singer = new Singer(); 3. private Musician = new Musician(); 4. 5. public void playSong(); 6. }
  • 32. Dependency Inversion Principle 1. interface MusicalTalent { 2. public void useTalent(); 3. } 4. 5. class Musician implements MusicalTalent() {} 6. class Singer implements MusicalTalent() {} 7. class SingingGuitarist implements MusicalTalent() {} 8. 9. class Band { 10. public void playSong( MusicalTalent mt ) { 11. mt.useTalent(); 12. } 13. }
  • 33. Dependency Inversion Principle 1. interface MusicalTalent { 2. public void useTalent(); 3. } 4. 5. class Musician implements MusicalTalent() {} 6. class Singer implements MusicalTalent() {} 7. class SingingGuitarist implements MusicalTalent() {} 8. 9. class Band { 10. private List<MusicalTalent> members = new ArrayList<MusicalTalent>(); 11. public Band() {} 12. public void addMember( MusicalTalent mt ) { 13. members.add( mt ); 14. } 15. 16. public void playSong() {} 17. }
  • 34. SOLID
  • 36. … Sterk aan elkaar gekoppeld
  • 37. Perfect ontwerp bestaat niet SOLID helpt de balans te vinden