24
S.O.L.I.D.

Solid

Embed Size (px)

Citation preview

Page 1: Solid

S.O.L.I.D.

Page 2: Solid

I’m not gonna touch it

Page 3: Solid

S.O.L.I.D

Single ResponsibilityOpen-ClosedLiskov SubstitutionInterface SegregationDependency Inversion

Robert C. Martin

Page 4: Solid

Single Responsibility Principle

Class should have one, and only one, reason to change.

Page 5: Solid

Single Responsibility Principle

Adherence to the SRP helps to improve maintainability. When object encapsulates multiple responsibilities changes to the object for one of responsibilities can negatively impact the others.

Page 6: Solid

Single Responsibility Principle

Object role stereotypes:● information holder● structurer● service provider● controller● coordinator● interfacer

Page 7: Solid

Single Responsibility Principle

Violations:● create user and send email in one class● struts action class that handles two

pages/dialogs

Page 8: Solid

Open-Closed Principle

Class should be open for extension, but closed for modification

Page 9: Solid

Open-Closed Principle

The reason is that if you modify a class, you’ll likely break the API/Contract of the class, which means that the classes that depend on it might fail when you do so. If you instead inherit the class to add new features, the base contract is untouched and it’s unlikely that dependent classes will fail.

Page 10: Solid

Open-Closed Principle

Violations:● if/else, switch statements● modify 3rd party lib sources

Page 11: Solid

Liskov Substitution Principle

Subtypes must be substitutable for their base types

Page 12: Solid

Liskov Substitution Principle

Objects in a program should be replaceable with instances of their subtypes without altering the correctness of the program.

Any method that takes class X as a parameter must be able to work with any subclasses of X

Page 13: Solid

Liskov Substitution Principle

This isn’t always possible to avoid LSP violations.Mitigating strategies:● TDD● Design by Contract● Favor object composition over class

inheritance

Page 14: Solid

Liskov Substitution Principle

Violations:● Rectangle example● .NET 2.0 Collections (IsReadOnly)

Page 15: Solid

Interface Segregation Principle

Client should not be forced to depend on an interface that it does not need/use

Page 16: Solid

Interface Segregation Principle

Large interfaces make it harder to extend smaller parts of the system.

Many client-specific interfaces are better than one general-purpose interface.

Page 17: Solid

Interface Segregation Principle

Violations:● empty method● method throws exception

Page 18: Solid

Dependency Inversion Principle

High-level modules should not depend on low-level modules. Both should depend upon abstractions.

Abstractions should not depend upon details. Details should depend upon abstractions

Page 19: Solid

Dependency Inversion Principle

Instead of lower level modules defining an interface that higher level module depend on, higher level modules define an interface that lower level module implement

Page 20: Solid

Dependency Inversion Principle

Violation:

Page 21: Solid

Dependency Inversion Principle

Solution:

Page 22: Solid

Other principles?

DRY/DIEKISSYAGNI

Page 23: Solid

Alternative?

SingletonTight couplingUntestabilityPremature optimizationIndescriptive namingDuplication

Page 24: Solid

Questions

?