This page is a work in progress… a compilation of information about object-oriented design principles and values.
S.O.L.I.D. Class Design Principles
Collected by Robert C. Martin for his book “Applying Principles and Patterns”
Single Responsibility Principle (SRP)
A class should have only one reason to change. For example, if a change to the business rules causes a class to change, then a change to the database schema, GUI, report format, or any other segment of the system should not force that class to change.
- Head First Design patterns page 185, 336, 339, 367
Open/Closed Principle (OCP)
Software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification.
- Head First Design patterns page 86-87, 407
Liskov substitution principle (LSP)
Subtypes must be substitutable for their base types. If class A inherits from class B, then wherever you can use A you should be able to use B. E.g. remember that square is not necessarily a rectangle! When extending: Preconditions cannot be straightened, Postconditions cannot be loosened, visible Invariants cannot be changed (?). Invariants: users depend on this both before and after sending a message. Use a proper set-based inheritance relationship. Not following set semantics is very risky. Subsumption Rule: A reference to a subtype can be used in any context where a reference to a super type is expected. This principle extremely limits what SHOULD be done with the pure extension (inheritance) mechanism. Do not follow at your own risk.
- Agile Principles and Patterns page ?
Interface Segregation Principle (ISP)
The dependency of one class to another one should depend on the smallest possible interface.
Dependency Inversion Principle (DIP)
Depend upon abstractions (interfaces), not upon concrete classes.
- Head First Design patterns page 139-143
Other Important Ones
Law of Demeter
aka Principle of Least Knowledge: Only talk to your friends
Any method M of an object O may only invoke the methods of the following kinds of objects:
- its arguments/parameters
- any objects it creates/instantiates
- its direct component objects
- Head First Design patterns page 265
Don’t call us, we’ll call you.
- Head First Design patterns page 296
Don’t Repeat Yourself (DRY)
- Pragmatic Programmer page 27
Program to an interface, not an implementation
Maybe just another way to say dependency inversion.
- Head First Design patterns page 11, 110-111, 243, 335
You Ain’t Gonna Need It (YAGNI)
Do not add code that you “think will be used later”. Add code only at “Last Responsible Moment”
Keep It Simple, Stupid (KISS)
What is the simplest thing that could possibly work?
Encapsulation and Information Hiding
Separation of Concerns (SoC)
- Head First Design patterns page 53
Convention over Configuration (CoC)
Command-query separation (CQS)
 Design by Contract (DbC)
Dependency Injection (DI)
Inversion of Control (IoC)
Avoid Fragile Baseclass
What is Identity
Intention Revealing Names
Zero Friction Development
Separate what varies from what stays the same. Also known as Serenity Principle.
- Head First Design patterns page 9, 111
Composition over inheritance
- Head First Design patterns page 23, 243, 397
Common Closure Principle
Classes that change together must be placed in the same package.