Computer Science
Grade 9
20 min
Object-Oriented Design Principles: SOLID
Introduction to the SOLID principles of object-oriented design (Single Responsibility, Open/Closed, Liskov Substitution, Interface Segregation, Dependency Inversion).
Tutorial Preview
1
Introduction & Learning Objectives
Learning Objectives
Define what a design principle is and why it's important in programming.
List and explain each of the five SOLID principles using a simple analogy.
Identify a violation of the Single Responsibility Principle in a simple code example.
Identify a violation of the Open/Closed Principle in a simple code example.
Refactor a small class to better follow the Single Responsibility Principle.
Explain how following SOLID principles leads to more maintainable and flexible code.
Have you ever tried to fix one thing in a Jenga tower and had the whole thing collapse? 🧱 What if your code did the same thing?
In this lesson, you'll learn the SOLID principles, which are five golden rules for designing software. Think of them as a professional blueprint for buil...
2
Key Concepts & Vocabulary
TermDefinitionExample
Design PrincipleA general guideline or rule of thumb for writing well-structured, easy-to-understand, and easy-to-maintain code.A common design principle is 'Don't Repeat Yourself' (DRY), which means you should avoid writing the same code in multiple places.
Single Responsibility Principle (SRP)A class should have only one job or one reason to change. It should be focused on doing one thing very well.A `Calculator` class should only perform calculations. It shouldn't also be responsible for printing the results to a file or sending them over the internet. Those are separate jobs for other classes.
Open/Closed Principle (OCP)A class should be 'open for extension' but 'closed for modification'. This means you should be able to ad...
3
Core Syntax & Patterns
The 'One Job' Rule (SRP)
class MyClass:
# All methods and properties here
# should relate to a SINGLE responsibility.
When designing a class, ask yourself: 'What is the single, primary job of this class?' If your answer includes the word 'and' (e.g., 'It calculates data AND saves it to a file'), you are likely violating this principle. Split the class into smaller, more focused classes.
The 'No Modification' Pattern (OCP)
if (type == 'A') { ... }
else if (type == 'B') { ... }
// To add type 'C', you must modify this code. This is bad.
Watch out for long `if/elif/else` or `switch` statements that check an object's type. This is a sign that your code is not 'closed for modificat...
4 more steps in this tutorial
Sign up free to access the complete tutorial with worked examples and practice.
Sign Up Free to ContinueSample Practice Questions
Challenging
A `DocumentProcessor` class has a method `process(doc)` that contains a large `if/elif` block to handle `PDF`, `Word`, and `Text` document types differently. Which two SOLID principles are most directly violated by this design?
A.LSP and DIP
B.SRP and OCP
C.ISP and LSP
D.DIP and ISP
Challenging
To fix a `PaymentProcessor` class that uses `if/elif` for payment types ('credit', 'paypal', 'crypto'), you decide to use the Open/Closed Principle. Which of these describes the best design?
A.Create one giant `Payment` class that handles all types.
B.Create a `PaymentHandler` interface with a `pay()` method, and then create `CreditCardHandler`, `PayPalHandler`, and `CryptoHandler` classes that implement it.
C.Make `CreditCard`, `PayPal`, and `Crypto` all inherit from the `PaymentProcessor` class.
D.Move the `if/elif` logic into a separate function outside of any class.
Challenging
A high-level `OrderService` class directly creates and uses a low-level `MySQLDatabase` object. How would you apply the Dependency Inversion Principle to make the `OrderService` more flexible and testable?
A.Rename `MySQLDatabase` to `GenericDatabase` but keep the code the same.
B.Move all the database code from `MySQLDatabase` directly into the `OrderService` class.
C.Create an `IDatabase` interface, make `MySQLDatabase` implement it, and have `OrderService` depend on the `IDatabase` interface instead.
D.Delete the `MySQLDatabase` class and use a simple text file for storage.
Want to practice and check your answers?
Sign up to access all questions with instant feedback, explanations, and progress tracking.
Start Practicing Free