SlideShare a Scribd company logo
1 of 108
Download to read offline
S
O
L
I
D
Single Responsibility
Open-Closed
Liskov Substitution
Interface Segregation
Dependency Inversion
Single Responsibility
Open-Closed
Liskov Substitution
Interface Segregation
Dependency Inversion
In object-oriented programming, the single
responsibility principle states that every object
should have a single responsibility, and that
responsibility should be entirely encapsulated by
the class. All its services should be narrowly
aligned with that responsibility.
http://en.wikipedia.org/wiki/Single_responsibility_principle
The single responsibility principle is a computer
programming principle that states that every
module or class should have responsibility over a
single part of the functionality provided by the
software, and that responsibility should be entirely
encapsulated by the class. All its services should be
narrowly aligned with that responsibility.
http://en.wikipedia.org/wiki/Single_responsibility_principle
The single responsibility principle is a computer
programming principle that states that every
module or class should have responsibility over a
single part of the functionality provided by the
software, and that responsibility should be entirely
encapsulated by the class. All its services should be
narrowly aligned with that responsibility.
http://en.wikipedia.org/wiki/Single_responsibility_principle
The term was introduced by Robert C. Martin [...].
Martin described it as being based on the principle
of cohesion, as described by Tom DeMarco in his
book Structured Analysis and Systems Specification.
http://en.wikipedia.org/wiki/Single_responsibility_principle
Cohesion is a measure of the strength of
association of the elements inside a module.
A highly cohesive module is a collection of
statements and data items that should be treated
as a whole because they are so closely related.
Any attempt to divide them up would only result
in increased coupling and decreased readability.
µονόλιθος
This is the Unix philosophy:
Write programs that do one
thing and do it well.Write
programs to work together.
Doug McIlroy
In McIlroy's summary, the hard
part is his second sentence:
Write programs to work
together.
John D Cook
In the long run every
program becomes rococo
— then rubble.
Alan Perlis
We refer to a sound line of reasoning,
for example, as coherent. The thoughts
fit, they go together, they relate to each
other.
GlennVanderburg
http://vanderburg.org/blog/2011/01/31/cohesion.html
This is exactly the characteristic of a
class that makes it coherent: the pieces
all seem to be related, they seem to
belong together, and it would feel
somewhat unnatural to pull them apart.
Such a class exhibits cohesion.
GlennVanderburg
http://vanderburg.org/blog/2011/01/31/cohesion.html
#include <stdlib.h>
Every class should
embody only about 3–5
distinct responsibilities.
Grady Booch
Object Solutions
One of the most foundational
principles of good design is:
Gather together those things
that change for the same reason,
and separate those things that
change for different reasons.
This principle is often known as
the single responsibility principle,
or SRP. In short, it says that a
subsystem, module, class, or even
a function, should not have more
than one reason to change.
Single Responsibility
Open-Closed
Liskov Substitution
Interface Segregation
Dependency Inversion
Interface inheritance (subtyping) is used whenever
one can imagine that client code should depend on
less functionality than the full interface.
Services are often partitioned into several unrelated
interfaces when it is possible to partition the clients
into different roles.
"General Design Principles"
CORBAservices
The dependency should
be on the interface,
the whole interface,
and nothing but the
interface.
This is exactly the characteristic of a
class that makes it coherent: the pieces
all seem to be related, they seem to
belong together, and it would feel
somewhat unnatural to pull them apart.
Such a class exhibits cohesion.
GlennVanderburg
http://vanderburg.org/blog/2011/01/31/cohesion.html
This is exactly the characteristic of an
interface that makes it coherent: the
pieces all seem to be related, they seem
to belong together, and it would feel
somewhat unnatural to pull them apart.
Such an interface exhibits cohesion.
public interface LineIO
{
String read();
void write(String lineToWrite);
}
public interface LineReader
{
String read();
}
public interface LineWriter
{
void write(String lineToWrite);
}
Single Responsibility
Open-Closed
Liskov Substitution
Interface Segregation
Dependency Inversion
Concept Hierarchies
The construction principle involved is best
called abstraction; we concentrate on features
common to many phenomena, and we abstract
away features too far removed from the
conceptual level at which we are working.
Ole-Johan Dahl and C A R Hoare
"Hierarchical Program Structures"
A type hierarchy is composed of subtypes and
supertypes. The intuitive idea of a subtype is
one whose objects provide all the behavior of
objects of another type (the supertype) plus
something extra.
Barbara Liskov
"Data Abstraction and Hierarchy"
What is wanted here is something like the
following substitution property: If for each
object o1 of type S there is an object o2 of type
T such that for all programs P defined in terms
of T, the behavior of P is unchanged when o1 is
substituted for o2, then S is a subtype of T.
Barbara Liskov
"Data Abstraction and Hierarchy"
https://msdn.microsoft.com/en-us/library/ms173147
E.g., Python's dict class, which is a
hashed mapping container
E.g., Python's OrderedDict class,
which preserves order of insertion
public class RecentlyUsedList
{
...
public int Count
{
get ...
}
public string this[int index]
{
get ...
}
public void Add(string newItem) ...
...
}
public class RecentlyUsedList
{
...
public int Count => ...
public string this[int index] => ...
public void Add(string newItem) ...
...
}
public class RecentlyUsedList
{
private IList<string> items = new List<string>();
public int Count => items.Count;
public string this[int index] => items[index];
public void Add(string newItem)
{
if(newItem == null)
throw new ArgumentNullException();
items.Remove(newItem);
items.Insert(0, newItem);
}
...
}
public class RecentlyUsedList : List<string>
{
public override void Add(string newItem)
{
if(newItem == null)
throw new ArgumentNullException();
items.Remove(newItem);
items.Insert(0, newItem);
}
...
}
namespace List_spec
{
...
[TestFixture]
public class Addition
{
private List<string> list;
[Setup]
public void List_is_initially_empty()
{
list = ...
}
...
[Test]
public void Addition_of_non_null_item_is_appended() ...
[Test]
public void Addition_of_null_is_permitted() ...
[Test]
public void Addition_of_duplicate_item_is_appended() ...
...
}
...
}
namespace List_spec
{
...
[TestFixture]
public class Addition
{
private List<string> list;
[Setup]
public void List_is_initially_empty()
{
list = new List<string>();
}
...
[Test]
public void Addition_of_non_null_item_is_appended() ...
[Test]
public void Addition_of_null_is_permitted() ...
[Test]
public void Addition_of_duplicate_item_is_appended() ...
...
}
...
}
namespace List_spec
{
...
[TestFixture]
public class Addition
{
private List<string> list;
[Setup]
public void List_is_initially_empty()
{
list = new RecentlyUsedList();
}
...
[Test]
public void Addition_of_non_null_item_is_appended() ...
[Test]
public void Addition_of_null_is_permitted() ...
[Test]
public void Addition_of_duplicate_item_is_appended() ...
...
}
...
}
What is wanted here is something like the
following substitution property: If for each
object o1 of type S there is an object o2 of type
T such that for all programs P defined in terms
of T, the behavior of P is unchanged when o1 is
substituted for o2, then S is a subtype of T.
Barbara Liskov
"Data Abstraction and Hierarchy"
What is wanted here is something like the
following substitution property: If for each
object o1 of type S there is an object o2 of type
T such that for all programs P defined in terms
of T, the behavior of P is unchanged when o1 is
substituted for o2, then S is a subtype of T.
Barbara Liskov
"Data Abstraction and Hierarchy"
What is wanted here is something like the
following substitution property: If for each
object o1 of type S there is an object o2 of type
T such that for all programs P defined in terms
of T, the behavior of P is unchanged when o1 is
substituted for o2, then S is a subtype of T.
Barbara Liskov
"Data Abstraction and Hierarchy"
What is wanted here is something like the
following substitution property: If for each
object o1 of type S there is an object o2 of type
T such that for all programs P defined in terms
of T, the behavior of P is unchanged when o1 is
substituted for o2, then S is a subtype of T.
Barbara Liskov
"Data Abstraction and Hierarchy"
Single Responsibility
Open-Closed
Liskov Substitution
Interface Segregation
Dependency Inversion
Bertrand Meyer gave us guidance as long
ago as 1988 when he coined the now famous
open-closed principle. To paraphrase him:
Software entites (classes, modules,
functions, etc.) should be open for
extension, but closed for modification.
"The Open-Closed Principle", Robert C Martin
C++ Report, January 1996
Open for
extension
Closed for
extension
Closed for
modification
Open for
modification
Extend existing code
without changing it,
e.g., using a framework
Extend or change existing
code, e.g., open-source
and non-published code
Non-extensible and
unchangeable code,
e.g., composable code
or legacy code
Non-extensible code that
can be maintained, e.g.,
composable code in a
maintained library
The principle stated that a good module structure
should be both open and closed:
▪ Closed, because clients need the module's
services to proceed with their own development,
and once they have settled on a version of the
module should not be affected by the
introduction of new services they do not need.
▪ Open, because there is no guarantee that we
will include right from the start every service
potentially useful to some client.
[...] A good module structure should
be [...] closed [...] because clients
need the module's services to
proceed with their own development,
and once they have settled on a
version of the module should not be
affected by the introduction of new
services they do not need.
There is no problem changing a
method name if you have access to
all the code that calls that method.
Even if the method is public, as long
as you can reach and change all the
callers, you can rename the method.
There is a problem only if the
interface is being used by code that
you cannot find and change. When
this happens, I say that the interface
becomes a published interface (a
step beyond a public interface).
The distinction between published
and public is actually more
important than that between public
and private.
Martin Fowler
https://martinfowler.com/bliki/PublishedInterface.html
With a non-published interface you
can change it and update the calling
code since it is all within a single
code base.
But anything published so you can't
reach the calling code needs more
complicated treatment.
Martin Fowler
https://martinfowler.com/bliki/PublishedInterface.html
[...] A good module structure should
be [...] open [...] because there is no
guarantee that we will include right
from the start every service potentially
useful to some client.
Speculative Generality
Brian Foote suggested this name for a
smell to which we are very sensitive.
You get it when people say, "Oh, I think
we need the ability to do this kind of
thing someday" and thus want all sorts
of hooks and special cases to handle
things that aren't required.
extends
A myth in the object-oriented design
community goes something like this:
If you use object-oriented technology,
you can take any class someone else
wrote, and, by using it as a base class,
refine it to do a similar task.
Robert B Murray
C++ Strategies andTactics
Design and implement for
inheritance or else prohibit it
By now, it should be apparent that
designing a class for inheritance places
substantial limitations on the class.
https://8thlight.com/blog/uncle-bob/2014/05/12/TheOpenClosedPrinciple.html
I've heard it said that the OCP is
wrong, unworkable, impractical,
and not for real programmers
with real work to do. The rise of
plugin architectures makes it
plain that these views are utter
nonsense. On the contrary, a
strong plugin architecture is likely
to be the most important aspect
of future software systems.
https://8thlight.com/blog/uncle-bob/2014/05/12/TheOpenClosedPrinciple.html
OCP ≡ Plug-ins?
OCP ≡ Plug-ins/
public abstract class Shape
{
...
}
public class Square : Shape
{
...
public void DrawSquare() ...
}
public class Circle : Shape
{
...
public void DrawCircle() ...
}
public abstract class Shape ...
public class Square : Shape ...
public class Circle : Shape ...
static void DrawAllShapes(Shape[] list)
{
foreach (Shape s in list)
if (s is Square)
(s as Square).DrawSquare();
else if (s is Circle)
(s as Circle).DrawCircle();
}
public abstract class Shape ...
public class Square : Shape ...
public class Circle : Shape ...
static void DrawAllShapes(Shape[] list)
{
foreach (Shape s in list)
if (s is Square)
(s as Square).DrawSquare();
else if (s is Circle)
(s as Circle).DrawCircle();
}
public abstract class Shape
{
...
public abstract void Draw();
}
public class Square : Shape
{
...
public override void Draw() ...
}
public class Circle : Shape
{
...
public override void Draw() ...
}
public abstract class Shape ...
public class Square : Shape ...
public class Circle : Shape ...
static void DrawAllShapes(Shape[] list)
{
foreach (Shape s in list)
s.Draw();
}
public abstract class Shape ...
public class Square : Shape ...
public class Circle : Shape ...
static void DrawAllShapes(Shape[] list)
{
foreach (Shape s in list)
s.Draw();
}
public abstract class Shape ...
public class Square : Shape ...
public class Circle : Shape ...
static void DrawAllShapes(Shape[] list)
{
foreach (Shape s in list)
s.Draw();
}
Bertrand Meyer gave us guidance as long
ago as 1988 when he coined the now famous
open-closed principle. To paraphrase him:
Software entites (classes, modules,
functions, etc.) should be open for
extension, but closed for modification.
"The Open-Closed Principle", Robert C Martin
C++ Report, January 1996
Bertrand Meyer gave us guidance as long
ago as 1988 when he coined the now famous
open-closed principle. To paraphrase him:
Software entites (classes, modules,
functions, etc.) should be open for
extension, but closed for modification.
"The Open-Closed Principle", Robert C Martin
C++ Report, January 1996
This double requirement looks
like a dilemma, and classical
module structures offer no clue.
But inheritance solves it.
A class is closed, since it may
be compiled, stored in a library,
baselined, and used by client
classes. But it is also open, since
any new class may use it as
parent, adding new features.
public abstract class Shape ...
public class Square : Shape ...
public class Circle : Shape ...
static void DrawAllShapes(Shape[] list)
{
foreach (Shape s in list)
if (s is Square)
(s as Square).DrawSquare();
else if (s is Circle)
(s as Circle).DrawCircle();
}
public abstract class Shape ...
public class Square : Shape ...
public class Circle : Shape ...
static void DrawAllShapes(Shape[] list)
{
foreach (Shape s in list)
if (s is Square)
(s as Square).DrawSquare();
else if (s is Circle)
(s as Circle).DrawCircle();
}
public abstract class Shape ...
public class Square : Shape ...
public class Circle : Shape ...
static void DrawAllShapes(Shape[] list)
{
foreach (Shape s in list)
s.Draw();
}
public abstract class Shape ...
public class Square : Shape ...
public class Circle : Shape ...
static void DrawAllShapes(Shape[] list)
{
foreach (Shape s in list)
s.Draw();
}
public sealed class Shape
{
public enum Type { Square, Circle }
...
public void Draw() ...
}
static void DrawAllShapes(Shape[] list)
{
foreach (Shape s in list)
s.Draw();
}
public sealed class Shape
{
public enum Type { Square, Circle }
...
public void Draw() ...
}
static void DrawAllShapes(Shape[] list)
{
foreach (Shape s in list)
s.Draw();
}
Don't publish interfaces prematurely.
Modify your code ownership policies
to smooth refactoring.
Single Responsibility
Open-Closed
Liskov Substitution
Interface Segregation
Dependency Inversion
The principle states:
A. High-level modules should not depend on
low-level modules. Both should depend on
abstractions.
B. Abstractions should not depend upon details.
Details should depend upon abstractions.
https://en.wikipedia.org/wiki/Dependency_inversion_principle
Program to an interface,
not an implementation.
Erich Gamma, Richard Helm, Ralph Johnson & John Vlissides
Program to an interface,
not an instance.
One of the most foundational
principles of good design is:
Gather together those things
that change for the same reason,
and separate those things that
change for different reasons.
This principle is often known as
the single responsibility principle,
or SRP. In short, it says that a
subsystem, module, class, or even
a function, should not have more
than one reason to change.
Write components that do one thing
and do it well.
Write components to work together.
Program to an interface, not an
implementation.
Don't publish interfaces prematurely.
Modify your code ownership policies
to smooth refactoring.
Solid Deconstruction

