Object Oriented Design Cheat Sheet
by David Harris (david) via cheatography.com/387/cs/179/
SOLID Other Principles (cont) Class Associations: Generalization
Single Responsibility Principle Design By Contract
A class changes for only one reason And then write tests
Open/Closed Principle Avoid Fragile Base Class
"Is-A" relationship (inheritance).
A class should be open for extension, closed Treat Base like a public API
Example: Porsche is a Car
for editing
Common Closure Principle
Liskov's Substitution Principle Classes that change together, stay together Class Associations: Realization
Derived types should cleanly and easily
replace base types Common Refactorings
Interface Segregation Principle Encapsulate Field
Favor multiple single-purpose interfaces Generalize Type One class implements behavior that is
over composite abstractly defined in another class.
Type-Checking ⇒ State/Strategy
Example: An Animal may Move(), but a Duck
Dependency Inversion Principle Conditional ⇒ Polymorphism
would move by waddling
Concrete classes depend on abstractions, Extract Method
not vice-versa
Extract Class Class Associations: Dependency
Move/Rename Method or Field
Other Principles
Move to Superclass/Subclass
Don't Repeat Yourself (DRY)
http://martinfowler.com/refactoring/catalog
Duplication should be abstracted One class weakly depends on another.
Example: Car uses Highway
Law of Demeter Class Associations: Association
Only talk to related classes
Access Modifiers
Hollywood Principle
Private Only inside the same class
"Don't call us, we'll call you"
instance
Two objects have some sort of relationship to
You Ain't Gonna Need It Protected Inside same or derived class
each other.
Only code what you need now Example: Car uses Highway instances
Keep It Simple, Stupid Public All other classes
Class Associations: Aggregation linking/referencing the class
Favor clarity over cleverness
Internal Only other classes in the same
Convention Over Configuration
assembly
Defaults cover 90% of uses
Protected All classes in same assembly, or
Encapsulation Internal derived classes in other assembly
An association where one object has-a (owns
What happens in Vegas... a) different object. Static Accessible on the class itself (can
Example: Car has a Driver combine with other accessors)
Class Associations: Composition
An aggregation with dependency - objects are
mutually destroyed/created.
Example: Car has an Engine
By David Harris (david) Published 9th January, 2012. Sponsored by CrosswordCheats.com
cheatography.com/david/ Last updated 5th June, 2014. Learn to solve cryptic crosswords!
Page 1 of 2. http://crosswordcheats.com
Object Oriented Design Cheat Sheet
by David Harris (david) via cheatography.com/387/cs/179/
Design Patterns (GoF)
Abstract Factory Creational
Builder Creational
Factory Method Creational
Prototype Creational
Singleton Creational
Adapter Structural
Bridge Structural
Composite Structural
Decorator Structural
Facade Structural
Flyweight Structural
Proxy Structural
Chain of Responsibility Behavioral
Command Behavioral
Interpreter Behavioral
Iterator Behavioral
Mediator Behavioral
Memento Behavioral
Observer Behavioral
State Behavioral
Strategy Behavioral
Template Method Behavioral
Visitor Behavioral
By David Harris (david) Published 9th January, 2012. Sponsored by CrosswordCheats.com
cheatography.com/david/ Last updated 5th June, 2014. Learn to solve cryptic crosswords!
Page 2 of 2. http://crosswordcheats.com