SlideShare a Scribd company logo
1 of 22
Download to read offline
Thursday, Feb 22nd 2024 1/22
The SOLID Principles
Jan Wedekind
Thursday, Feb 22nd 2024
Motivation
Thursday, Feb 22nd 2024 2/22
Find guiding design principles to
maintain software quality over
time.
Software Rot
Thursday, Feb 22nd 2024 3/22
Symptoms of rotting software design:a
• Rigidity: software difficult (a lot of work) to change
• Fragility: changes easily break the software
• Immobility: it is easier to rewrite than reuse parts
• Viscosity: design preserving methods are harder to employ
than hacks
aRobert C. Martin: Design Principles and Design Patterns
Aims
Thursday, Feb 22nd 2024 4/22
In contrast we want to achieve the following:a
• Keep software application flexible
• Keep software application robust
• Keep software application reusable
• Keep software application developable
aRobert C. Martin: Design Principles and Design Patterns
SOLID Authors
Thursday, Feb 22nd 2024 5/22
Robert C. Martin
• Author of Clean Code, Functional Design,
and more books
• Author of Design Principles and Design
Patterns paper based on his experience and
on work by Bertrand Meyer, Barbara
Liskov, and Erich Gamma et al.
• http://cleancoder.com/
Michael Feathers
• Author of Working Effectively With Legacy
Code
• Summarized Robert C. Martin’s paper
using the SOLID acronym
• https://www.r7krecon.com/
The SOLID Principles
Thursday, Feb 22nd 2024 6/22
1. Single responsibility
2. Open-closed
3. Liskov substitution
4. Interface segregation
5. Dependency inversion
Single Responsibility - Before
Thursday, Feb 22nd 2024 7/22
def adults_to_html(people):
result = "<ul>n"
for person in people:
if person.age >= 18:
result += " <li>" + person.name + "</li>n"
result += "</ul>"
return result
# ...
page = adults_to_html(people)
Single Responsibility - After
Thursday, Feb 22nd 2024 8/22
def select_adults(people):
return [person for person in people if person.age >= 18]
def people_to_html(people):
result = "<ul>n"
for person in people:
result += " <li>" + person.name + "</li>n"
result += "</ul>"
return result
# ...
page = people_to_html(select_adults(people))
Open-Closed - Before
Thursday, Feb 22nd 2024 9/22
def total_area(shapes):
result = 0
for shape in shapes:
match type(shape):
case Rectangle:
result += shape.width * shape.height
case Sphere:
result += math.pi * shape.radius ** 2
case _:
raise f"Unsupported shape {shape}"
return result
Open-Closed - After
Thursday, Feb 22nd 2024 10/22
class Rectangle:
def area(self):
return self.width * self.height
class Circle:
def area(self):
return math.pi * self.radius ** 2
def total_area(shapes):
result = 0
for shape in shapes:
result += shape.area()
return result
Liskov-Substitution - Before
Thursday, Feb 22nd 2024 11/22
class Rectangle:
def __init__(self, width, height):
self.width = width
self.height = height
def set_width(self, width):
self.width = width
def set_height(self, height):
self.height = height
class Square(Rectangle):
def __init__(self, side):
super().__init__(side, side)
def set_width(self, width):
super().set_width(width)
super().set_height(width)
def set_height(self, height):
self.set_width(height)
Liskov-Substitution - After
Thursday, Feb 22nd 2024 12/22
class Shape:
pass
class Rectangle(Shape):
def __init__(self, width, height):
self.width = width
self.height = height
def set_width(self, width):
self.width = width
def set_height(self, height):
self.height = height
class Square(Shape):
def __init__(self, side):
self.side = side
def set_side(self, side):
self.side = side
Barbara Liskov
Liskov-Substitution - Contracts
Thursday, Feb 22nd 2024 13/22
“The Liskov Substitution Principle states, among other constraints,
that a subtype is not substitutable for its super type if it
strengthens its operations’ preconditions, or weakens its operations’
postconditions”a
precondition
precondition postcondition
postcondition
type
subtype
method
method
aBaniassad: Making the Liskov Substitution Principle Happy and Sad
Interface Segregation - Before
Thursday, Feb 22nd 2024 14/22
class AccountHolder:
def __init__(self, name, age, balance):
self.name = name
self.age = age
self.balance = balance
def is_adult(self):
return self.adult >= 18
def deposit(self, amount):
self.balance += amount
def withdraw(self, amount):
self.balance -= amount
Interface Segregation - After
Thursday, Feb 22nd 2024 15/22
class Person:
def __init__(self, name, age):
self.name, self.age = name, age
def is_adult(self):
return self.adult >= 18
class Account:
def __init__(self, balance):
self.balance = balance
def deposit(self, amount):
self.balance += amount
def withdraw(self, amount):
self.balance -= amount
class AccountHolder(Person):
def __init__(self, name, age, account):
super().__init__(name, age)
self.account = account
Dependency Inversion - Before
Thursday, Feb 22nd 2024 16/22
def get_names(connection):
cursor = connection.cursor()
cursor.execute('SELECT name FROM member_table')
rows = cursor.fetchall()
names = [row[0] for row in rows]
return names
connection = sqlite3.connect('example.db')
names_list = get_names(connection)
connection.close()
print(names_list)
Dependency Inversion - After
Thursday, Feb 22nd 2024 17/22
class Database(abc.ABC):
@abc.abstractmethod
def sql(self, query):
pass
class SQLiteDatabase(Database):
def __init__(self, db_file_name):
self.connection = sqlite3.connect(db_file_name)
def __del__(self):
self.connection.close()
def sql(self, query):
cursor = self.connection.cursor()
cursor.execute(query)
return cursor.fetchall()
def get_names(database):
rows = database.sql('SELECT name FROM member_table')
return [row[0] for row in rows]
Dependency Inversion - After
Thursday, Feb 22nd 2024 18/22
database = SQLiteDatabase('example.db')
names_list = get_names(database)
print(names_list)
Aspects of a Class
Thursday, Feb 22nd 2024 19/22
The 5 aspects of the class are:a
responsibility towards parent
interface
towards
callers
interface
towards
callees
responsibility towards inheritors
class'
purpose
aMike Lindner: The Five Principles For SOLID Software Design
The 5 Principles
Thursday, Feb 22nd 2024 20/22
The 5 corresponding principles are:a
Liskov substitution principle
single
responsibility
principle
interface
segregation
principle
dependency
inversion
principle
open-closed principle
aMike Lindner: The Five Principles For SOLID Software Design
Arjan Egges: Uncle Bob’s SOLID Principles Made Easy
Thursday, Feb 22nd 2024 21/22
19 minutes video
Jim Weirich: The Building Blocks of Modularity
Thursday, Feb 22nd 2024 22/22
33 minutes video