More Related Content

What's hot (20)

What's new in PHP 8.0?
What's new in PHP 8.0?What's new in PHP 8.0?
What's new in PHP 8.0?
 
6. static keyword
6. static keyword6. static keyword
6. static keyword
 
Methods in Java
Methods in JavaMethods in Java
Methods in Java
 
Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructors
 
Java 8 : Un ch'ti peu de lambda
Java 8 : Un ch'ti peu de lambdaJava 8 : Un ch'ti peu de lambda
Java 8 : Un ch'ti peu de lambda
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
 
Introduction to PHP
Introduction to PHPIntroduction to PHP
Introduction to PHP
 
Java String
Java String Java String
Java String
 
Polymorphism In Java
Polymorphism In JavaPolymorphism In Java
Polymorphism In Java
 
Operators in java
Operators in javaOperators in java
Operators in java
 
Lesson 6 php if...else...elseif statements
Lesson 6   php if...else...elseif statementsLesson 6   php if...else...elseif statements
Lesson 6 php if...else...elseif statements
 
Python: Basic Inheritance
Python: Basic InheritancePython: Basic Inheritance
Python: Basic Inheritance
 
array of object pointer in c++
array of object pointer in c++array of object pointer in c++
array of object pointer in c++
 
Library functions in c++
Library functions in c++Library functions in c++
Library functions in c++
 
