Design Principles Flashcards
(19 cards)
What is coupling?
The degree of direct knowledge that one element has of another.
What is tight coupling?
A class that knows more than it should about the way another class is implemented. Eg
When a java class has another class as an attribute and makes use of it.
// Java program to illustrate
// tight coupling concept
class Subject {
Topic t = new Topic();
public void startReading()
{
t.understand();
}
}
class Topic {
public void understand()
{
System.out.println(“Tight coupling concept”);
}
}
What is loose coupling?
A class only knows what another class has through its exposed interface
Eg: Where a java class has a new instance of that class but it isn’t a field of that class
// Java program to illustrate
// tight coupling concept
class Volume
{
public static void main(String args[])
{
Box b = new Box(5,5,5);
System.out.println(b.volume);
}
}
class Box
{
public int volume;
Box(int length, int width, int height)
{
this.volume = length * width * height;
}
}
What is Cohesion?
A method used to indicate the degree to which a class has a single, well focused purpose, The higher the cohesiveness of the class the better
Aims of Coupling x Cohesion
Easier to develop
Less fragile
Easier to add new features
Easier to maintain
Benefits of higher cohesion
Easier to maintain
Much less frequently changed
Classes are more usable than other as designed with well focused purpose.
Single responsibility principle
Every class should have a single responsibility and that should be entirely encapsulated by the class. All services should be narrowly aligned with that responsibility
God classes and God objects
God class: Keeps track of a lot of data and has lots of responsibility
God object: Object that knows or does too much
Purpose of single responsibility
Organize code
More stable
Higher coupling
Dependency inversion principle
Depend on abstractions not concretions. Program to interfaces not implementations. Program to the most abstract class possible
Purpose of Dependency inversion principle
Concrete classes may change a lot whereas abstract classes usually rarely change.
Interface segregation principle
Reduce the side effects of using larger interfaces by breaking application interfaces into smaller ones
Rules of interface segregation principle
Don’t make large multipurpose interfaces.
Don’t make clients depend on interfaces that aren’t used
Purpose of Interface segregation principle
When something is changed, changes for everyone else will be minimized.
How to identify bloated interfaces?
- See if your interface has too many methods.
- See if they have low cohesion
- Can apply to abstract classes
What is the open closed principle?
Software entities should be open for extension but closed for modification. This uses abstraction and polymorphism
Benefits of open closed principle;
Add new systems by creating new classes of the original interface without needing to modify our implementation
Example of open closed
interface for payment
method: processPayment
class1: Revolut payment - implements process
class2: Paypal payment - implements process
class3: payment sys - uses either for options
Violations of open closed (3)
- Modifying existing code: Defeats the purpose of principle, introduces risk of breaking og functionality eg, rather than making new customercontroller, could have made different controller for sql impl
- God classes: God classes tend to grow and become hard to extend and maintain
- Conditional statements: Adding new conditional cases requires modifying existing code