Design Principles Exercise
Tip
These exercises are examples of the kind of questions you can expect in the quiz.
Exercise 1¶
Consider the following partial design of an application.
Can you identify cases where SOLID design principles is applied/violated?
- Single Responsibility Principle
- There seems to be a good separation and isolation of responsibilities between the
CoffeeApp
and different types ofCoffeeMachine
.
- There seems to be a good separation and isolation of responsibilities between the
- Open/Closed Principle
- Adding new types of coffee machines will not require any modification to existing ones. (I.e. app is open for extension but closed for modification.)
- Liskov Substitution Principle
- There is not enough information to consider the application of this principle. Based on the diagram, it seems every subtype is substitutable for their parent type.
- Interface Segregation Principle
- The responsibilities of
CoffeeMachine
is broken down intoFilterCoffeeMachine
andEspressoCoffeeMachine
(more granular interfaces to prevent a bloatedCoffeeMachine
interface).
- The responsibilities of
- Dependency Inversion Principle
- High-level classes (e.g.
CoffeeApp
) does not depend on low-level classes (e.g.BasicCoffeeMachine
); instead they both depend on abstractions (i.e.CoffeeMachine
interface).
- High-level classes (e.g.
Exercise 2¶
Download this codebase which implements the game of Blackjack. Consider the high-level design of the application.
Can you identify cases where SOLID design principles is applied/violated?
- Single Responsibility Principle
- A
Card
class holds information about a playing card as well as how it is displayed in the application's GUI (i.e.ImageIcon
field); it might make for a better design if the GUI related stuff were not tightly coupled together with model entities. - On the previous note, the application's design can certainly improve (in terms of separation of responsibilities) if the MVC pattern were to be applied to it.
- A
- Open/Closed Principle
- The
Deck
andHand
classes are tightly coupled to the logic of the game (i.e. the constructor ofDeck
creates 52 standard playing cards where the value assigned to each card is based on the rules of Blackjack); in addition to violating the Single Responsibility Principle, any modification to the game rules, or extension to the application (e.g. to support different versions of Blackjack or different card game) will (likely) require modifying theDeck
andHand
classes. The latter is a violation of the Open/Closed Principle.
- The
- Liskov Substitution Principle
- There is no violation nor application of this principle (sub-typing is not employed here).
- Interface Segregation Principle
- There is no violation nor application of this principle (abstraction--in the form of creating interfaces--is not employed here).
-
Dependency Inversion Principle
- High-level classes such as
Blackjack
directly depend on the low-level classes likeDeck
andHand
. It might make for a better design if an abstraction were to be introduced between them to change the direction of dependency:
- High-level classes such as