Exceptions in Java
Exceptions in JavaExceptions in Java
Exceptions in Java
 
Loops in C
Loops in CLoops in C
Loops in C
 
Lambdas y API Stream - Apuntes de Java
Lambdas y API Stream - Apuntes de JavaLambdas y API Stream - Apuntes de Java
Lambdas y API Stream - Apuntes de Java
 
Templates in c++
Templates in c++Templates in c++
Templates in c++
 
C++ Generators and Property-based Testing
C++ Generators and Property-based TestingC++ Generators and Property-based Testing
C++ Generators and Property-based Testing
 
Control statement in c
Control statement in cControl statement in c
Control statement in c
 

Similar to Solid Deconstruction

SOLID Deconstruction
SOLID DeconstructionSOLID Deconstruction
SOLID DeconstructionKevlin Henney
 
SOLID Deconstruction
SOLID DeconstructionSOLID Deconstruction
SOLID DeconstructionKevlin Henney
 
It Is Possible to Do Object-Oriented Programming in Java
It Is Possible to Do Object-Oriented Programming in JavaIt Is Possible to Do Object-Oriented Programming in Java
It Is Possible to Do Object-Oriented Programming in JavaKevlin Henney
 
Object oriented programming
Object oriented programmingObject oriented programming
Object oriented programmingmustafa sarac
 
