Design Principles Flashcards

(19 cards)

1
Q

What is coupling?

A

The degree of direct knowledge that one element has of another.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is tight coupling?

A

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”);
}
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is loose coupling?

A

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;
}
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is Cohesion?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Aims of Coupling x Cohesion

A

Easier to develop
Less fragile
Easier to add new features
Easier to maintain

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Benefits of higher cohesion

A

Easier to maintain
Much less frequently changed
Classes are more usable than other as designed with well focused purpose.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Single responsibility principle

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

God classes and God objects

A

God class: Keeps track of a lot of data and has lots of responsibility
God object: Object that knows or does too much

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Purpose of single responsibility

A

Organize code
More stable
Higher coupling

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Dependency inversion principle

A

Depend on abstractions not concretions. Program to interfaces not implementations. Program to the most abstract class possible

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Purpose of Dependency inversion principle

A

Concrete classes may change a lot whereas abstract classes usually rarely change.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Interface segregation principle

A

Reduce the side effects of using larger interfaces by breaking application interfaces into smaller ones

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Rules of interface segregation principle

A

Don’t make large multipurpose interfaces.
Don’t make clients depend on interfaces that aren’t used

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Purpose of Interface segregation principle

A

When something is changed, changes for everyone else will be minimized.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How to identify bloated interfaces?

A
  1. See if your interface has too many methods.
  2. See if they have low cohesion
  3. Can apply to abstract classes
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is the open closed principle?

A

Software entities should be open for extension but closed for modification. This uses abstraction and polymorphism

17
Q

Benefits of open closed principle;

A

Add new systems by creating new classes of the original interface without needing to modify our implementation

18
Q

Example of open closed

A

interface for payment
method: processPayment

class1: Revolut payment - implements process

class2: Paypal payment - implements process

class3: payment sys - uses either for options

19
Q

Violations of open closed (3)

A
  1. 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
  2. God classes: God classes tend to grow and become hard to extend and maintain
  3. Conditional statements: Adding new conditional cases requires modifying existing code