Fundamentals 11 min read

Master the 7 Essential SOLID Principles for Clean, Maintainable Code

This article introduces the classic GOF design patterns and then explains seven core software design principles—Single Responsibility, Open/Closed, Liskov Substitution, Dependency Inversion, Interface Segregation, Composition over Inheritance, and the Law of Demeter—showing how they improve code reliability, readability, and extensibility.

macrozheng
macrozheng
macrozheng
Master the 7 Essential SOLID Principles for Clean, Maintainable Code

1. Introduction

In 1994 the "Gang of Four" (Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides) published "Design Patterns – Elements of Reusable Object‑Oriented Software", introducing 23 classic design patterns divided into Creational, Structural, and Behavioral categories, and later J2EE patterns.

Design patterns are not new technologies but accumulated best‑practice solutions that help less‑experienced developers learn software design quickly.

2. Methodology

2.1 Single Responsibility Principle

The SRP states that a class should have only one responsibility, e.g., encapsulating an

http

request in a utility class so callers can reuse it easily.

It encourages fine‑grained responsibilities and keeps related concerns separate, as seen in MVC separation of data and business logic.

2.2 Open/Closed Principle

Software should be open for extension but closed for modification. Adding new functionality should require new code rather than changing existing code.

Example: different companies require different encryption algorithms (

rsa

,

aes

,

md5

). By defining an abstract encryption interface and implementing concrete strategies, new algorithms can be added without modifying existing code.

2.3 Liskov Substitution Principle

Subclasses may extend parent behavior but must not alter the original contract; overriding methods should preserve expected semantics.

Violating LSP can cause serialization errors when a subclass introduces a field with a different type than the base class.

2.4 Dependency Inversion Principle

High‑level modules should depend on abstractions, not concrete implementations; this supports the Open/Closed Principle.

High‑level modules should not depend on low‑level modules; both depend on abstractions.

Abstractions should not depend on details.

Details should depend on abstractions.

In practice, define service interfaces and let implementations be injected (e.g., in MVC Service layer), reducing coupling and improving parallel development.

2.5 Interface Segregation Principle

Prefer many fine‑grained interfaces over a single bulky one, allowing clients to depend only on the methods they use.

When integrating with many insurance providers, split the insurance operations into separate interfaces per action (e.g., policy, claim) rather than a monolithic interface.

2.6 Composition over Inheritance

Favor composition/aggregation instead of inheritance. In Spring MVC, use IoC to inject dependencies rather than inheriting from concrete classes.

<code>@Component
public class AService {
    /** ioc dependency injection */
    @Autowired
    private BService bService;
    // business logic...
}
</code>

Composition keeps code layers clear and easier to maintain.

2.7 Law of Demeter (Least Knowledge Principle)

A class should interact with as few other classes as possible, keeping modules independent.

Typical Java bean with simple getters/setters exemplifies this; business logic should reside in service layers, not in data entities.

3. Summary

The seven principles above—SRP, OCP, LSP, DIP, ISP, composition over inheritance, and the Law of Demeter—are distilled from extensive real‑world experience and provide a practical methodology for building reliable, maintainable software systems.

design patternssoftware architectureobject-orientedSOLIDprinciples
macrozheng
Written by

macrozheng

Dedicated to Java tech sharing and dissecting top open-source projects. Topics include Spring Boot, Spring Cloud, Docker, Kubernetes and more. Author’s GitHub project “mall” has 50K+ stars.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.