SlideShare a Scribd company logo
1 of 30
Download to read offline
HÖNNUN OG SMÍÐI HUGBÚNAÐAR 2015
L22 DESIGN PRINCIPLES
Agenda
▪ Why Principles?
▪ SOLID Principle
– 1. Single Responsibility Principle
– 2. Open/Closed Principle
– 3. Liskov Substitution Principle
– 4. Interface Segregation Principle
– 5. Dependency Inversion Principle
Reading
▪ SOLID - the first five principles of Object Oriented Deign by Samuel
Oloruntoba
Big Ball of Mud
Is this your code?
Technical Dept
Referring to the eventual consequences of poor
system design
Hacks, fix it later, mistakes etc
Why Principles?
Based on knowledge and research
Encourage writing high quality code
Promote better communication
Why Principles?
Should not be taken as dogmatic
All design decision have context and specific set of
problems to solve
SOLID Principles
Promotes Object Oriented Design and Development
S
O
L
I
D
Single Responsibility Principle
Open/Closed Principle
Liskov Substitution Principle
Interface Segregation Principle
Dependency Inversion Principle
Single Responsibility Principle (SRP)
A class should have one and only one reason to
change, meaning that a class should have only
one job
Violating this principle leads to low cohesion
and make the code brittle and loads to code
bloat and confusion
It is not easy to spot
Single Responsibility Principle
Cohesion refers to the degree to which the elements
of a module belong together
High cohesion: methods that serve the given class
tend to be similar in many aspects
Increased understanding of modules
Increased ease in maintaining a system
Increased ease in reusing a module
Single Responsibility Principle
Example: Rectangle has two responsibilities
The Open-Closed Principle (OCP)
Adding new functionality would involve minimal
changes to existing code
Objects or entities should be open for
extension, but closed for modification.
The Open-Closed Principle (OCP)
Most changes will be handled as new methods and
new classes
Designs following this principle would result in
resilient code which does not break on addition of
new functionality
public class ResourceAllocator
{
...
public int allocate(intresourceType)
{
intresourceId;
switch (resourceType)
{
case TIME_SLOT:
resourceId = findFreeTimeSlot();
markTimeslotBusy(resourceId);
break;
case SPACE_SLOT:
resourceId = findFreeSpaceSlot();
markSpaceSlotBusy(resourceId);
break;
...
}
return resourceId;
}
...
Resource Allocator Example
Resource Allocator Example
List resources = new ArrayList();
...
public int allocate(intresourceType)
{
int resourceId = findFreeResource(resourceType);
markAsBusy(resourceId);
return resourceId;
}
Designed for extension
The Liskov Substitution Principle (LSP)
Let q(x) be a property provable about
objects of x of type T. Then q(y) should be
provable for objects y of type S where S is
a subtype of T.
All code operating with reference to the base
class should be completely transparent to the
type of the inherited object
The Liskov Substitution Principle (LSP)
It should be possible to substitute an object of
one type with another within the same class
hierarchy
Inheriting classes should not perform any
actions that will invalidate the assumptions
made by the base class
LSP Example
public class Rectangle {
protected int _width;
protected int _height;
public int getWidth() {
return _width;
}
public int getHeight() {
return _height;
}
public void setWidth(int width) {
_width = width;
}
public void setHeight(int height) {
_height = height;
}
}
LSP Example
public class Square extends Rectangle {
public void setWidth(int width) {
_width = width;
_height = width;
}
public void setHeight(int height) {
_height = height;
_width = _height;
}
}
Implementation convenience
LSP Example
import junit.framework.Assert;
import org.junit.Test;
public class RectangleTests
{
@Test
public void areaOfRectangle() {
Rectangle r = new Square();
r.setWidth(5);
r.setHeight(2);
// Will Fail - r is a square and sets
// width and height equal to each other.
Assert.assertEquals(r.getWidth() * r.getHeight(),10);
}
}
Interface Segregation Principle (ISP)
A client should never be forced to
implement an interface that it doesn’t use
or clients shouldn’t be forced to depend
on methods they do not use.
Split large interfaces into smaller and more
specific interfaces
Interface Segregation Principle (ISP)
The smaller the interface, the better
For fat or polluted interfaces clients are forced to
depend on methods they do not use
Dependency Inversion Principle (DIP)
Entities must depend on abstractions not
on concretions. It states that the high level
module must not depend on the low level
module, but they should depend on
abstractions.
Low-level components should depend on high-level,
not vice versa.
Dependency Inversion Principle (DIP)
The high-level components should be the one
defining logic and enforcing change
High-level components depending on low-level
components decreases the capability of the high-
level components
Dependency Inversion Principle
Separated interface pattern
Program to interfaces
Dependency inversion is the very heart of
framework design and loosely coupled design
SOLID Principles
Promotes Object Oriented Design and Development
S
O
L
I
D
Single Responsibility Principle
Open/Closed Principle
Liskov Substitution Principle
Interface Segregation Principle
Dependency Inversion Principle
L22 Design Principles

More Related Content

Viewers also liked

IWMW 2001: Practical Web Strategies: Conflict, Ethics and Your Web Site (5)
IWMW 2001: Practical Web Strategies: Conflict, Ethics and Your Web Site (5)IWMW 2001: Practical Web Strategies: Conflict, Ethics and Your Web Site (5)
IWMW 2001: Practical Web Strategies: Conflict, Ethics and Your Web Site (5)IWMW
 
How To Build A Perfect Presentation
How To Build A Perfect PresentationHow To Build A Perfect Presentation
How To Build A Perfect PresentationOur Other Office
 
Доля карає жадібних. Вадим Скомаровський “ Чому в морі вода солона”.
Доля карає жадібних. Вадим Скомаровський “ Чому  в морі вода  солона”.Доля карає жадібних. Вадим Скомаровський “ Чому  в морі вода  солона”.
Доля карає жадібних. Вадим Скомаровський “ Чому в морі вода солона”.Irennn12
 
IWMW 1999: SMIL and the world smiles with you
IWMW 1999: SMIL and the world smiles with youIWMW 1999: SMIL and the world smiles with you
IWMW 1999: SMIL and the world smiles with youIWMW
 
Kelly Services - India
Kelly Services - IndiaKelly Services - India
Kelly Services - IndiaJubie Joseph
 
Трикутники в житті людини
Трикутники в житті людиниТрикутники в житті людини
Трикутники в житті людиниKate Storochenko
 
L01 New Technology 2017 Course Description
L01 New Technology 2017 Course DescriptionL01 New Technology 2017 Course Description
L01 New Technology 2017 Course DescriptionÓlafur Andri Ragnarsson
 

Viewers also liked (11)

IWMW 2001: Practical Web Strategies: Conflict, Ethics and Your Web Site (5)
IWMW 2001: Practical Web Strategies: Conflict, Ethics and Your Web Site (5)IWMW 2001: Practical Web Strategies: Conflict, Ethics and Your Web Site (5)
IWMW 2001: Practical Web Strategies: Conflict, Ethics and Your Web Site (5)
 
How To Build A Perfect Presentation
How To Build A Perfect PresentationHow To Build A Perfect Presentation
How To Build A Perfect Presentation
 
Berouw
BerouwBerouw
Berouw
 
Доля карає жадібних. Вадим Скомаровський “ Чому в морі вода солона”.
Доля карає жадібних. Вадим Скомаровський “ Чому  в морі вода  солона”.Доля карає жадібних. Вадим Скомаровський “ Чому  в морі вода  солона”.
Доля карає жадібних. Вадим Скомаровський “ Чому в морі вода солона”.
 
IWMW 1999: SMIL and the world smiles with you
IWMW 1999: SMIL and the world smiles with youIWMW 1999: SMIL and the world smiles with you
IWMW 1999: SMIL and the world smiles with you
 
Bahrain
BahrainBahrain
Bahrain
 
Kelly Services - India
Kelly Services - IndiaKelly Services - India
Kelly Services - India
 
Трикутники в житті людини
Трикутники в житті людиниТрикутники в житті людини
Трикутники в житті людини
 
Puentes definicion terminos
Puentes definicion terminosPuentes definicion terminos
Puentes definicion terminos
 
L01 New Technology 2017 Course Description
L01 New Technology 2017 Course DescriptionL01 New Technology 2017 Course Description
L01 New Technology 2017 Course Description
 
Harish_Resume
Harish_ResumeHarish_Resume
Harish_Resume
 

Similar to L22 Design Principles

principles of object oriented class design
principles of object oriented class designprinciples of object oriented class design
principles of object oriented class designNeetu Mishra
 
Object Oriented Concepts and Principles
Object Oriented Concepts and PrinciplesObject Oriented Concepts and Principles
Object Oriented Concepts and Principlesdeonpmeyer
 
Design principles - SOLID
Design principles - SOLIDDesign principles - SOLID
Design principles - SOLIDPranalee Rokde
 
SOLID Design Principles
SOLID Design PrinciplesSOLID Design Principles
SOLID Design PrinciplesAndreas Enbohm
 
Object Oriented Principle’s
Object Oriented Principle’sObject Oriented Principle’s
Object Oriented Principle’svivek p s
 
Object-oriented design principles
Object-oriented design principlesObject-oriented design principles
Object-oriented design principlesXiaoyan Chen
 
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
 
DesignPrinciples-and-DesignPatterns
DesignPrinciples-and-DesignPatternsDesignPrinciples-and-DesignPatterns
DesignPrinciples-and-DesignPatternsBasavaraj Patil
 
Solid Principles
Solid PrinciplesSolid Principles
Solid PrinciplesHitheshh
 
2009 Dotnet Information Day: More effective c#
2009 Dotnet Information Day: More effective c#2009 Dotnet Information Day: More effective c#
2009 Dotnet Information Day: More effective c#Daniel Fisher
 
Software design principles - jinal desai
Software design principles - jinal desaiSoftware design principles - jinal desai
Software design principles - jinal desaijinaldesailive
 
An Introduction to the SOLID Principles
An Introduction to the SOLID PrinciplesAn Introduction to the SOLID Principles
An Introduction to the SOLID PrinciplesAttila Bertók
 
Design poo my_jug_en_ppt
Design poo my_jug_en_pptDesign poo my_jug_en_ppt
Design poo my_jug_en_pptagnes_crepet
 
Inversion of Control
Inversion of ControlInversion of Control
Inversion of ControlShuhab Tariq
 
Design principle vs design patterns
Design principle vs design patternsDesign principle vs design patterns
Design principle vs design patternsPrabhakar Sharma
 

Similar to L22 Design Principles (20)

principles of object oriented class design
principles of object oriented class designprinciples of object oriented class design
principles of object oriented class design
 
Object Oriented Concepts and Principles
Object Oriented Concepts and PrinciplesObject Oriented Concepts and Principles
Object Oriented Concepts and Principles
 
SOLID
SOLIDSOLID
SOLID
 
SOLID
 SOLID SOLID
SOLID
 
Design principles - SOLID
Design principles - SOLIDDesign principles - SOLID
Design principles - SOLID
 
Oop principles
Oop principlesOop principles
Oop principles
 
SOLID Design Principles
SOLID Design PrinciplesSOLID Design Principles
SOLID Design Principles
 
Object Oriented Principle’s
Object Oriented Principle’sObject Oriented Principle’s
Object Oriented Principle’s
 
Design for Testability
Design for TestabilityDesign for Testability
Design for Testability
 
Object-oriented design principles
Object-oriented design principlesObject-oriented design principles
Object-oriented design principles
 
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
 
DesignPrinciples-and-DesignPatterns
DesignPrinciples-and-DesignPatternsDesignPrinciples-and-DesignPatterns
DesignPrinciples-and-DesignPatterns
 
Solid Principles
Solid PrinciplesSolid Principles
Solid Principles
 
2009 Dotnet Information Day: More effective c#
2009 Dotnet Information Day: More effective c#2009 Dotnet Information Day: More effective c#
2009 Dotnet Information Day: More effective c#
 
Software design principles - jinal desai
Software design principles - jinal desaiSoftware design principles - jinal desai
Software design principles - jinal desai
 
L03 Software Design
L03 Software DesignL03 Software Design
L03 Software Design
 
An Introduction to the SOLID Principles
An Introduction to the SOLID PrinciplesAn Introduction to the SOLID Principles
An Introduction to the SOLID Principles
 
Design poo my_jug_en_ppt
Design poo my_jug_en_pptDesign poo my_jug_en_ppt
Design poo my_jug_en_ppt
 
Inversion of Control
Inversion of ControlInversion of Control
Inversion of Control
 
Design principle vs design patterns
Design principle vs design patternsDesign principle vs design patterns
Design principle vs design patterns
 

More from Ólafur Andri Ragnarsson

New Technology Summer 2020 Course Introduction
New Technology Summer 2020 Course IntroductionNew Technology Summer 2020 Course Introduction
New Technology Summer 2020 Course IntroductionÓlafur Andri Ragnarsson
 
New Technology 2019 L13 Rise of the Machine
New Technology 2019 L13 Rise of the Machine New Technology 2019 L13 Rise of the Machine
New Technology 2019 L13 Rise of the Machine Ólafur Andri Ragnarsson
 

More from Ólafur Andri Ragnarsson (20)

Nýsköpun - Leiðin til framfara
Nýsköpun - Leiðin til framfaraNýsköpun - Leiðin til framfara
Nýsköpun - Leiðin til framfara
 
Nýjast tækni og framtíðin
Nýjast tækni og framtíðinNýjast tækni og framtíðin
Nýjast tækni og framtíðin
 
New Technology Summer 2020 Course Introduction
New Technology Summer 2020 Course IntroductionNew Technology Summer 2020 Course Introduction
New Technology Summer 2020 Course Introduction
 
L01 Introduction
L01 IntroductionL01 Introduction
L01 Introduction
 
L23 Robotics and Drones
L23 Robotics and Drones L23 Robotics and Drones
L23 Robotics and Drones
 
L22 Augmented and Virtual Reality
L22 Augmented and Virtual RealityL22 Augmented and Virtual Reality
L22 Augmented and Virtual Reality
 
L20 Personalised World
L20 Personalised WorldL20 Personalised World
L20 Personalised World
 
L19 Network Platforms
L19 Network PlatformsL19 Network Platforms
L19 Network Platforms
 
L18 Big Data and Analytics
L18 Big Data and AnalyticsL18 Big Data and Analytics
L18 Big Data and Analytics
 
L17 Algorithms and AI
L17 Algorithms and AIL17 Algorithms and AI
L17 Algorithms and AI
 
L16 Internet of Things
L16 Internet of ThingsL16 Internet of Things
L16 Internet of Things
 
L14 From the Internet to Blockchain
L14 From the Internet to BlockchainL14 From the Internet to Blockchain
L14 From the Internet to Blockchain
 
L14 The Mobile Revolution
L14 The Mobile RevolutionL14 The Mobile Revolution
L14 The Mobile Revolution
 
New Technology 2019 L13 Rise of the Machine
New Technology 2019 L13 Rise of the Machine New Technology 2019 L13 Rise of the Machine
New Technology 2019 L13 Rise of the Machine
 
L12 digital transformation
L12 digital transformationL12 digital transformation
L12 digital transformation
 
L10 The Innovator's Dilemma
L10 The Innovator's DilemmaL10 The Innovator's Dilemma
L10 The Innovator's Dilemma
 
L09 Disruptive Technology
L09 Disruptive TechnologyL09 Disruptive Technology
L09 Disruptive Technology
 
L09 Technological Revolutions
L09 Technological RevolutionsL09 Technological Revolutions
L09 Technological Revolutions
 
L07 Becoming Invisible
L07 Becoming InvisibleL07 Becoming Invisible
L07 Becoming Invisible
 
L06 Diffusion of Innovation
L06 Diffusion of InnovationL06 Diffusion of Innovation
L06 Diffusion of Innovation
 

Recently uploaded

ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...Christina Lin
 
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
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideChristina Lin
 
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
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantAxelRicardoTrocheRiq
 
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
 
buds n tech IT solutions
buds n  tech IT                solutionsbuds n  tech IT                solutions
buds n tech IT solutionsmonugehlot87
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...gurkirankumar98700
 
What are the features of Vehicle Tracking System?
What are the features of Vehicle Tracking System?What are the features of Vehicle Tracking System?
What are the features of Vehicle Tracking System?Watsoo Telematics
 
cybersecurity notes for mca students for learning
cybersecurity notes for mca students for learningcybersecurity notes for mca students for learning
cybersecurity notes for mca students for learningVitsRangannavar
 
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
 
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
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataBradBedford3
 
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
 
Engage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyEngage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyFrank van der Linden
 
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
 
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
 
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.
 
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.
 

Recently uploaded (20)

ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
 
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
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
 
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)
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service Consultant
 
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
 
