0% found this document useful (0 votes)
115 views

SOLID Presentation

This document provides summaries of key object-oriented principles: - Cohesion refers to how strongly related a module's responsibilities are, while coupling refers to how dependent modules are on each other. High cohesion and low coupling are goals. - The Single Responsibility Principle states that classes should have one responsibility to minimize the likelihood of changes. - The Open-Closed Principle aims for entities that can be extended without modifying existing code, such as via inheritance. - The Liskov Substitution Principle requires that subclasses uphold base class behaviors and invariants. - The Interface Segregation Principle calls for dividing interfaces into specific smaller interfaces to avoid unnecessary dependencies. - The Dependency

Uploaded by

Vener Guevarra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
115 views

SOLID Presentation

This document provides summaries of key object-oriented principles: - Cohesion refers to how strongly related a module's responsibilities are, while coupling refers to how dependent modules are on each other. High cohesion and low coupling are goals. - The Single Responsibility Principle states that classes should have one responsibility to minimize the likelihood of changes. - The Open-Closed Principle aims for entities that can be extended without modifying existing code, such as via inheritance. - The Liskov Substitution Principle requires that subclasses uphold base class behaviors and invariants. - The Interface Segregation Principle calls for dividing interfaces into specific smaller interfaces to avoid unnecessary dependencies. - The Dependency

Uploaded by

Vener Guevarra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 56

Object Oriented Principles - Quiz

COHESION:

“A measure of how strongly-related and focused the various


responsibilities of a software module are” - Wikipedia

COUPLING:

“The degree to which each program module relies on each one of the
other modules” – Wikipedia

ENCAPSULATION:

“The hiding of design decisions in a computer program that are most likely
to change” - Wikipedia
S.O.L.I.D. Principles

SRP: SINGLE RESPONSIBILITY PRINCIPLE

OCP: OPEN CLOSED PRINCIPLE

LSP: LISKOV SUBSTITUTION PRINCIPLE

ISP: INTERFACE SEGREGATION PRINCIPLE

DIP: DEPENDENCY INVERSION PRINCIPLE


The Single Responsibility Principle
SRP: The Single Responsibility Principle

The Single Responsibility Principle states that every object should


have a single responsibility, and that responsibility should be
entirely encapsulated by the class.
Wikipedia

There should never be more than one reason for a class to change.
Robert C. “Uncle Bob” Martin
Cohesion and Coupling

• Cohesion: how strongly-related and focused are the various


responsibilities of a module

• Coupling: the degree to which each program module relies


on each one of the other modules

Strive for low coupling and high


cohesion!
Responsibilities are Axes of Change

• Requirements changes typically map to responsibilities

• More responsibilities == More likelihood of change

• Having multiple responsibilities within a class couples


together these responsibilities

• The more classes a change affects, the more likely the


change will introduce errors.
What is a Responsibility?

• “a reason to change”

• A difference in usage scenarios from the client’s


perspective

• Multiple small interfaces (follow ISP) can help to


achieve SRP
SRP: Single Responsibility
Example App: Read A Flat File And Send An Email
SRP: SINGLE RESPONSIBILITY

Example App New Requirements: Send From Non-WinForms App. Read XML Or Flat File
Example App: A Better Structure
Summary

• Following SRP leads to lower coupling and higher cohesion

• Many small classes with distinct responsibilities result in a more


flexible design

• Related Fundamentals:
oOpen/Closed Principle
oInterface Segregation Principle
oSeparation of Concerns

• Recommended Reading:
oClean Code by Robert C. Martin [http://amzn.to/Clean-Code]
The Open / Closed Principle
OCP: The Open/Closed Principle

• The Open / Closed Principle states that software


entities (classes, modules, functions, etc.) should
be open for extension, but closed for modification

Wikipedia
The Open / Closed Principle

Open to Extension
New behavior can be added in the future

Closed to Modification
Changes to source or binary code are not required

Dr. Bertrand Meyer originated the OCP term in his 1988 book,
Object Oriented Software Construction
Change behavior without changing code?

Rely on abstractions
No limit to variety of implementations of each
abstraction

Abstractions include:
• Interfaces
• Abstract Base Classes

In procedural code, some level of OCP can be


achieved via parameters
The Problem

• Adding new rules require changes to the calculator every time

• Each change can introduce bugs and requires re-testing, etc.

• We want to avoid introducing changes that cascade through many


modules in our application

• Writing new classes is less likely to introduce problems


oNothing depends on new classes (yet)
o New classes have no legacy coupling to make them hard to design or test
Three Approaches to Achieve OCP

• Parameters (Procedural Programming)


oAllow client to control behavior specifics via a parameter
oCombined with delegates/lambda, can be very powerful approach

• Inheritance / Template Method Pattern


oChild types override behavior of a base class (or interface)

• Composition / Strategy Pattern


oClient code depends on abstraction
oProvides a “plug in” model
oImplementations utilize Inheritance; Client utilizes Composition
When do we apply OCP?

• Experience Tells You


If you know from your own experience in the problem
domain that a particular class of change is likely to recur,
you can apply OCP up front in your design
Otherwise – “Fool me once, shame on you; fool me twice, shame on me”
• Don’t apply OCP at first
• If the module changes once, accept it.
• If it changes a second time, refactor to achieve OCP

Remember TANSTAAFL
• There Ain’t No Such Thing As A Free Lunch
• OCP adds complexity to design
• No design can be closed against all changes
Summary

• Conformance to OCP yields flexibility, reusability, and maintainability

• Know which changes to guard against, and resist premature abstraction

• Related Fundamentals:
oSingle Responsibility Principle
oStrategy Pattern
oTemplate Method Pattern
• Recommended Reading:
oAgile Principles, Patterns, and Practices by Robert
C. Martin and Micah Martin
[http://amzn.to/agilepppcsharp]
The Liskov Substitution Principle
LSP: The Liskov Substitution Principle

The Liskov Substitution Principle states that


Subtypes must be substitutable for their base types.

Named for Barbara Liskov, who first described the principle in 1988.
Substitutability

Child classes must not:

1) Remove base class behavior


2) Violate base class invariants