More Related Content

More from Jan Wedekind

Fokus-serien basierte Rekonstruktion von Mikroobjekten (2002)
Fokus-serien basierte Rekonstruktion von Mikroobjekten (2002)Fokus-serien basierte Rekonstruktion von Mikroobjekten (2002)
Fokus-serien basierte Rekonstruktion von Mikroobjekten (2002)Jan Wedekind
 
Play Squash with Ruby, OpenGL, and a Wiimote - ShRUG Feb 2011
Play Squash with Ruby, OpenGL, and a Wiimote - ShRUG Feb 2011Play Squash with Ruby, OpenGL, and a Wiimote - ShRUG Feb 2011
Play Squash with Ruby, OpenGL, and a Wiimote - ShRUG Feb 2011Jan Wedekind
 
Digital Imaging with Free Software - Talk at Sheffield Astronomical Society J...
Digital Imaging with Free Software - Talk at Sheffield Astronomical Society J...Digital Imaging with Free Software - Talk at Sheffield Astronomical Society J...
Digital Imaging with Free Software - Talk at Sheffield Astronomical Society J...Jan Wedekind
 
Machine Vision made easy with Ruby - ShRUG June 2010
Machine Vision made easy with Ruby - ShRUG June 2010Machine Vision made easy with Ruby - ShRUG June 2010
Machine Vision made easy with Ruby - ShRUG June 2010Jan Wedekind
 
