Design Principles Flashcards
1
Q
Princple of Least Knowledge (Law of Demeter)
A
A method M in an object O can call on:
- methods in O
- methods of parameters to M
- method of object instantiated
- method of object that is part of O
2
Q
Liskov Substitution Principle
A
Subtypes should be substitutable for their base types
3
Q
Encapsulate what varies
A
- separate varying aspects of code so changing doesn’t affect rest of code
- encapsulate behaviour
- encapsulate object creation
- encapsulate method invocation (command)
4
Q
Favour composition over inheritance
A
- enables to change behaviour at run time
- delegate the implementation of a behaviour to composed class
5
Q
loosely-coupled design
A
- loosely coupled designs minimise interdependency between objects
6
Q
Hoolywood principle: don’t call us, we’ll call you
A
- enable low-level components to hook themselves into a system, but the high-level components decide “when and how” they are needed
- i.e. template method pattern
7
Q
OCP
A
- new changes must be implemented by new code not altering existing
- i.e. strategy, state, decorator and template
8
Q
SRP
A
classes should only have one reason to change = high cohesion
9
Q
Program to an interface (super-type) and not to an implementation
A
- the declared type of the variable should be a super-type (abstract class or interface)
- helps use polymorphism
10
Q
Depend upon abstractions. Do not depend on create classes
A
- A high level module should not depend on “low-level” modules, but both should depend on abstractions