Skip to main content

SOLID Design Principles

SOLID: Five Simple Principles That Make Code Better

SOLID is a set of 5 object-oriented design principles that help make software easier to understand, maintain, test, and extend.

  • S - Single Responsibility - One class, one job
  • O - Open/Closed - Extend without modifying existing code
  • L - Liskov Substitution - Subtypes should work like their parent
  • I - Interface Segregation - Don't force unnecessary methods
  • D - Dependency Inversion - Depend on abstractions, not concrete classes

S — Single Responsibility Principle

A class should have one responsibility. A class should ideally have one reason to change.


Bad:

Employee

  •  Calculate salary
  •  Save employee to database
  •  Send email

These are three different responsibilities.


Better:

Employee

  • SalaryCalculator
  • EmployeeRepository
  • EmailService


Think: One class → one job.


O — Open/Closed Principle

Software should be open for extension but closed for modification.

You should be able to add new behavior without constantly changing existing, tested code.


Example:

Payment

  •    CreditCardPayment
  •    PayPalPayment
  •    ApplePayPayment

Adding ApplePayPayment shouldn't require rewriting the existing payment logic.

Think: Add new behavior without breaking old behavior.


L — Liskov Substitution Principle

It states that a subclass must be usable in place of its parent class without breaking the program.


If:

Bird

 ↓

Penguin

and Bird assumes every bird can fly, then making Penguin inherit that behavior creates a problem.


Better modeling separates the capabilities:

Bird

  • FlyingBird
    • Eagle
    • Sparrow
  • Penguin


Think: A subtype should genuinely behave like its parent.


I — Interface Segregation Principle

Don't force a class to implement methods it doesn't need.


One huge interface:

Employee

  • work()
  • eat()
  • drive()
  • fly()


A normal employee shouldn't have to implement fly().

Split interfaces:

  • Workable
  • Eatable
  • Drivable
  • Flyable


A class implements only what it actually needs.

Think: Small, focused interfaces are better than huge interfaces.


D — Dependency Inversion Principle

High-level code should depend on abstractions, not concrete implementations.

High-level modules (business logic) must not rely directly on low-level modules (database tools, network calls).


OrderService

     ↓

MySQLDatabase

(OrderService is tightly tied to MySQL.)


          Database

          Interface

             ↑

       ┌─────┴─────┐

       ↓           ↓

    MySQL       SQLServer

       ↑

       │

 OrderService

(OrderService depends on the Database interface, not directly on MySQL.)


Think: Depend on what something does, not exactly how it does it.


Easy way to remember

  • S → Single job
  • O → Open to extension
  • L → Like the parent
  • I → Interfaces should be small
  • D → Depend on abstractions


SOLID is primarily an object-oriented software design principle set—not a complete system-design methodology.

Comments

Popular posts from this blog

Customer Retention Metrics (Growth marketing)

Customer retention metrics are key performance indicators (KPIs) that measure how effectively a business keeps its customers over time, with common examples including Customer Retention Rate, Customer Churn Rate, and Customer Lifetime Value (CLV). These metrics help assess customer satisfaction, identify areas for improvement, and predict future revenue 1. Customer Retention Rate How to calculate and improve customer retention rate (+ formula) Customer retention rate measures the number of customers a company retains over a given period of time. Calculate retention rate with this formula: [(E-N)/S] x 100 = CRR. Identify the time frame you want to study Collect the number of existing customers at the start of the time period (S) Find the number of total customers at the end of the time period (E) Determine the number of new customers added within the time period (N) 2. Customer Churn Rate Your customer churn rate is simply the inverse of your customer retention rate. For instance,...

Customer Lifetime Value (CLV or LTV)

Customer Lifetime Value is the estimated total value a customer brings to a business over the entire duration of their relationship. CLV CLV (Customer Lifetime Value), LTV (Lifetime Value), and LCV (Lifetime Customer Value) are often used interchangeably in marketing and business analytics, and they all have the same meaning. Basic CLV Formula CLV = Average Purchase Value × Purchase Frequency × Customer Lifespan  Example Average purchase value = $100 Purchases per year = 5 Customer lifespan = 4 years CLV = 100 × 5 × 4 = $2,000 More Accurate Formula Many companies include gross margin. CLV = Average Revenue per Customer × Gross Margin × Customer Lifetime Example : Revenue = $1,200 Gross Margin = 40% Lifetime = already included in revenue CLV = 1,200 × 40% = $480 profit Subscription Business Formula For SaaS businesses: CLV = ARPU × Gross Margin ÷ Churn Rate Example Monthly ARPU = $40 Gross Margin = 80% Monthly Churn = 4% CLV = 40 × 0.80 ÷ 0.04 = $800 ...

Strategic Analysis Framework - PESTEL

Why Every Business Strategy Should Start with a PESTEL Analysis The PESTEL Framework is a strategic analysis tool used to evaluate the external macro-environmental factors that can affect an organization, industry, or project. PESTEL stands for: P – Political Government actions and political stability that influence business operations. Eg Tax policies, trade regulations, labor laws, political stability, government subsidies E – Economic Economic conditions affecting purchasing power and business performance. Eg Inflation, interest rates, unemployment, economic growth/shrink, exchange rates S – Social Cultural and demographic trends influencing consumer behavior. Eg Population growth, lifestyle changes, education levels, consumer attitudes T – Technological Technological developments impacting products, services, and operations. Eg Automation, AI, R&D, digital transformation, cybersecurity E – Environmental Ecological and environmental issues affecting businesses. ...