Computer Vision using Ruby and libJIT - RubyConf 2009
Computer Vision using Ruby and libJIT - RubyConf 2009Computer Vision using Ruby and libJIT - RubyConf 2009
Computer Vision using Ruby and libJIT - RubyConf 2009Jan Wedekind
 
Real-time Computer Vision With Ruby - OSCON 2008
Real-time Computer Vision With Ruby - OSCON 2008Real-time Computer Vision With Ruby - OSCON 2008
Real-time Computer Vision With Ruby - OSCON 2008Jan Wedekind
 
Object Recognition and Real-Time Tracking in Microscope Imaging - IMVIP 2006
Object Recognition and Real-Time Tracking in Microscope Imaging - IMVIP 2006Object Recognition and Real-Time Tracking in Microscope Imaging - IMVIP 2006
Object Recognition and Real-Time Tracking in Microscope Imaging - IMVIP 2006Jan Wedekind
 
Steerable Filters generated with the Hypercomplex Dual-Tree Wavelet Transform...
Steerable Filters generated with the Hypercomplex Dual-Tree Wavelet Transform...Steerable Filters generated with the Hypercomplex Dual-Tree Wavelet Transform...
Steerable Filters generated with the Hypercomplex Dual-Tree Wavelet Transform...Jan Wedekind
 
Ruby & Machine Vision - Talk at Sheffield Hallam University Feb 2009
Ruby & Machine Vision - Talk at Sheffield Hallam University Feb 2009Ruby & Machine Vision - Talk at Sheffield Hallam University Feb 2009
Ruby & Machine Vision - Talk at Sheffield Hallam University Feb 2009Jan Wedekind
 

More from Jan Wedekind (9)

Fokus-serien basierte Rekonstruktion von Mikroobjekten (2002)
Fokus-serien basierte Rekonstruktion von Mikroobjekten (2002)Fokus-serien basierte Rekonstruktion von Mikroobjekten (2002)
Fokus-serien basierte Rekonstruktion von Mikroobjekten (2002)
 
Play Squash with Ruby, OpenGL, and a Wiimote - ShRUG Feb 2011
Play Squash with Ruby, OpenGL, and a Wiimote - ShRUG Feb 2011Play Squash with Ruby, OpenGL, and a Wiimote - ShRUG Feb 2011
Play Squash with Ruby, OpenGL, and a Wiimote - ShRUG Feb 2011
 
Digital Imaging with Free Software - Talk at Sheffield Astronomical Society J...
Digital Imaging with Free Software - Talk at Sheffield Astronomical Society J...Digital Imaging with Free Software - Talk at Sheffield Astronomical Society J...
Digital Imaging with Free Software - Talk at Sheffield Astronomical Society J...
 
Machine Vision made easy with Ruby - ShRUG June 2010
Machine Vision made easy with Ruby - ShRUG June 2010Machine Vision made easy with Ruby - ShRUG June 2010
Machine Vision made easy with Ruby - ShRUG June 2010
 
Computer Vision using Ruby and libJIT - RubyConf 2009
Computer Vision using Ruby and libJIT - RubyConf 2009Computer Vision using Ruby and libJIT - RubyConf 2009
Computer Vision using Ruby and libJIT - RubyConf 2009
 
Real-time Computer Vision With Ruby - OSCON 2008
Real-time Computer Vision With Ruby - OSCON 2008Real-time Computer Vision With Ruby - OSCON 2008
Real-time Computer Vision With Ruby - OSCON 2008
 
Object Recognition and Real-Time Tracking in Microscope Imaging - IMVIP 2006
Object Recognition and Real-Time Tracking in Microscope Imaging - IMVIP 2006Object Recognition and Real-Time Tracking in Microscope Imaging - IMVIP 2006
Object Recognition and Real-Time Tracking in Microscope Imaging - IMVIP 2006
 
Steerable Filters generated with the Hypercomplex Dual-Tree Wavelet Transform...
Steerable Filters generated with the Hypercomplex Dual-Tree Wavelet Transform...Steerable Filters generated with the Hypercomplex Dual-Tree Wavelet Transform...
Steerable Filters generated with the Hypercomplex Dual-Tree Wavelet Transform...
 
Ruby & Machine Vision - Talk at Sheffield Hallam University Feb 2009
Ruby & Machine Vision - Talk at Sheffield Hallam University Feb 2009Ruby & Machine Vision - Talk at Sheffield Hallam University Feb 2009
Ruby & Machine Vision - Talk at Sheffield Hallam University Feb 2009
 

Recently uploaded

EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityNeo4j
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - InfographicHr365.us smith
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantAxelRicardoTrocheRiq
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...soniya singh
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptkotipi9215
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...MyIntelliSource, Inc.
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...kellynguyen01
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxTier1 app
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfkalichargn70th171
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio, Inc.
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfjoe51371421
 
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
 
Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...aditisharan08
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)OPEN KNOWLEDGE GmbH
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackVICTOR MAESTRE RAMIREZ
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number SystemsJheuzeDellosa
 
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfThe Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfkalichargn70th171
 
XpertSolvers: Your Partner in Building Innovative Software Solutions
XpertSolvers: Your Partner in Building Innovative Software SolutionsXpertSolvers: Your Partner in Building Innovative Software Solutions
XpertSolvers: Your Partner in Building Innovative Software SolutionsMehedi Hasan Shohan
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...MyIntelliSource, Inc.
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...ICS
 

Recently uploaded (20)

EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered Sustainability
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - Infographic
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service Consultant
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.ppt
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdf
 
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...
 
Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStack
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number Systems
 
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfThe Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
 
XpertSolvers: Your Partner in Building Innovative Software Solutions
XpertSolvers: Your Partner in Building Innovative Software SolutionsXpertSolvers: Your Partner in Building Innovative Software Solutions
XpertSolvers: Your Partner in Building Innovative Software Solutions
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 

