17. Waar staat het voor
Single Responsibility Principle
Open/Closed Principle
Liskov Substitution Principle
Interface Segregation Principle
Dependency Inversion Principle
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
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. }