buds n tech IT solutions
buds n  tech IT                solutionsbuds n  tech IT                solutions
buds n tech IT solutions
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
 
What are the features of Vehicle Tracking System?
What are the features of Vehicle Tracking System?What are the features of Vehicle Tracking System?
What are the features of Vehicle Tracking System?
 
cybersecurity notes for mca students for learning
cybersecurity notes for mca students for learningcybersecurity notes for mca students for learning
cybersecurity notes for mca students for learning
 
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
 
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...
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
 
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
 
Engage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyEngage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The Ugly
 
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...
 
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
 
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
 
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 ...
 

L22 Design Principles

  • 1. HÖNNUN OG SMÍÐI HUGBÚNAÐAR 2015 L22 DESIGN PRINCIPLES
  • 2. Agenda ▪ Why Principles? ▪ SOLID Principle – 1. Single Responsibility Principle – 2. Open/Closed Principle – 3. Liskov Substitution Principle – 4. Interface Segregation Principle – 5. Dependency Inversion Principle
  • 3. Reading ▪ SOLID - the first five principles of Object Oriented Deign by Samuel Oloruntoba
  • 4. Big Ball of Mud Is this your code?
  • 5. Technical Dept Referring to the eventual consequences of poor system design Hacks, fix it later, mistakes etc
  • 6.
  • 7. Why Principles? Based on knowledge and research Encourage writing high quality code Promote better communication
  • 8. Why Principles? Should not be taken as dogmatic All design decision have context and specific set of problems to solve
  • 9. SOLID Principles Promotes Object Oriented Design and Development S O L I D Single Responsibility Principle Open/Closed Principle Liskov Substitution Principle Interface Segregation Principle Dependency Inversion Principle
  • 10. Single Responsibility Principle (SRP) A class should have one and only one reason to change, meaning that a class should have only one job Violating this principle leads to low cohesion and make the code brittle and loads to code bloat and confusion It is not easy to spot
  • 11. Single Responsibility Principle Cohesion refers to the degree to which the elements of a module belong together High cohesion: methods that serve the given class tend to be similar in many aspects Increased understanding of modules Increased ease in maintaining a system Increased ease in reusing a module
  • 12. Single Responsibility Principle Example: Rectangle has two responsibilities
  • 13. The Open-Closed Principle (OCP) Adding new functionality would involve minimal changes to existing code Objects or entities should be open for extension, but closed for modification.
  • 14. The Open-Closed Principle (OCP) Most changes will be handled as new methods and new classes Designs following this principle would result in resilient code which does not break on addition of new functionality
  • 15. public class ResourceAllocator { ... public int allocate(intresourceType) { intresourceId; switch (resourceType) { case TIME_SLOT: resourceId = findFreeTimeSlot(); markTimeslotBusy(resourceId); break; case SPACE_SLOT: resourceId = findFreeSpaceSlot(); markSpaceSlotBusy(resourceId); break; ... } return resourceId; } ... Resource Allocator Example
  • 16. Resource Allocator Example List resources = new ArrayList(); ... public int allocate(intresourceType) { int resourceId = findFreeResource(resourceType); markAsBusy(resourceId); return resourceId; } Designed for extension
  • 17. The Liskov Substitution Principle (LSP) Let q(x) be a property provable about objects of x of type T. Then q(y) should be provable for objects y of type S where S is a subtype of T. All code operating with reference to the base class should be completely transparent to the type of the inherited object
  • 18. The Liskov Substitution Principle (LSP) It should be possible to substitute an object of one type with another within the same class hierarchy Inheriting classes should not perform any actions that will invalidate the assumptions made by the base class
  • 19. LSP Example public class Rectangle { protected int _width; protected int _height; public int getWidth() { return _width; } public int getHeight() { return _height; } public void setWidth(int width) { _width = width; } public void setHeight(int height) { _height = height; } }
  • 20. LSP Example public class Square extends Rectangle { public void setWidth(int width) { _width = width; _height = width; } public void setHeight(int height) { _height = height; _width = _height; } } Implementation convenience
  • 21. LSP Example import junit.framework.Assert; import org.junit.Test; public class RectangleTests { @Test public void areaOfRectangle() { Rectangle r = new Square(); r.setWidth(5); r.setHeight(2); // Will Fail - r is a square and sets // width and height equal to each other. Assert.assertEquals(r.getWidth() * r.getHeight(),10); } }
  • 22. Interface Segregation Principle (ISP) A client should never be forced to implement an interface that it doesn’t use or clients shouldn’t be forced to depend on methods they do not use. Split large interfaces into smaller and more specific interfaces
  • 23. Interface Segregation Principle (ISP) The smaller the interface, the better For fat or polluted interfaces clients are forced to depend on methods they do not use
  • 24.
  • 25. Dependency Inversion Principle (DIP) Entities must depend on abstractions not on concretions. It states that the high level module must not depend on the low level module, but they should depend on abstractions. Low-level components should depend on high-level, not vice versa.
  • 26. Dependency Inversion Principle (DIP) The high-level components should be the one defining logic and enforcing change High-level components depending on low-level components decreases the capability of the high- level components
  • 27. Dependency Inversion Principle Separated interface pattern Program to interfaces Dependency inversion is the very heart of framework design and loosely coupled design
  • 28.
  • 29. SOLID Principles Promotes Object Oriented Design and Development S O L I D Single Responsibility Principle Open/Closed Principle Liskov Substitution Principle Interface Segregation Principle Dependency Inversion Principle