[M2] SOLID and Design

0.0(0)
studied byStudied by 0 people
0.0(0)
full-widthCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/40

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

41 Terms

1
New cards

What do we mean by “high cohesion, low coupling”?

  • High cohesion → A class/module does one well-defined job.

  • Low coupling → A class depends on as few other classes as possible.

  • Result: Code is easier to maintain, change, and test.

2
New cards

What does it mean for class A to depend on class B?

Class A depends on B if A needs B to work — e.g., A calls B’s methods, uses B objects, or extends B.
If B changes, A may break → this is coupling.

3
New cards

What is SRP?

A class should have one and only one reason to change.

4
New cards

How to identify SRP violations?

  • Class is doing multiple jobs (e.g., logic + saving files + UI handling).

  • Method names vary wildly in purpose.

5
New cards

Why is SRP useful?

Makes code easier to maintain, test, and update.

6
New cards

What is OCP?

Classes should be open for extension, closed for modification.

7
New cards

How to identify OCP violations?

You must edit existing code every time a new feature is added instead of extending.

8
New cards

Why is OCP useful?

Reduces bugs by not constantly touching stable code.

9
New cards

What is LSP?

Subclasses must be usable anywhere the parent class is expected.

10
New cards

How to identify LSP violations?

  • Subclass throws errors for normal parent behavior.

  • Subclass removes expected functionality.

11
New cards

Why is LSP useful?

Ensures inheritance behaves predictably.

12
New cards

What is ISP?

Don’t force classes to implement large, bloated interfaces.
Instead: many small, specific interfaces.

13
New cards

How to identify ISP violations?

Class implements methods it doesn’t use.

14
New cards

Why is ISP useful?

Prevents unnecessary code and confusing designs.

15
New cards

What is DIP?

High-level modules should depend on abstractions, not concrete classes.

16
New cards

How to identify DIP violations?

  • High-level class uses new ConcreteClass() directly.

  • No interfaces are used between layers.

17
New cards

Why is DIP useful?

Allows swapping implementations easily (e.g., switching databases).

18
New cards

What is a user story and who is the target audience?

A short description of a feature from the user’s perspective.
Format: “As a <user>, I want <goal> so that <benefit>.”
Audience: Developers + product team to understand user needs.

19
New cards

How can we identify stakeholders in our program?

Ask: Who uses the system? Who benefits? Who is impacted?
Stakeholders include: end users, admins, developers, business owners, clients.

20
New cards
21
New cards

What is an API?

An Application Programming Interface — a structured way for programs to communicate.
In Java, you typically use an API by:

  1. Making an HTTP request.

  2. Parsing the JSON response.

  3. Using the data in your program.

22
New cards

What is JSON and how is it relevant to an API?

  • JSON = JavaScript Object Notation, a lightweight data format.

  • APIs commonly send and receive data in JSON.

  • In Java, libraries like Jackson or Gson convert JSON objects.

23
New cards

What is the primary purpose of Exceptions in Java?

To report exceptional conditions (unusual, strange, unexpected) that require special treatment, moving away from the normal program execution flow.

24
New cards

Why is using a throw/catch block locally (in the same method) often considered bad practice?

Because the situation the exception reports is often not truly exceptional, and real uses of exceptions are generally not local; throw and catch are typically not in the same block of code.

25
New cards

What are the main benefits of using exceptions?

1. Less programmer time spent on handling errors. 2. Cleaner program structure (isolates exceptional situations). 3. Separation of concerns (local attention to the algorithm, global attention to errors).

26
New cards

What is the root class of the Java exception hierarchy?

Throwable.

27
New cards

What are the two main direct subclasses of Throwable?

Error and Exception

28
New cards

Give examples of Error and RuntimeException subclasses.

Error Subclasses: AssertionError, OutOfMemoryError. RuntimeException Subclasses: ArithmeticException, ClassCastException, IndexOutOfBoundsException, NullPointerException

29
New cards

What is a Checked Exception?

An exception that the compiler checks to ensure it is either caught by a calling method or declared with throws in the method signature

30
New cards

What is an Unchecked Exception?

An exception (subclass of RuntimeException) that the program throws without compile-time checking to see if it's caught. If not caught, it crashes the program and prints the stack trace

31
New cards

When should you typically use a Checked Exception (according to Joshua Bloch)?

For conditions from which the caller can reasonably be expected to recover

32
New cards

When should you typically use an Unchecked Exception (RuntimeException)?

To indicate programming errors (precondition violations), especially if the programmer could have predicted the exception. Also for things the program will throw without checking if eventually caught (e.g., NullPointerException).

33
New cards

Are methods required to handle Error or RuntimeException?

No. These are unchecked. Errors are serious problems that reasonable applications should not try to catch

34
New cards

How do you "throw an exception" in Java?

Use the throw keyword followed by a new Throwable instance (e.g., throw new Throwable(message);).

35
New cards

What is the difference between throw and a return statement?

throw exits the current method like a return statement, but it signals an exceptional (bad) state.

36
New cards

What are the two mandatory choices if you call code that may throw a non-RuntimeException (checked exception)?

. Wrap the code in a try-catch block. 2. Use throws in your method signature to declare that your code may throw the exception.

37
New cards

When is the finally block executed?

The finally clause is always executed, regardless of whether an exception was thrown or caught. (It is typically used for clean-up, like closing open files).

38
New cards

How does Cascading Catching work?

Catch blocks are executed in order. A catch block for a superclass exception (ExSup) should be placed after catch blocks for its subclasses (ExSubA) to ensure the subclass exception is handled by the most specific catch block first

39
New cards

What is the common design guideline regarding where to throw and catch exceptions?

Throw low, catch high." Throwing and catching should ideally not be in the same method

40
New cards

What is a Chained Exception?

A Throwable that contains a cause (another Throwable) that led to the current exception being thrown, allowing for a chain of exceptions

41
New cards

Why is Wrapping Exceptions (Chained Exceptions) important for DIP/Abstraction?

It prevents the upper layer's API from being tied to the implementation details of a lower layer (e.g., preventing a database-specific SQLException from propagating). The upper layer throws a custom, higher-level exception, containing the low-level exception as the cause