Functional OO programming (as part of the the PTT lecture)
Functional OO programming (as part of the the PTT lecture)Functional OO programming (as part of the the PTT lecture)
Functional OO programming (as part of the the PTT lecture)Ralf Laemmel
 
EEE oops Vth semester viva questions with answer
EEE oops Vth semester viva questions with answerEEE oops Vth semester viva questions with answer
EEE oops Vth semester viva questions with answerJeba Moses
 
Object oriented programming
Object oriented programmingObject oriented programming
Object oriented programmingMH Abid
 
1 intro
1 intro1 intro
1 introabha48
 
Object And Oriented Programing ( Oop ) Languages
Object And Oriented Programing ( Oop ) LanguagesObject And Oriented Programing ( Oop ) Languages
Object And Oriented Programing ( Oop ) LanguagesJessica Deakin
 
Object Oriented Language
Object Oriented LanguageObject Oriented Language
Object Oriented Languagedheva B
 
Question and answer Programming
Question and answer ProgrammingQuestion and answer Programming
Question and answer ProgrammingInocentshuja Ahmad
 
SE18_Lec 06_Object Oriented Analysis and Design
SE18_Lec 06_Object Oriented Analysis and DesignSE18_Lec 06_Object Oriented Analysis and Design
SE18_Lec 06_Object Oriented Analysis and DesignAmr E. Mohamed
 

Similar to Solid Deconstruction (20)

SOLID Deconstruction
SOLID DeconstructionSOLID Deconstruction
SOLID Deconstruction
 
SOLID Deconstruction
SOLID DeconstructionSOLID Deconstruction
SOLID Deconstruction
 
It Is Possible to Do Object-Oriented Programming in Java
It Is Possible to Do Object-Oriented Programming in JavaIt Is Possible to Do Object-Oriented Programming in Java
It Is Possible to Do Object-Oriented Programming in Java
 
Object oriented programming
Object oriented programmingObject oriented programming
Object oriented programming
 
OOP by hrishikesh dhola
OOP by hrishikesh dholaOOP by hrishikesh dhola
OOP by hrishikesh dhola
 
Functional OO programming (as part of the the PTT lecture)
Functional OO programming (as part of the the PTT lecture)Functional OO programming (as part of the the PTT lecture)
Functional OO programming (as part of the the PTT lecture)
 
EEE oops Vth semester viva questions with answer
EEE oops Vth semester viva questions with answerEEE oops Vth semester viva questions with answer
EEE oops Vth semester viva questions with answer
 
SEMINAR
SEMINARSEMINAR
SEMINAR
 
Seminar
SeminarSeminar
Seminar
 
Object oriented programming
Object oriented programmingObject oriented programming
Object oriented programming
 
Java Notes
Java NotesJava Notes
Java Notes
 
1 intro
1 intro1 intro
1 intro
 
Object And Oriented Programing ( Oop ) Languages
Object And Oriented Programing ( Oop ) LanguagesObject And Oriented Programing ( Oop ) Languages
Object And Oriented Programing ( Oop ) Languages
 
My c++
My c++My c++
My c++
 
Object Oriented Language
Object Oriented LanguageObject Oriented Language
Object Oriented Language
 
OOSD1-unit1_1_16_09.pptx
OOSD1-unit1_1_16_09.pptxOOSD1-unit1_1_16_09.pptx
OOSD1-unit1_1_16_09.pptx
 
Question and answer Programming
Question and answer ProgrammingQuestion and answer Programming
Question and answer Programming
 
SE18_Lec 06_Object Oriented Analysis and Design
SE18_Lec 06_Object Oriented Analysis and DesignSE18_Lec 06_Object Oriented Analysis and Design
SE18_Lec 06_Object Oriented Analysis and Design
 
