SlideShare a Scribd company logo
1 of 36
SOLID
The Principles of Clean Object-Oriented Programming
Attila Bertók
C# technical lead
bertok.atti@gmail.com
https://www.linkedin.com/in/bertokattila/
SOLID
 Five principles of agile object-oriented software design
 Robert C. Martin: Agile Software Development: Principles, Patterns, and
Practices (2002)
 Robert C. Martin: Agile Software Development: Principles, Patterns, and
Practices in C# (2003)
 Robert C. Martin: Clean Code (2008)
 Robert C. Martin: Clean Architecture (2017)
SOLID – SRP
Single Responsibility Principle
SOLID - SRP
 The least well understood principle of SOLID. 
 It is not about the module (method/class/component) doing exactly one
thing.
 Correct phrasing:
“A module should be responsible to one, and only one, user or stakeholder – and
thus have one, and only one, reason to change.”
 This, in fact, does nevertheless contain the above definition of a module
doing exactly one thing.
Cohesion & Coupling
 We will discuss these in more detail when discussing Static Code Analysis
Metrics
 Coupling: interdependency between modules
 Cohesion: relatedness of functionality within one module
 Low Coupling and High Cohesion should be the goal
 SRP is basically stating that “Cohesion should be as high as possible”
SOLID - SRP
 “A class should have only one reason to change”
 As an example, consider a module that compiles and prints a report.
 Such a module can be changed for two reasons.
 The content of the report could change.
 The format of the report could change.
 These two things change for very different causes; one substantive, and one
cosmetic.
 The single responsibility principle says that these two aspects of the problem
are really two separate responsibilities, and should therefore be in separate
classes or modules. It would be a bad design to couple two things that change
for different reasons at different times.
Another Example: The Modem
public interface Modem
{
public void Dial(string phoneNumber);
public void Hangup();
public void Send(char c);
public char Receive();
}
Modem Example, Part 2
<<I>> Data Channel
+ Send(char)
+ Receive : char
<<I>> Connection
+ Dial(string)
+ Hangup()
Modem
// with two responsibilities
SOLID - SRP
 One “thing” should do exactly one “thing”
 What is a “thing”?
 Usually a class
 Sometimes a function
 Trivially a variable
 Would you write code like this?
bool isLowerCase = string.Equals(input, input.ToLower());
if (isLowerCase) {
isLowerCase = input.Length < 50;
// why declare another bool, we no longer use the previous one, lets repurpose!
}
SOLID - SRP
 Why would you then do something like this?
public void DoStuff(bool flag) {
if (flag) {
// do something
} else {
// do something else
}
}
 The above is only correct, if DoStuff() is a wrapper function, that you use to
dynamically invoke either DoSomething() or DoSomethingElse()
SOLID - SRP
 The same holds true for classes
 One class should only be responsible for one single thing
 So, increase cohesion!
 Use small classes
 Reduce the number of instance variables!
 Move functionality on different level of abstraction to a separate class
 Methods that do not manipulate instance variables might not be part of the class (static
methods)
 Use composition
An Example of Strong Cohesion
 Demo
SOLID – O/CP
Open/Closed Principle
SOLID – O/CP
 Classes should be open for extension but closed for modification
 Changes should be localizable – a single change should not cascade down and
require dependent modules to change as well
 Changes should be achieved by adding new code instead of modified existing
code
SOLID – O/CP
 Open for extension: This means that the behavior of the module can be
extended. As the requirements of the application change, we can extend the
module with new behaviors that satisfy those changes. In other words, we are
able to change what the module does.
 Closed for modification: Extending the behavior of a module does not result
in changes to the source, or binary, code of the module. The binary
executable version of the module whether in a linkable library, a DLL, or a
.EXE file remains untouched.
 Is it possible to satisfy both criteria at once?
SOLID – O/CP
 Use abstraction!
 An interface or an abstract base class can be fixed, thus closed for
modification. However, the implementation is an extension point, so the
abstract module remains open for extension.
 Some common design patterns to satisfy OCP are:
 Strategy
 Template Method
 Visitor
SOLID – LSP
Liskov Substitution Principle
SOLID – LSP
 Derived classes can be used instead of the classes they derive of