And in general must not require calling code to know they are
different from their base type.
Inheritance and the IS-A Relationship

Naïve OOP teaches use of IS-A to describe child classes’ relationship


to base classes

LSP suggests that IS-A should be replaced with


IS-SUBSTITUTABLE-FOR
LSP: Liskov Substitution Principle

EXAMPLE APP: VIOLATING LSP WITH DATABASE CONNECTION INFO


LSP: Liskov Substitution Principle
EXAMPLE APP: CORRECT I N G FOR LSP – MOVE T HE DA T A B A SE REA DER
Invariants

• Consist of reasonable assumptions of behavior by clients

• Can be expressed as preconditions and postconditions for methods

• Frequently, unit tests are used to specify expected behavior of a method or


class

• Design By Contract is a technique that makes defining these pre- and post-
conditions explicit within code itself.

• To follow LSP, derived classes must not violate any constraints defined (or
assumed by clients) on the base classes
When do we fix LSP?

• If you notice obvious smells like those shown

• If you find yourself being bitten by the OCP


violations LSP invariably causes
The Interface Segregation Principle
ISP: The Interface Segregation Principle

The Interface Segregation Principle states that


Clients should not be forced to depend on methods
they do not use.
Agile Principles, Patterns, and Practices in C#

Corollary:
Prefer small, cohesive interfaces to “fat” interfaces
ISP: Interface
Segregation Principle
“This principle deals with the disadvantages of ‘fat’ interfaces. Classes
that have ‘fat’ interfaces are classes whose interfaces are not cohesive.
In other words, the interfaces of the class can be broken up into groups
of member functions. Each group serves a different set of clients. Thus
some clients use one group of member functions, and other clients use
the other groups.”

- Robert Martin
ISP: Interface
Segregation Principle
Example App: Clarifying The Email Sender and Message Info Parsing
The Dependency Inversion Principle
DIP: Dependency Inversion
Principle

“What is it that makes a design rigid, fragile and immobile? It is the interdependence of the modules within that design. A design is rigid if it cannot be easily changed. Such rigidity is due to the fact that a single change to heavily interdependent software begins a cascade of changes in dependent modules.”

- Robert Martin
DIP: The Dependency Inversion Principle

High-level modules should not depend on low-level


modules. Both should depend on abstractions.

Abstractions should not depend on details. Details


should depend on abstractions.
DIP: DEPENDENCY
INVERSION PRINCIPLE
DIP: Dependency
Inversion Principle
DIP: DEPENDENCY
INVERSION PRINCIPLE
DIP: Dependency
Inversion
Principle
What are dependencies?

• Framework
• Third Party Libraries
• Database
• File System
• Email
• Web Services
• System Resources (Clock)
• Configuration
• The new Keyword
• Static methods
• Thread.Sleep
• Random
Dependency Injection

• Dependency Injection is a technique that is used to allow calling


code to inject the dependencies a class needs when it is
instantiated.

• The Hollywood Principle


o“Don’t call us; we’ll call you”

• Three Primary Techniques


o Constructor Injection
oProperty Injection
oParameter Injection

• Other methods exist as well


Summary
• Depend on abstractions.

• Don’t force high-level modules to depend on low-level modules through direct


instantiation or static method calls

• Declare class dependencies explicitly in their constructors

• Inject dependencies via constructor, property, or parameter injection

• Related Fundamentals:
o Single Responsibility Principle
o Interface Segregation Principle
o Façade Pattern
o Inversion of Control Containers

• Recommended Reading:
o Agile Principles, Patterns, and Practices by Robert C. Martin and Micah Martin
[http://amzn.to/agilepppcsharp]
o http://www.martinfowler.com/articles/injection.html
The Problem

• Dependencies Flow Toward Infrastructure

• Core / Business / Domain Classes Depend on Implementation


Details

• Result
o Tight coupling
o No way to change implementation details without recompile (OCP violation)
o Difficult to test
Dependency Injection

• Dependency is transitive
o If UI depends on BLL depends on DAL depends on Database Then
*everything* depends on the Database

• Depend on abstractions (DIP)

• Package interfaces (abstractions) with the client (ISP)

• Structure Solutions and Projects so Core / BLL is at center, with


fewest dependencies
Summary

• Don’t Depend on Infrastructure Assemblies from Core

• Apply DIP to reverse dependencies

• Related Fundamentals:
o Open Closed Principle
o Interface Segregation Principle
o Strategy Pattern

• Recommended Reading:
o Agile Principles, Patterns, and Practices by Robert C. Martin and Micah Martin
[http://amzn.to/agilepppcsharp]
o http://www.martinfowler.com/articles/injection.html
Don’t Repeat Yourself

“Every piece of knowledge must have a single, unambiguous


representation in the system.”
The Pragmatic Programmer

“Repetition in logic calls for abstraction. Repetition in


process calls for automation.”
97 Things Every Programmer Should
Know

Variations include:
• Once and Only Once
• Duplication Is Evil (DIE)

You might also like