Oops slide
Oops slide Oops slide
Oops slide
 
Java chapter 3
Java   chapter 3Java   chapter 3
Java chapter 3
 

More from Kevlin Henney

The Case for Technical Excellence
The Case for Technical ExcellenceThe Case for Technical Excellence
The Case for Technical ExcellenceKevlin Henney
 
Empirical Development
Empirical DevelopmentEmpirical Development
Empirical DevelopmentKevlin Henney
 
Lambda? You Keep Using that Letter
Lambda? You Keep Using that LetterLambda? You Keep Using that Letter
Lambda? You Keep Using that LetterKevlin Henney
 
Lambda? You Keep Using that Letter
Lambda? You Keep Using that LetterLambda? You Keep Using that Letter
Lambda? You Keep Using that LetterKevlin Henney
 
Procedural Programming: It’s Back? It Never Went Away
Procedural Programming: It’s Back? It Never Went AwayProcedural Programming: It’s Back? It Never Went Away
Procedural Programming: It’s Back? It Never Went AwayKevlin Henney
 
Structure and Interpretation of Test Cases
Structure and Interpretation of Test CasesStructure and Interpretation of Test Cases
Structure and Interpretation of Test CasesKevlin Henney
 
Refactoring to Immutability
Refactoring to ImmutabilityRefactoring to Immutability
Refactoring to ImmutabilityKevlin Henney
 
Turning Development Outside-In
Turning Development Outside-InTurning Development Outside-In
Turning Development Outside-InKevlin Henney
 
Giving Code a Good Name
Giving Code a Good NameGiving Code a Good Name
Giving Code a Good NameKevlin Henney
 
Clean Coders Hate What Happens To Your Code When You Use These Enterprise Pro...
Clean Coders Hate What Happens To Your Code When You Use These Enterprise Pro...Clean Coders Hate What Happens To Your Code When You Use These Enterprise Pro...
Clean Coders Hate What Happens To Your Code When You Use These Enterprise Pro...Kevlin Henney
 
Thinking Outside the Synchronisation Quadrant
Thinking Outside the Synchronisation QuadrantThinking Outside the Synchronisation Quadrant
Thinking Outside the Synchronisation QuadrantKevlin Henney
 
The Error of Our Ways
The Error of Our WaysThe Error of Our Ways
The Error of Our WaysKevlin Henney
 

More from Kevlin Henney (20)

Program with GUTs
Program with GUTsProgram with GUTs
Program with GUTs
 
The Case for Technical Excellence
The Case for Technical ExcellenceThe Case for Technical Excellence
The Case for Technical Excellence
 
Empirical Development
Empirical DevelopmentEmpirical Development
Empirical Development
 
Lambda? You Keep Using that Letter
Lambda? You Keep Using that LetterLambda? You Keep Using that Letter
Lambda? You Keep Using that Letter
 
Lambda? You Keep Using that Letter
Lambda? You Keep Using that LetterLambda? You Keep Using that Letter
Lambda? You Keep Using that Letter
 
Get Kata
Get KataGet Kata
Get Kata
 
Procedural Programming: It’s Back? It Never Went Away
Procedural Programming: It’s Back? It Never Went AwayProcedural Programming: It’s Back? It Never Went Away
Procedural Programming: It’s Back? It Never Went Away
 
Structure and Interpretation of Test Cases
Structure and Interpretation of Test CasesStructure and Interpretation of Test Cases
Structure and Interpretation of Test Cases
 
Agility ≠ Speed
Agility ≠ SpeedAgility ≠ Speed
Agility ≠ Speed
 
Refactoring to Immutability
Refactoring to ImmutabilityRefactoring to Immutability
Refactoring to Immutability
 
Old Is the New New
Old Is the New NewOld Is the New New
Old Is the New New
 
Turning Development Outside-In
Turning Development Outside-InTurning Development Outside-In
Turning Development Outside-In
 
Giving Code a Good Name
Giving Code a Good NameGiving Code a Good Name
Giving Code a Good Name
 
Clean Coders Hate What Happens To Your Code When You Use These Enterprise Pro...
Clean Coders Hate What Happens To Your Code When You Use These Enterprise Pro...Clean Coders Hate What Happens To Your Code When You Use These Enterprise Pro...
Clean Coders Hate What Happens To Your Code When You Use These Enterprise Pro...
 
Thinking Outside the Synchronisation Quadrant
Thinking Outside the Synchronisation QuadrantThinking Outside the Synchronisation Quadrant
Thinking Outside the Synchronisation Quadrant
 
Code as Risk
Code as RiskCode as Risk
Code as Risk
 
Software Is Details
Software Is DetailsSoftware Is Details
Software Is Details
 
Game of Sprints
Game of SprintsGame of Sprints
Game of Sprints
 
Good Code
Good CodeGood Code
Good Code
 
The Error of Our Ways
The Error of Our WaysThe Error of Our Ways
The Error of Our Ways
 

Recently uploaded

Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...stazi3110
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfAlina Yurenko
 
CRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceCRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceBrainSell Technologies
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Andreas Granig
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024StefanoLambiase
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityNeo4j
 
How to Track Employee Performance A Comprehensive Guide.pdf
How to Track Employee Performance A Comprehensive Guide.pdfHow to Track Employee Performance A Comprehensive Guide.pdf
How to Track Employee Performance A Comprehensive Guide.pdfLivetecs LLC
 
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...Cizo Technology Services
 
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)jennyeacort
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEEVICTOR MAESTRE RAMIREZ
 
MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based projectAnoyGreter
 
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...Matt Ray
 
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...confluent
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEOrtus Solutions, Corp
 
Best Web Development Agency- Idiosys USA.pdf
Best Web Development Agency- Idiosys USA.pdfBest Web Development Agency- Idiosys USA.pdf
Best Web Development Agency- Idiosys USA.pdfIdiosysTechnologies1
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsAhmed Mohamed
 
How to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion ApplicationHow to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion ApplicationBradBedford3
 
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...Angel Borroy López
 

Recently uploaded (20)

Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
 
2.pdf Ejercicios de programación competitiva
2.pdf Ejercicios de programación competitiva2.pdf Ejercicios de programación competitiva
2.pdf Ejercicios de programación competitiva
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
 
CRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceCRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. Salesforce
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered Sustainability
 
How to Track Employee Performance A Comprehensive Guide.pdf
How to Track Employee Performance A Comprehensive Guide.pdfHow to Track Employee Performance A Comprehensive Guide.pdf
How to Track Employee Performance A Comprehensive Guide.pdf
 
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
 
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEE
 
Advantages of Odoo ERP 17 for Your Business
Advantages of Odoo ERP 17 for Your BusinessAdvantages of Odoo ERP 17 for Your Business
Advantages of Odoo ERP 17 for Your Business
 
MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based project
 
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
 
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
 
Best Web Development Agency- Idiosys USA.pdf
Best Web Development Agency- Idiosys USA.pdfBest Web Development Agency- Idiosys USA.pdf
Best Web Development Agency- Idiosys USA.pdf
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML Diagrams
 
How to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion ApplicationHow to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion Application
 
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
 