Animal
Bird Dog
Move
Fly Walk
SOLID – LSP
Bad solution
public class Bird : IAnimal {
public void Move() {
throw
NotImplementedException
(“Use fly()!”);
}
public void Fly() {
FlapWings();
}
}
Good solution
public class Bird : IAnimal {
public void Move() {
Fly();
}
public void Fly() {
FlapWings();
}
}
SOLID – LSP
public class Bird : IAnimal {
public void Move() {
Fly();
}
}
public class Dog : IAnimal {
public void Move() {
Walk();
}
}
public void MoveAllAnimals(IEnumerable<IAnimal> animals) {
foreach (animal in Animals) {
animal.Move();
}
}
SOLID – LSP
 Common example of breaking the LSP: the Square/Rectangle problem
 A square is (by mathematic definition) a rectangle.
 public class Rectangle {
public virtual int Height { get; set; }
public virtual int Width { get; set; }
}
 public class Square : Rectangle {
private int side;
public override int Height {
get { return side; }
set { side = value; }
}
public override int Width {
get { return side; }
set { width = side; }
}
}
SOLID – LSP
 Nah, that’s okay, where’s the issue?
 Well…
Rectangle r = RectangleFactory.Create();
r.Height = 2;
r.Width = 5;
r.Area.Should().Be(10);
works just fine if the RectangleFactory returns a Rectangle, but fails, if it returns a
Square – which is a valid return value, as a square is a rectangle in our
implementation.
SOLID – ISP
Interface Segregation Principle
SOLID – ISP
 Use multiple small interfaces
 Split interfaces by functional boundaries
 When a method only requires something related to the interface, use the
interface as the type of the input parameter
 Why?
 So that your client won’t do anything stupid.
 So that your client doesn’t depend on things it does not need.
SOLID – ISP
Bad solution
A solution I’ve worked on: IInstance.
 428 lines of code
 100+ methods
 8 regions:
 Configuration and Tracing
 LoginManager
 WMSManager
 ProductionManager
 …