The SOLID Principles for Software Design

  • 1. Thursday, Feb 22nd 2024 1/22 The SOLID Principles Jan Wedekind Thursday, Feb 22nd 2024
  • 2. Motivation Thursday, Feb 22nd 2024 2/22 Find guiding design principles to maintain software quality over time.
  • 3. Software Rot Thursday, Feb 22nd 2024 3/22 Symptoms of rotting software design:a • Rigidity: software difficult (a lot of work) to change • Fragility: changes easily break the software • Immobility: it is easier to rewrite than reuse parts • Viscosity: design preserving methods are harder to employ than hacks aRobert C. Martin: Design Principles and Design Patterns
  • 4. Aims Thursday, Feb 22nd 2024 4/22 In contrast we want to achieve the following:a • Keep software application flexible • Keep software application robust • Keep software application reusable • Keep software application developable aRobert C. Martin: Design Principles and Design Patterns
  • 5. SOLID Authors Thursday, Feb 22nd 2024 5/22 Robert C. Martin • Author of Clean Code, Functional Design, and more books • Author of Design Principles and Design Patterns paper based on his experience and on work by Bertrand Meyer, Barbara Liskov, and Erich Gamma et al. • http://cleancoder.com/ Michael Feathers • Author of Working Effectively With Legacy Code • Summarized Robert C. Martin’s paper using the SOLID acronym • https://www.r7krecon.com/
  • 6. The SOLID Principles Thursday, Feb 22nd 2024 6/22 1. Single responsibility 2. Open-closed 3. Liskov substitution 4. Interface segregation 5. Dependency inversion
  • 7. Single Responsibility - Before Thursday, Feb 22nd 2024 7/22 def adults_to_html(people): result = "<ul>n" for person in people: if person.age >= 18: result += " <li>" + person.name + "</li>n" result += "</ul>" return result # ... page = adults_to_html(people)
  • 8. Single Responsibility - After Thursday, Feb 22nd 2024 8/22 def select_adults(people): return [person for person in people if person.age >= 18] def people_to_html(people): result = "<ul>n" for person in people: result += " <li>" + person.name + "</li>n" result += "</ul>" return result # ... page = people_to_html(select_adults(people))
  • 9. Open-Closed - Before Thursday, Feb 22nd 2024 9/22 def total_area(shapes): result = 0 for shape in shapes: match type(shape): case Rectangle: result += shape.width * shape.height case Sphere: result += math.pi * shape.radius ** 2 case _: raise f"Unsupported shape {shape}" return result
  • 10. Open-Closed - After Thursday, Feb 22nd 2024 10/22 class Rectangle: def area(self): return self.width * self.height class Circle: def area(self): return math.pi * self.radius ** 2 def total_area(shapes): result = 0 for shape in shapes: result += shape.area() return result
  • 11. Liskov-Substitution - Before Thursday, Feb 22nd 2024 11/22 class Rectangle: def __init__(self, width, height): self.width = width self.height = height def set_width(self, width): self.width = width def set_height(self, height): self.height = height class Square(Rectangle): def __init__(self, side): super().__init__(side, side) def set_width(self, width): super().set_width(width) super().set_height(width) def set_height(self, height): self.set_width(height)
  • 12. Liskov-Substitution - After Thursday, Feb 22nd 2024 12/22 class Shape: pass class Rectangle(Shape): def __init__(self, width, height): self.width = width self.height = height def set_width(self, width): self.width = width def set_height(self, height): self.height = height class Square(Shape): def __init__(self, side): self.side = side def set_side(self, side): self.side = side Barbara Liskov
  • 13. Liskov-Substitution - Contracts Thursday, Feb 22nd 2024 13/22 “The Liskov Substitution Principle states, among other constraints, that a subtype is not substitutable for its super type if it strengthens its operations’ preconditions, or weakens its operations’ postconditions”a precondition precondition postcondition postcondition type subtype method method aBaniassad: Making the Liskov Substitution Principle Happy and Sad
  • 14. Interface Segregation - Before Thursday, Feb 22nd 2024 14/22 class AccountHolder: def __init__(self, name, age, balance): self.name = name self.age = age self.balance = balance def is_adult(self): return self.adult >= 18 def deposit(self, amount): self.balance += amount def withdraw(self, amount): self.balance -= amount
  • 15. Interface Segregation - After Thursday, Feb 22nd 2024 15/22 class Person: def __init__(self, name, age): self.name, self.age = name, age def is_adult(self): return self.adult >= 18 class Account: def __init__(self, balance): self.balance = balance def deposit(self, amount): self.balance += amount def withdraw(self, amount): self.balance -= amount class AccountHolder(Person): def __init__(self, name, age, account): super().__init__(name, age) self.account = account
  • 16. Dependency Inversion - Before Thursday, Feb 22nd 2024 16/22 def get_names(connection): cursor = connection.cursor() cursor.execute('SELECT name FROM member_table') rows = cursor.fetchall() names = [row[0] for row in rows] return names connection = sqlite3.connect('example.db') names_list = get_names(connection) connection.close() print(names_list)
  • 17. Dependency Inversion - After Thursday, Feb 22nd 2024 17/22 class Database(abc.ABC): @abc.abstractmethod def sql(self, query): pass class SQLiteDatabase(Database): def __init__(self, db_file_name): self.connection = sqlite3.connect(db_file_name) def __del__(self): self.connection.close() def sql(self, query): cursor = self.connection.cursor() cursor.execute(query) return cursor.fetchall() def get_names(database): rows = database.sql('SELECT name FROM member_table') return [row[0] for row in rows]
  • 18. Dependency Inversion - After Thursday, Feb 22nd 2024 18/22 database = SQLiteDatabase('example.db') names_list = get_names(database) print(names_list)
  • 19. Aspects of a Class Thursday, Feb 22nd 2024 19/22 The 5 aspects of the class are:a responsibility towards parent interface towards callers interface towards callees responsibility towards inheritors class' purpose aMike Lindner: The Five Principles For SOLID Software Design
  • 20. The 5 Principles Thursday, Feb 22nd 2024 20/22 The 5 corresponding principles are:a Liskov substitution principle single responsibility principle interface segregation principle dependency inversion principle open-closed principle aMike Lindner: The Five Principles For SOLID Software Design
  • 21. Arjan Egges: Uncle Bob’s SOLID Principles Made Easy Thursday, Feb 22nd 2024 21/22 19 minutes video
  • 22. Jim Weirich: The Building Blocks of Modularity Thursday, Feb 22nd 2024 22/22 33 minutes video