Solid Deconstruction

  • 1.
  • 2.
  • 6. In object-oriented programming, the single responsibility principle states that every object should have a single responsibility, and that responsibility should be entirely encapsulated by the class. All its services should be narrowly aligned with that responsibility. http://en.wikipedia.org/wiki/Single_responsibility_principle
  • 7. The single responsibility principle is a computer programming principle that states that every module or class should have responsibility over a single part of the functionality provided by the software, and that responsibility should be entirely encapsulated by the class. All its services should be narrowly aligned with that responsibility. http://en.wikipedia.org/wiki/Single_responsibility_principle
  • 8. The single responsibility principle is a computer programming principle that states that every module or class should have responsibility over a single part of the functionality provided by the software, and that responsibility should be entirely encapsulated by the class. All its services should be narrowly aligned with that responsibility. http://en.wikipedia.org/wiki/Single_responsibility_principle
  • 9. The term was introduced by Robert C. Martin [...]. Martin described it as being based on the principle of cohesion, as described by Tom DeMarco in his book Structured Analysis and Systems Specification. http://en.wikipedia.org/wiki/Single_responsibility_principle
  • 10.
  • 11. Cohesion is a measure of the strength of association of the elements inside a module. A highly cohesive module is a collection of statements and data items that should be treated as a whole because they are so closely related. Any attempt to divide them up would only result in increased coupling and decreased readability.
  • 12.
  • 14.
  • 15. This is the Unix philosophy: Write programs that do one thing and do it well.Write programs to work together. Doug McIlroy
  • 16.
  • 17. In McIlroy's summary, the hard part is his second sentence: Write programs to work together. John D Cook
  • 18.
  • 19. In the long run every program becomes rococo — then rubble. Alan Perlis
  • 20. We refer to a sound line of reasoning, for example, as coherent. The thoughts fit, they go together, they relate to each other. GlennVanderburg http://vanderburg.org/blog/2011/01/31/cohesion.html
  • 21. This is exactly the characteristic of a class that makes it coherent: the pieces all seem to be related, they seem to belong together, and it would feel somewhat unnatural to pull them apart. Such a class exhibits cohesion. GlennVanderburg http://vanderburg.org/blog/2011/01/31/cohesion.html
  • 22.
  • 23.
  • 25. Every class should embody only about 3–5 distinct responsibilities. Grady Booch Object Solutions
  • 26. One of the most foundational principles of good design is: Gather together those things that change for the same reason, and separate those things that change for different reasons. This principle is often known as the single responsibility principle, or SRP. In short, it says that a subsystem, module, class, or even a function, should not have more than one reason to change.
  • 28. Interface inheritance (subtyping) is used whenever one can imagine that client code should depend on less functionality than the full interface. Services are often partitioned into several unrelated interfaces when it is possible to partition the clients into different roles. "General Design Principles" CORBAservices
  • 29. The dependency should be on the interface, the whole interface, and nothing but the interface.
  • 30. This is exactly the characteristic of a class that makes it coherent: the pieces all seem to be related, they seem to belong together, and it would feel somewhat unnatural to pull them apart. Such a class exhibits cohesion. GlennVanderburg http://vanderburg.org/blog/2011/01/31/cohesion.html
  • 31. This is exactly the characteristic of an interface that makes it coherent: the pieces all seem to be related, they seem to belong together, and it would feel somewhat unnatural to pull them apart. Such an interface exhibits cohesion.
  • 32. public interface LineIO { String read(); void write(String lineToWrite); }
  • 33. public interface LineReader { String read(); } public interface LineWriter { void write(String lineToWrite); }
  • 34.
  • 36.
  • 37. Concept Hierarchies The construction principle involved is best called abstraction; we concentrate on features common to many phenomena, and we abstract away features too far removed from the conceptual level at which we are working. Ole-Johan Dahl and C A R Hoare "Hierarchical Program Structures"
  • 38. A type hierarchy is composed of subtypes and supertypes. The intuitive idea of a subtype is one whose objects provide all the behavior of objects of another type (the supertype) plus something extra. Barbara Liskov "Data Abstraction and Hierarchy"
  • 39. What is wanted here is something like the following substitution property: If for each object o1 of type S there is an object o2 of type T such that for all programs P defined in terms of T, the behavior of P is unchanged when o1 is substituted for o2, then S is a subtype of T. Barbara Liskov "Data Abstraction and Hierarchy"
  • 41. E.g., Python's dict class, which is a hashed mapping container E.g., Python's OrderedDict class, which preserves order of insertion
  • 42.
  • 43.
  • 44. public class RecentlyUsedList { ... public int Count { get ... } public string this[int index] { get ... } public void Add(string newItem) ... ... }
  • 45. public class RecentlyUsedList { ... public int Count => ... public string this[int index] => ... public void Add(string newItem) ... ... }
  • 46. public class RecentlyUsedList { private IList<string> items = new List<string>(); public int Count => items.Count; public string this[int index] => items[index]; public void Add(string newItem) { if(newItem == null) throw new ArgumentNullException(); items.Remove(newItem); items.Insert(0, newItem); } ... }
  • 47. public class RecentlyUsedList : List<string> { public override void Add(string newItem) { if(newItem == null) throw new ArgumentNullException(); items.Remove(newItem); items.Insert(0, newItem); } ... }
  • 48. namespace List_spec { ... [TestFixture] public class Addition { private List<string> list; [Setup] public void List_is_initially_empty() { list = ... } ... [Test] public void Addition_of_non_null_item_is_appended() ... [Test] public void Addition_of_null_is_permitted() ... [Test] public void Addition_of_duplicate_item_is_appended() ... ... } ... }
  • 49. namespace List_spec { ... [TestFixture] public class Addition { private List<string> list; [Setup] public void List_is_initially_empty() { list = new List<string>(); } ... [Test] public void Addition_of_non_null_item_is_appended() ... [Test] public void Addition_of_null_is_permitted() ... [Test] public void Addition_of_duplicate_item_is_appended() ... ... } ... }
  • 50. namespace List_spec { ... [TestFixture] public class Addition { private List<string> list; [Setup] public void List_is_initially_empty() { list = new RecentlyUsedList(); } ... [Test] public void Addition_of_non_null_item_is_appended() ... [Test] public void Addition_of_null_is_permitted() ... [Test] public void Addition_of_duplicate_item_is_appended() ... ... } ... }
  • 51. What is wanted here is something like the following substitution property: If for each object o1 of type S there is an object o2 of type T such that for all programs P defined in terms of T, the behavior of P is unchanged when o1 is substituted for o2, then S is a subtype of T. Barbara Liskov "Data Abstraction and Hierarchy"
  • 52. What is wanted here is something like the following substitution property: If for each object o1 of type S there is an object o2 of type T such that for all programs P defined in terms of T, the behavior of P is unchanged when o1 is substituted for o2, then S is a subtype of T. Barbara Liskov "Data Abstraction and Hierarchy"
  • 53. What is wanted here is something like the following substitution property: If for each object o1 of type S there is an object o2 of type T such that for all programs P defined in terms of T, the behavior of P is unchanged when o1 is substituted for o2, then S is a subtype of T. Barbara Liskov "Data Abstraction and Hierarchy"
  • 54. What is wanted here is something like the following substitution property: If for each object o1 of type S there is an object o2 of type T such that for all programs P defined in terms of T, the behavior of P is unchanged when o1 is substituted for o2, then S is a subtype of T. Barbara Liskov "Data Abstraction and Hierarchy"
  • 56. Bertrand Meyer gave us guidance as long ago as 1988 when he coined the now famous open-closed principle. To paraphrase him: Software entites (classes, modules, functions, etc.) should be open for extension, but closed for modification. "The Open-Closed Principle", Robert C Martin C++ Report, January 1996
  • 57. Open for extension Closed for extension Closed for modification Open for modification Extend existing code without changing it, e.g., using a framework Extend or change existing code, e.g., open-source and non-published code Non-extensible and unchangeable code, e.g., composable code or legacy code Non-extensible code that can be maintained, e.g., composable code in a maintained library
  • 58.
  • 59. The principle stated that a good module structure should be both open and closed: ▪ Closed, because clients need the module's services to proceed with their own development, and once they have settled on a version of the module should not be affected by the introduction of new services they do not need. ▪ Open, because there is no guarantee that we will include right from the start every service potentially useful to some client.
  • 60. [...] A good module structure should be [...] closed [...] because clients need the module's services to proceed with their own development, and once they have settled on a version of the module should not be affected by the introduction of new services they do not need.
  • 61.
  • 62. There is no problem changing a method name if you have access to all the code that calls that method. Even if the method is public, as long as you can reach and change all the callers, you can rename the method.
  • 63. There is a problem only if the interface is being used by code that you cannot find and change. When this happens, I say that the interface becomes a published interface (a step beyond a public interface).
  • 64. The distinction between published and public is actually more important than that between public and private. Martin Fowler https://martinfowler.com/bliki/PublishedInterface.html
  • 65. With a non-published interface you can change it and update the calling code since it is all within a single code base. But anything published so you can't reach the calling code needs more complicated treatment. Martin Fowler https://martinfowler.com/bliki/PublishedInterface.html
  • 66. [...] A good module structure should be [...] open [...] because there is no guarantee that we will include right from the start every service potentially useful to some client.
  • 67.
  • 68. Speculative Generality Brian Foote suggested this name for a smell to which we are very sensitive. You get it when people say, "Oh, I think we need the ability to do this kind of thing someday" and thus want all sorts of hooks and special cases to handle things that aren't required.
  • 70. A myth in the object-oriented design community goes something like this: If you use object-oriented technology, you can take any class someone else wrote, and, by using it as a base class, refine it to do a similar task. Robert B Murray C++ Strategies andTactics
  • 71.
  • 72. Design and implement for inheritance or else prohibit it By now, it should be apparent that designing a class for inheritance places substantial limitations on the class.
  • 73.
  • 75. I've heard it said that the OCP is wrong, unworkable, impractical, and not for real programmers with real work to do. The rise of plugin architectures makes it plain that these views are utter nonsense. On the contrary, a strong plugin architecture is likely to be the most important aspect of future software systems. https://8thlight.com/blog/uncle-bob/2014/05/12/TheOpenClosedPrinciple.html
  • 78.
  • 79. public abstract class Shape { ... } public class Square : Shape { ... public void DrawSquare() ... } public class Circle : Shape { ... public void DrawCircle() ... }
  • 80. public abstract class Shape ... public class Square : Shape ... public class Circle : Shape ... static void DrawAllShapes(Shape[] list) { foreach (Shape s in list) if (s is Square) (s as Square).DrawSquare(); else if (s is Circle) (s as Circle).DrawCircle(); }
  • 81. public abstract class Shape ... public class Square : Shape ... public class Circle : Shape ... static void DrawAllShapes(Shape[] list) { foreach (Shape s in list) if (s is Square) (s as Square).DrawSquare(); else if (s is Circle) (s as Circle).DrawCircle(); }
  • 82. public abstract class Shape { ... public abstract void Draw(); } public class Square : Shape { ... public override void Draw() ... } public class Circle : Shape { ... public override void Draw() ... }
  • 83. public abstract class Shape ... public class Square : Shape ... public class Circle : Shape ... static void DrawAllShapes(Shape[] list) { foreach (Shape s in list) s.Draw(); }
  • 84. public abstract class Shape ... public class Square : Shape ... public class Circle : Shape ... static void DrawAllShapes(Shape[] list) { foreach (Shape s in list) s.Draw(); }
  • 85. public abstract class Shape ... public class Square : Shape ... public class Circle : Shape ... static void DrawAllShapes(Shape[] list) { foreach (Shape s in list) s.Draw(); }
  • 86. Bertrand Meyer gave us guidance as long ago as 1988 when he coined the now famous open-closed principle. To paraphrase him: Software entites (classes, modules, functions, etc.) should be open for extension, but closed for modification. "The Open-Closed Principle", Robert C Martin C++ Report, January 1996
  • 87. Bertrand Meyer gave us guidance as long ago as 1988 when he coined the now famous open-closed principle. To paraphrase him: Software entites (classes, modules, functions, etc.) should be open for extension, but closed for modification. "The Open-Closed Principle", Robert C Martin C++ Report, January 1996
  • 88.
  • 89. This double requirement looks like a dilemma, and classical module structures offer no clue. But inheritance solves it. A class is closed, since it may be compiled, stored in a library, baselined, and used by client classes. But it is also open, since any new class may use it as parent, adding new features.
  • 90. public abstract class Shape ... public class Square : Shape ... public class Circle : Shape ... static void DrawAllShapes(Shape[] list) { foreach (Shape s in list) if (s is Square) (s as Square).DrawSquare(); else if (s is Circle) (s as Circle).DrawCircle(); }
  • 91. public abstract class Shape ... public class Square : Shape ... public class Circle : Shape ... static void DrawAllShapes(Shape[] list) { foreach (Shape s in list) if (s is Square) (s as Square).DrawSquare(); else if (s is Circle) (s as Circle).DrawCircle(); }
  • 92. public abstract class Shape ... public class Square : Shape ... public class Circle : Shape ... static void DrawAllShapes(Shape[] list) { foreach (Shape s in list) s.Draw(); }
  • 93. public abstract class Shape ... public class Square : Shape ... public class Circle : Shape ... static void DrawAllShapes(Shape[] list) { foreach (Shape s in list) s.Draw(); }
  • 94. public sealed class Shape { public enum Type { Square, Circle } ... public void Draw() ... } static void DrawAllShapes(Shape[] list) { foreach (Shape s in list) s.Draw(); }
  • 95. public sealed class Shape { public enum Type { Square, Circle } ... public void Draw() ... } static void DrawAllShapes(Shape[] list) { foreach (Shape s in list) s.Draw(); }
  • 96.
  • 97. Don't publish interfaces prematurely. Modify your code ownership policies to smooth refactoring.
  • 99. The principle states: A. High-level modules should not depend on low-level modules. Both should depend on abstractions. B. Abstractions should not depend upon details. Details should depend upon abstractions. https://en.wikipedia.org/wiki/Dependency_inversion_principle
  • 100.
  • 101.
  • 102. Program to an interface, not an implementation. Erich Gamma, Richard Helm, Ralph Johnson & John Vlissides
  • 103. Program to an interface, not an instance.
  • 104.
  • 105.
  • 106. One of the most foundational principles of good design is: Gather together those things that change for the same reason, and separate those things that change for different reasons. This principle is often known as the single responsibility principle, or SRP. In short, it says that a subsystem, module, class, or even a function, should not have more than one reason to change.
  • 107. Write components that do one thing and do it well. Write components to work together. Program to an interface, not an implementation. Don't publish interfaces prematurely. Modify your code ownership policies to smooth refactoring.