Good solution
IInstance : ILoginManager,
IWmsManager, IProductionManager, …
Usage:
ProductionEngineWrapper
(IProductionManager
productionManager) {
// can only call
// production related code
// from this method
// but not WMS-related
}
Another Example
class Printer
{
public enum PaperType { A3, A4, Letter };
public PaperType Paper { get; set; }
}
class MyGraph
{
// details
}
Another Example, cont’d
static void Main()
{
MyGraph graph = new MyGraph();
graph.Load(@"C:settings.txt);
PrintAGraphInA4(graph);
}
static void PrintAGraphInA4(MyGraph
graph)
{
// Note:
// the code only needed access
// to the Print Method
// but was exposed to
// Load/Save/
// MathModel/GraphTitle/etc.
Printer printer = new Printer();
printer.Paper =
Printer.PaperType.A4;
graph.Print(printer);
}
Another Example, corrected
class Printer
{
public enum PaperType { A3, A4, Letter };
public PaperType Paper { get; set; }
}
interface IPrintable
{
void Print(Printer printerDevice);
}
class MyGraph : IPrintable
{
// details
}
Corrected example, continued
static void Main()
{
MyGraph graph = new MyGraph();
graph.Load(@"C:settings.txt”);
// because inheritance is used for
// MyGraph : Iprintable
// we can simply pass the graph
// to automatically cast
// to Iprintable
PrintInA4(graph);
}
static void PrintInA4(IPrintable
printable)
{
// we do not care
// what we are printing.
Printer printer = new Printer();
printer.Paper =
Printer.PaperType.A4_PAPER;
printable.Print(printer);
}
SOLID - DIP
Dependency Inversion Principle
SOLID - DIP
 Rely on abstractions and interfaces, not concrete implementations
 High level modules should not depend on low level modules.
 Why?
 See O/C principle! Implementations are subject to change, abstractions are usually
not!
 The level of abstraction is broken. Your object should not care about the lifetime
of the objects it uses.
 Objects should be used
 Not created and destroyed
SOLID - DIP
Bad solution
public class MainViewModel {
public IInstructionViewModel
InstructionViewModel
{ get; set; }
public IProductionViewModel
ProductionViewModel
{ get; set; }
public MainViewModel() {
InstructionViewModel = new()
ProductionViewModel = new()
}
}
Good solution
public class MainViewModel {
public IInstructionViewModel
InstructionViewModel
{ get; set; }
public IProductionViewModel
ProductionViewModel
{ get; set; }
public MainViewModel(
IInstructionViewModel ivm,
IProductionViewModel pvm) {
this.InstructionViewModel = ivm
ProductionViewModel = pvm
}
}
 Where do I get my dependencies from?
 Factory methods
 It’s not “Enterprise Code©®™” without factories!
 DI framework! (Service Locator or Dependency Container patterns)
Q&A
Questions?
Thank you!

More Related Content

What's hot

01 c++ Intro.ppt
01 c++ Intro.ppt01 c++ Intro.ppt
01 c++ Intro.pptTareq Hasan
 
(Costless) Software Abstractions for Parallel Architectures
(Costless) Software Abstractions for Parallel Architectures(Costless) Software Abstractions for Parallel Architectures
(Costless) Software Abstractions for Parallel ArchitecturesJoel Falcou
 
Principle source of optimazation
Principle source of optimazationPrinciple source of optimazation
Principle source of optimazationSiva Sathya
 
Automatic Task-based Code Generation for High Performance DSEL
Automatic Task-based Code Generation for High Performance DSELAutomatic Task-based Code Generation for High Performance DSEL
Automatic Task-based Code Generation for High Performance DSELJoel Falcou
 
SOLID - Principles of Object Oriented Design
SOLID - Principles of Object Oriented DesignSOLID - Principles of Object Oriented Design
SOLID - Principles of Object Oriented DesignRiccardo Cardin
 
Introduction to database-ER Model
Introduction to database-ER ModelIntroduction to database-ER Model
Introduction to database-ER ModelAjit Nayak
 
C, C++ Interview Questions Part - 1
C, C++ Interview Questions Part - 1C, C++ Interview Questions Part - 1
C, C++ Interview Questions Part - 1ReKruiTIn.com
 
More Little Wonders of C#/.NET
More Little Wonders of C#/.NETMore Little Wonders of C#/.NET
More Little Wonders of C#/.NETBlackRabbitCoder
 
Improving Software Quality Using Object Oriented Design Principles
Improving Software Quality Using Object Oriented Design PrinciplesImproving Software Quality Using Object Oriented Design Principles
Improving Software Quality Using Object Oriented Design PrinciplesDr. Syed Hassan Amin
 
Generic and Meta-Transformations for Model Transformation Engineering
Generic and Meta-Transformations for Model Transformation EngineeringGeneric and Meta-Transformations for Model Transformation Engineering
Generic and Meta-Transformations for Model Transformation EngineeringDaniel Varro
 
Basics of kotlin ASJ
Basics of kotlin ASJBasics of kotlin ASJ
Basics of kotlin ASJDSCBVRITH
 
OODP Unit 1 OOPs classes and objects
OODP Unit 1 OOPs classes and objectsOODP Unit 1 OOPs classes and objects
OODP Unit 1 OOPs classes and objectsShanmuganathan C
 
Interoduction to c++
Interoduction to c++Interoduction to c++
Interoduction to c++Amresh Raj
 
Pointer and Object in C++
Pointer and Object in C++Pointer and Object in C++
Pointer and Object in C++Kamlesh Makvana
 
AspectC++: Language Proposal and Prototype Implementation
AspectC++: Language Proposal and Prototype ImplementationAspectC++: Language Proposal and Prototype Implementation
AspectC++: Language Proposal and Prototype Implementationdinomasch
 

What's hot (20)

C++ Interview Questions
C++ Interview QuestionsC++ Interview Questions
C++ Interview Questions
 
C by balaguruswami - e.balagurusamy
C   by balaguruswami - e.balagurusamyC   by balaguruswami - e.balagurusamy
C by balaguruswami - e.balagurusamy
 
01 c++ Intro.ppt
01 c++ Intro.ppt01 c++ Intro.ppt
01 c++ Intro.ppt
 
(Costless) Software Abstractions for Parallel Architectures
(Costless) Software Abstractions for Parallel Architectures(Costless) Software Abstractions for Parallel Architectures
(Costless) Software Abstractions for Parallel Architectures
 
Principle source of optimazation
Principle source of optimazationPrinciple source of optimazation
Principle source of optimazation
 
Automatic Task-based Code Generation for High Performance DSEL
Automatic Task-based Code Generation for High Performance DSELAutomatic Task-based Code Generation for High Performance DSEL
Automatic Task-based Code Generation for High Performance DSEL
 
SOLID - Principles of Object Oriented Design
SOLID - Principles of Object Oriented DesignSOLID - Principles of Object Oriented Design
SOLID - Principles of Object Oriented Design
 
Introduction to database-ER Model
Introduction to database-ER ModelIntroduction to database-ER Model
Introduction to database-ER Model
 
C, C++ Interview Questions Part - 1
C, C++ Interview Questions Part - 1C, C++ Interview Questions Part - 1
C, C++ Interview Questions Part - 1
 
C++ interview question
C++ interview questionC++ interview question
C++ interview question
 
More Little Wonders of C#/.NET
More Little Wonders of C#/.NETMore Little Wonders of C#/.NET
More Little Wonders of C#/.NET
 
Improving Software Quality Using Object Oriented Design Principles
Improving Software Quality Using Object Oriented Design PrinciplesImproving Software Quality Using Object Oriented Design Principles
Improving Software Quality Using Object Oriented Design Principles
 
Generic and Meta-Transformations for Model Transformation Engineering
Generic and Meta-Transformations for Model Transformation EngineeringGeneric and Meta-Transformations for Model Transformation Engineering
Generic and Meta-Transformations for Model Transformation Engineering
 
Basics of kotlin ASJ
Basics of kotlin ASJBasics of kotlin ASJ
Basics of kotlin ASJ
 
OODP Unit 1 OOPs classes and objects
OODP Unit 1 OOPs classes and objectsOODP Unit 1 OOPs classes and objects
OODP Unit 1 OOPs classes and objects
 
Interoduction to c++
Interoduction to c++Interoduction to c++
Interoduction to c++
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator Overloading
 
Pointer and Object in C++
Pointer and Object in C++Pointer and Object in C++
Pointer and Object in C++
 
2. data, operators, io
2. data, operators, io2. data, operators, io
2. data, operators, io
 
AspectC++: Language Proposal and Prototype Implementation
AspectC++: Language Proposal and Prototype ImplementationAspectC++: Language Proposal and Prototype Implementation
AspectC++: Language Proposal and Prototype Implementation
 

Similar to An Introduction to the SOLID Principles

Object Oriented Principle&rsquo;s
Object Oriented Principle&rsquo;sObject Oriented Principle&rsquo;s
Object Oriented Principle&rsquo;svivek p s
 
Object Oriented Concepts and Principles
Object Oriented Concepts and PrinciplesObject Oriented Concepts and Principles
Object Oriented Concepts and Principlesdeonpmeyer
 
Solid Principles
Solid PrinciplesSolid Principles
Solid PrinciplesHitheshh
 
SOLID Design Principles
SOLID Design PrinciplesSOLID Design Principles
SOLID Design PrinciplesAndreas Enbohm
 
The software design principles
The software design principlesThe software design principles
The software design principlesAman Kesarwani
 
How much do we know about Object-Oriented Programming?
How much do we know about Object-Oriented Programming?How much do we know about Object-Oriented Programming?
How much do we know about Object-Oriented Programming?Sandro Mancuso
 
Mastering Python lesson 4_functions_parameters_arguments
Mastering Python lesson 4_functions_parameters_argumentsMastering Python lesson 4_functions_parameters_arguments
Mastering Python lesson 4_functions_parameters_argumentsRuth Marvin
 
Open Problems in Automatically Refactoring Legacy Java Software to use New Fe...
Open Problems in Automatically Refactoring Legacy Java Software to use New Fe...Open Problems in Automatically Refactoring Legacy Java Software to use New Fe...
Open Problems in Automatically Refactoring Legacy Java Software to use New Fe...Raffi Khatchadourian
 
principles of object oriented class design
principles of object oriented class designprinciples of object oriented class design
principles of object oriented class designNeetu Mishra
 
The maze of Design Patterns & SOLID Principles
The maze of Design Patterns & SOLID PrinciplesThe maze of Design Patterns & SOLID Principles
The maze of Design Patterns & SOLID PrinciplesMuhammad Raza
 
Project Lambda: Functional Programming Constructs in Java - Simon Ritter (Ora...
Project Lambda: Functional Programming Constructs in Java - Simon Ritter (Ora...Project Lambda: Functional Programming Constructs in Java - Simon Ritter (Ora...
Project Lambda: Functional Programming Constructs in Java - Simon Ritter (Ora...jaxLondonConference
 
D1 from interfaces to solid
D1 from interfaces to solidD1 from interfaces to solid
D1 from interfaces to solidArnaud Bouchez
 
Diving in OOP (Day 1) : Polymorphism and Inheritance (Early Binding/Compile T...
Diving in OOP (Day 1) : Polymorphism and Inheritance (Early Binding/Compile T...Diving in OOP (Day 1) : Polymorphism and Inheritance (Early Binding/Compile T...
Diving in OOP (Day 1) : Polymorphism and Inheritance (Early Binding/Compile T...Akhil Mittal
 
OOP-Advanced Programming with c++
OOP-Advanced Programming with c++OOP-Advanced Programming with c++
OOP-Advanced Programming with c++Mohamed Essam
 

Similar to An Introduction to the SOLID Principles (20)

Object Oriented Principle&rsquo;s
Object Oriented Principle&rsquo;sObject Oriented Principle&rsquo;s
Object Oriented Principle&rsquo;s
 
Object Oriented Concepts and Principles
Object Oriented Concepts and PrinciplesObject Oriented Concepts and Principles
Object Oriented Concepts and Principles
 
SOLID
SOLIDSOLID
SOLID
 
Solid Principles
Solid PrinciplesSolid Principles
Solid Principles
 
SOLID Design Principles
SOLID Design PrinciplesSOLID Design Principles
SOLID Design Principles
 
The software design principles
The software design principlesThe software design principles
The software design principles
 
Java 8-revealed
Java 8-revealedJava 8-revealed
Java 8-revealed
 
How much do we know about Object-Oriented Programming?
How much do we know about Object-Oriented Programming?How much do we know about Object-Oriented Programming?
How much do we know about Object-Oriented Programming?
 
Design patterns
Design patternsDesign patterns
Design patterns
 
Mastering Python lesson 4_functions_parameters_arguments
Mastering Python lesson 4_functions_parameters_argumentsMastering Python lesson 4_functions_parameters_arguments
Mastering Python lesson 4_functions_parameters_arguments
 
Open Problems in Automatically Refactoring Legacy Java Software to use New Fe...
Open Problems in Automatically Refactoring Legacy Java Software to use New Fe...Open Problems in Automatically Refactoring Legacy Java Software to use New Fe...
Open Problems in Automatically Refactoring Legacy Java Software to use New Fe...
 
Abstraction
Abstraction Abstraction
Abstraction
 
Abstraction
AbstractionAbstraction
Abstraction
 
principles of object oriented class design
principles of object oriented class designprinciples of object oriented class design
principles of object oriented class design
 
The maze of Design Patterns & SOLID Principles
The maze of Design Patterns & SOLID PrinciplesThe maze of Design Patterns & SOLID Principles
The maze of Design Patterns & SOLID Principles
 
Project Lambda: Functional Programming Constructs in Java - Simon Ritter (Ora...
Project Lambda: Functional Programming Constructs in Java - Simon Ritter (Ora...Project Lambda: Functional Programming Constructs in Java - Simon Ritter (Ora...
Project Lambda: Functional Programming Constructs in Java - Simon Ritter (Ora...
 
D1 from interfaces to solid
D1 from interfaces to solidD1 from interfaces to solid
D1 from interfaces to solid
 
Diving in OOP (Day 1) : Polymorphism and Inheritance (Early Binding/Compile T...
Diving in OOP (Day 1) : Polymorphism and Inheritance (Early Binding/Compile T...Diving in OOP (Day 1) : Polymorphism and Inheritance (Early Binding/Compile T...
Diving in OOP (Day 1) : Polymorphism and Inheritance (Early Binding/Compile T...
 
What`s New in Java 8
What`s New in Java 8What`s New in Java 8
What`s New in Java 8
 
OOP-Advanced Programming with c++
OOP-Advanced Programming with c++OOP-Advanced Programming with c++
OOP-Advanced Programming with c++
 

Recently uploaded

Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraDeakin University
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr LapshynFwdays
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024The Digital Insurer
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Wonjun Hwang
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
costume and set research powerpoint presentation
costume and set research powerpoint presentationcostume and set research powerpoint presentation
costume and set research powerpoint presentationphoebematthew05
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 

Recently uploaded (20)

Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning era
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food Manufacturing
 
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptxVulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
costume and set research powerpoint presentation
costume and set research powerpoint presentationcostume and set research powerpoint presentation
costume and set research powerpoint presentation
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort ServiceHot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 

An Introduction to the SOLID Principles

  • 1. SOLID The Principles of Clean Object-Oriented Programming
  • 2. Attila Bertók C# technical lead bertok.atti@gmail.com https://www.linkedin.com/in/bertokattila/
  • 3. SOLID  Five principles of agile object-oriented software design  Robert C. Martin: Agile Software Development: Principles, Patterns, and Practices (2002)  Robert C. Martin: Agile Software Development: Principles, Patterns, and Practices in C# (2003)  Robert C. Martin: Clean Code (2008)  Robert C. Martin: Clean Architecture (2017)
  • 4. SOLID – SRP Single Responsibility Principle
  • 5. SOLID - SRP  The least well understood principle of SOLID.   It is not about the module (method/class/component) doing exactly one thing.  Correct phrasing: “A module should be responsible to one, and only one, user or stakeholder – and thus have one, and only one, reason to change.”  This, in fact, does nevertheless contain the above definition of a module doing exactly one thing.
  • 6. Cohesion & Coupling  We will discuss these in more detail when discussing Static Code Analysis Metrics  Coupling: interdependency between modules  Cohesion: relatedness of functionality within one module  Low Coupling and High Cohesion should be the goal  SRP is basically stating that “Cohesion should be as high as possible”
  • 7. SOLID - SRP  “A class should have only one reason to change”  As an example, consider a module that compiles and prints a report.  Such a module can be changed for two reasons.  The content of the report could change.  The format of the report could change.  These two things change for very different causes; one substantive, and one cosmetic.  The single responsibility principle says that these two aspects of the problem are really two separate responsibilities, and should therefore be in separate classes or modules. It would be a bad design to couple two things that change for different reasons at different times.
  • 8. Another Example: The Modem public interface Modem { public void Dial(string phoneNumber); public void Hangup(); public void Send(char c); public char Receive(); }
  • 9. Modem Example, Part 2 <<I>> Data Channel + Send(char) + Receive : char <<I>> Connection + Dial(string) + Hangup() Modem // with two responsibilities
  • 10. SOLID - SRP  One “thing” should do exactly one “thing”  What is a “thing”?  Usually a class  Sometimes a function  Trivially a variable  Would you write code like this? bool isLowerCase = string.Equals(input, input.ToLower()); if (isLowerCase) { isLowerCase = input.Length < 50; // why declare another bool, we no longer use the previous one, lets repurpose! }
  • 11. SOLID - SRP  Why would you then do something like this? public void DoStuff(bool flag) { if (flag) { // do something } else { // do something else } }  The above is only correct, if DoStuff() is a wrapper function, that you use to dynamically invoke either DoSomething() or DoSomethingElse()
  • 12. SOLID - SRP  The same holds true for classes  One class should only be responsible for one single thing  So, increase cohesion!  Use small classes  Reduce the number of instance variables!  Move functionality on different level of abstraction to a separate class  Methods that do not manipulate instance variables might not be part of the class (static methods)  Use composition
  • 13. An Example of Strong Cohesion  Demo
  • 15. SOLID – O/CP  Classes should be open for extension but closed for modification  Changes should be localizable – a single change should not cascade down and require dependent modules to change as well  Changes should be achieved by adding new code instead of modified existing code
  • 16. SOLID – O/CP  Open for extension: This means that the behavior of the module can be extended. As the requirements of the application change, we can extend the module with new behaviors that satisfy those changes. In other words, we are able to change what the module does.  Closed for modification: Extending the behavior of a module does not result in changes to the source, or binary, code of the module. The binary executable version of the module whether in a linkable library, a DLL, or a .EXE file remains untouched.  Is it possible to satisfy both criteria at once?
  • 17. SOLID – O/CP  Use abstraction!  An interface or an abstract base class can be fixed, thus closed for modification. However, the implementation is an extension point, so the abstract module remains open for extension.  Some common design patterns to satisfy OCP are:  Strategy  Template Method  Visitor
  • 18. SOLID – LSP Liskov Substitution Principle
  • 19. SOLID – LSP  Derived classes can be used instead of the classes they derive of Animal Bird Dog Move Fly Walk
  • 20. SOLID – LSP Bad solution public class Bird : IAnimal { public void Move() { throw NotImplementedException (“Use fly()!”); } public void Fly() { FlapWings(); } } Good solution public class Bird : IAnimal { public void Move() { Fly(); } public void Fly() { FlapWings(); } }
  • 21. SOLID – LSP public class Bird : IAnimal { public void Move() { Fly(); } } public class Dog : IAnimal { public void Move() { Walk(); } } public void MoveAllAnimals(IEnumerable<IAnimal> animals) { foreach (animal in Animals) { animal.Move(); } }
  • 22. SOLID – LSP  Common example of breaking the LSP: the Square/Rectangle problem  A square is (by mathematic definition) a rectangle.  public class Rectangle { public virtual int Height { get; set; } public virtual int Width { get; set; } }  public class Square : Rectangle { private int side; public override int Height { get { return side; } set { side = value; } } public override int Width { get { return side; } set { width = side; } } }
  • 23. SOLID – LSP  Nah, that’s okay, where’s the issue?  Well… Rectangle r = RectangleFactory.Create(); r.Height = 2; r.Width = 5; r.Area.Should().Be(10); works just fine if the RectangleFactory returns a Rectangle, but fails, if it returns a Square – which is a valid return value, as a square is a rectangle in our implementation.
  • 24. SOLID – ISP Interface Segregation Principle
  • 25. SOLID – ISP  Use multiple small interfaces  Split interfaces by functional boundaries  When a method only requires something related to the interface, use the interface as the type of the input parameter  Why?  So that your client won’t do anything stupid.  So that your client doesn’t depend on things it does not need.
  • 26. SOLID – ISP Bad solution A solution I’ve worked on: IInstance.  428 lines of code  100+ methods  8 regions:  Configuration and Tracing  LoginManager  WMSManager  ProductionManager  … Good solution IInstance : ILoginManager, IWmsManager, IProductionManager, … Usage: ProductionEngineWrapper (IProductionManager productionManager) { // can only call // production related code // from this method // but not WMS-related }
  • 27. Another Example class Printer { public enum PaperType { A3, A4, Letter }; public PaperType Paper { get; set; } } class MyGraph { // details }
  • 28. Another Example, cont’d static void Main() { MyGraph graph = new MyGraph(); graph.Load(@"C:settings.txt); PrintAGraphInA4(graph); } static void PrintAGraphInA4(MyGraph graph) { // Note: // the code only needed access // to the Print Method // but was exposed to // Load/Save/ // MathModel/GraphTitle/etc. Printer printer = new Printer(); printer.Paper = Printer.PaperType.A4; graph.Print(printer); }
  • 29. Another Example, corrected class Printer { public enum PaperType { A3, A4, Letter }; public PaperType Paper { get; set; } } interface IPrintable { void Print(Printer printerDevice); } class MyGraph : IPrintable { // details }
  • 30. Corrected example, continued static void Main() { MyGraph graph = new MyGraph(); graph.Load(@"C:settings.txt”); // because inheritance is used for // MyGraph : Iprintable // we can simply pass the graph // to automatically cast // to Iprintable PrintInA4(graph); } static void PrintInA4(IPrintable printable) { // we do not care // what we are printing. Printer printer = new Printer(); printer.Paper = Printer.PaperType.A4_PAPER; printable.Print(printer); }
  • 31. SOLID - DIP Dependency Inversion Principle
  • 32. SOLID - DIP  Rely on abstractions and interfaces, not concrete implementations  High level modules should not depend on low level modules.  Why?  See O/C principle! Implementations are subject to change, abstractions are usually not!  The level of abstraction is broken. Your object should not care about the lifetime of the objects it uses.  Objects should be used  Not created and destroyed
  • 33. SOLID - DIP Bad solution public class MainViewModel { public IInstructionViewModel InstructionViewModel { get; set; } public IProductionViewModel ProductionViewModel { get; set; } public MainViewModel() { InstructionViewModel = new() ProductionViewModel = new() } } Good solution public class MainViewModel { public IInstructionViewModel InstructionViewModel { get; set; } public IProductionViewModel ProductionViewModel { get; set; } public MainViewModel( IInstructionViewModel ivm, IProductionViewModel pvm) { this.InstructionViewModel = ivm ProductionViewModel = pvm } }
  • 34.  Where do I get my dependencies from?  Factory methods  It’s not “Enterprise Code©®™” without factories!  DI framework! (Service Locator or Dependency Container patterns)