際際滷

際際滷Share a Scribd company logo
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

More Related Content

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