Explain object-oriented design principles Flashcards

1
Q

Single Responsibility Principle (SRP)

  1. Explain the principle.
  2. Give a concrete example of how you follow, or break, the principle.
  3. What is the purpose of the principle? Why is it good to follow the principle? (*)
A
  1. Each class or method should only be responsible for one thing. I.e it should have one, and only one, reason to change.
  2. Imagine if one had a class called “BankAccount”, which handled taking out money, putting in money, and changes to balance due to interest on savings. This would break the SRP, since it would do multiple things. To follow it, it could be split by “AccountWithdraw”, “AccountDeposit”, and “AccountInterest”.
  3. For one, the code is more robust because changes are easier to implement, reducing confusion. Secondly, it’s easier to test, and all code for a certain responsibility is found in one place, making for easy access.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Open-Closed Principle (OCP)

  1. Explain the principle.
  2. Give a concrete example of how you follow, or break, the principle.
  3. What is the purpose of the principle? Why is it good to follow the principle? (*)
A
  1. Software modules (classes, methods, etc) should be open for extension, but closed for modification. I.e: One should be able to add new functions without heavy code modification.
  2. One way to break this is through “switch” or “type-checking” statements, which have to be modified every time a new feature/sub-class/object is added.
  3. Allow one to easily maintain, reuse, and extend code, with a lower chance of bugs.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Liskov Substitution Principle (LSP) (*)

  1. Explain the principle.
  2. Give a concrete example of how you follow, or break, the principle.
  3. What is the purpose of the principle? Why is it good to follow the principle? (*)
A
  1. If S is a subtype of T, all instances of T should be replaceable with instances of S, without undesirable changes to the code. I.e subclasses shouldn’t change the behavior of superclasses.
  2. An example of good implementation is having a superclass “Vehicle”, with a “car” subclass, that follows “Vehicle” behavior (e.g: gas/brake/start), but also adds behavior such as “Openboot”.
  3. Better inheritance hierarchy, and we can change supertypes out for subtypes without unexpected behavior.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Interface Segregation Principle (ISP)

  1. Explain the principle.
  2. Give a concrete example of how you follow, or break, the principle.
  3. What is the purpose of the principle? Why is it good to follow the principle? (*)
A
  1. Same as SRP but for interfaces.
    Clients should not be forced to depend upon interfaces that they do not use.
  2. An example of bad adherence to the ISP is if you had an interface called generateReport which had two methods, generatePDF and generateExcel. It would be better to split it up since if you wanted to make just one of the two, you’d have to implement both.
  3. The reasoning for splitting fat interfaces into smaller ones is that classes then do not have to implement dummy methods they don’t need. Reduces need of unnecessary code.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Dependency Inversion Principle (DIP)

  1. Explain the principle.
  2. Give a concrete example of how you follow, or break, the principle.
  3. What is the purpose of the principle? Why is it good to follow the principle? (*)
A
  1. High-level modules should not depend on low-level modules. Both should depend on abstractions. Furthermore, abstractions should not depend on details - details should depend on abstractions.
    In layman’s terms, we want to create as few dependencies as possible, and we want all of them to be “intentional”. By using superclasses instead of subclasses, we can reduce the dependency on a specific class.
  2. Example of good design is making a supertype which doesn’t need to be modified or expanded when subtypes are created.
  3. Lower coupling and more flexibility.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Separation of Concern (SoC)

  1. Explain the principle.
  2. Give a concrete example of how you follow, or break, the principle.
  3. What is the purpose of the principle? Why is it good to follow the principle? (*)
A
  1. Essentially similar to the SRP. One way to look at it is that SoC refers to modules, and their functionality, whilst the SRP refers to classes which make up a module.
  2. An example is MVC, where the program is split into a Model, View, and Controller.
  3. Many benefits, including that multiple programmers can work on the code at once (isolated from each other), the code is more reusable, more maintainable and testable.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

High Cohesion, Low Coupling

  1. Explain the principle.
  2. Give a concrete example of how you follow, or break, the principle.
  3. What is the purpose of the principle? Why is it good to follow the principle? (*)
A
  1. High Cohesion is when all components work together to solve the module’s area of responsibility, without having to work with components in other modules.
    Low Coupling means that modules should be independent of each other (i.e they should have low dependency).
2. Bad use: Class 1 och class 2 have objects of each other, and methods a in class 1, and method b in class 2 refer to each other. 
Lays the groundwork for code reuse (flexible and modular code). 
  1. High readability and maintainability.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Prefer Composition over Inheritance

  1. Explain the principle.
  2. Give a concrete example of how you follow, or break, the principle.
  3. What is the purpose of the principle? Why is it good to follow the principle? (*)
A
1. Composition: A class that references one or more objects of other classes in instance variables. This allows you to model a has-a association between objects. 
Inheritance: The mechanism of basing an object or class upon its super-class with a set of attributes and methods (i.e polymorphism - establishing is-a association).

Composition should be preferred since: It creates fewer dependencies, more robust code (since subclasses can stop functioning as intended if the superclass is modified), allows for more flexible code (Java doesn’t support multiple inheritance implementations), and allows for code reuse where inheritance isn’t suitable.

  1. Why its needed: Imagine you have a pizza shop, and need to manage all the pizzas you sell. With inheritance, you would make a base pizza with tomato sauce (call it Pizza), then create a subclass with cheese (PizzaWithCheese). That’s fine. But what if you want to make a pizza with pepperoni? You make another subclass of PizzaWithCheese, called PizzaWithCheeseAndPepperoni. How about one with olives too? Sure. You create another subclass of PizzaWithCheeseAndPepperoni. But then you decide to make only vegetarian pizzas. See the problem? It’s getting very complicated, and you have to rewrite all previous classes, because your pizza with olives is dependent on having pepperoni!
  2. See Part 1.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Law of Demeter

  1. Explain the principle.
  2. Give a concrete example of how you follow, or break, the principle.
  3. What is the purpose of the principle? Why is it good to follow the principle? (*)
A
1. Just like your mother taught you - don’t talk to strangers! LoD requires that a method m, of an object O, may only invoke (call) the methods of: O itself, m’s parameters, any objects created/instantiated within m, O’s component objects, and global variables accessible by O, in the scope of m.
That is, don’t call methods outside of the class you’re currently in!
  1. If you have a chain of messages [eg: a.getB().getC().doSomething()], you aren’t following LoD.
  2. Reduces coupling (dependencies) in the code, making it more maintainable and readable. Allows for easier adherence to other S.O.L.I.D principles since changes in one class won’t affect other classes.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Command-Query Separation Principle

  1. Explain the principle.
  2. Give a concrete example of how you follow, or break, the principle.
  3. What is the purpose of the principle? Why is it good to follow the principle? (*)
A
  1. An object’s methods should do only one of two things - they should be either:
    Queries: Which return a result (data) and don’t change the observable state of the system (not observable - have no side effects).
    Commands: Which change the state of a system (perform an action) but do not return something (ie. are observable).
  2. If you created a method which takes an array of numbers, modify the array, and then return it with just even ones, that would break the CQS. To follow it, that method should instead Mutate-by-copy (copy the original array), and return a copy with just the even numbers.
  3. Reduces the chance of unexpected behavior.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly