Lab 8: Object–Oriented Design

In this lab we will be talking about some common and important concepts and patterns in object–oriented design. These are not specific to Java, but we will of course be using Java as the illustrating language.

The SOLID Principle

SOLID is an acronym for five of the most important ideas in object–oriented design:

Single Responsibility Principle

Open/Closed Principle

Liskov Substitution Principle

Interface Segregation Principle

Dependency Inversion Principle

Unfortunately, all of these names are terrible. Let’s walk through what they actually mean.

Single Responsibility Principle

The single responsibility principle says that every class should only do one thing. Put more concretely, it means that only one change in the specification of the software should be able to induce a change in the implementation of the class. Here is an example of some code that fails to follow the single responsibility principle, followed by code that does follow the principle:

In this example, the code’s behavior changes if given a specific subtype of some specific type. This is a violation of Liskov.

Interface Segregation Principle

The Interface Segregation Principle says that users of an interface shouldn’t have to depend on parts of the interface they don’t use. This is quite similar to the Single Responsibility Principle, but from the perspective of the user. Here is an example that doesn’t follow this principle:

Notice that the IWorker construct has been added, so that the Manager is no longer tied to the particular type of worker.

Conclusion

These are the components of SOLID. Understanding these is important to the creation of good OO programs, and fulfilling the promise of OO programming facilitating the creation of large programs.

Assignment

For this lab, please write your own original examples of each of these principles, along with explanations in your own words of what the principle is, and why it’s desirable. Submit the examples as .java files, and the explanations as .pdf files. Due by Tuesday, November 14th at 11:59pm.