SlideShare una empresa de Scribd logo
1 de 8
A ‘SOLID’ Primer By KristofferRoupé Jun 2009
SingleResponsibilityPrinciple THERE SHOULD NEVER BE MORE THAN ONE REASON FOR A CLASS TO CHANGE. In the example the rectangle has 2 responsibilitiesand therefore it has 2 reasons to change. ,[object Object]
If the AreaComputer changes the way areas are computed, so does the Rectangle.  7 class Rectangle  8   area  9   draw 10 end 11  12 class Renderer 13   def render rectangle 14      rectangle.draw 15   end 16 end 17  18 class AreaComputer 19   def compute rectangle 20      rectangle.area 21   end 22 end
Open/ClosedPrinciple SOFTWARE ENTITIES (CLASSES, MODULES, FUNCTIONS, ETC.) SHOULD BE OPEN FOR EXTENSION BUT CLOSED FOR MODIFICATION.  It says that you should design modules that never change.  When requirements change, you extend the behavior of such modules by adding new code, not by changing old code that already works.  34 module Drawable 35   def draw  36   end 37 end 38  39 class Square 40   include Drawable 41 end 42  43 class Circle 44   include Drawable 45 end 46  47 def draw_all_shapes(shapes) 48   shapes.each { |shape| shape.draw } 49 end
Liskov Substitution Principle FUNCTIONS THAT USE POINTERS OR REFERENCES TO BASE CLASSES MUST BE ABLE TO USE OBJECTS OF DERIVED CLASSES WITHOUT KNOWING IT  56# simple violation of LSP 57 def draw_shape(shape) 58   shape.draw_circleif shape.method_defined? 'draw_cicle' 59   shape.draw_rectangleif shape.method_defined? 'draw_rectangle'  60 end From this code it is quite obvious that the function has to know what kind of shape it is drawing. (Notice the ugly conditional statements)
Liskov Substitution Principle cond. A little more subtle violation of LSP.  You would expect that a rectangle should act as the methods and properties given, even if it is a derived class.  In this case the test would fail, hence breaking the contract of the parent.  62 # more subtle violation 63 class Rectangle 64   def height value 65     @height = value 66   end 67   def width value 68     @width = value 69   end 70 end 71  72 class Square < Rectangle 73   def height value 74     @heigth = @width = value 75   end 76   def width value 77     height = value 78   end 79 end 80  81 describe Square do 82   it "should not violate the contract of it's parent" do 83     rectangle = Square.new 84     rectangle.height = 4 85     rectangle.width = 2 86     area = rectangle.height * rectangle.width 87     area.should be 8 88   end 89 end
Interface Segregation Principle CLIENTS SHOULD NOT BE FORCED TO DEPEND UPON INTERFACES THAT THEY DO NOT USE This principle deals with the disadvantages of “fat” interfaces. Classes that have “fat” interfaces are classes whose interfaces are not cohesive. In other words, the interfaces of the class can be broken up into groups of member functions  95 class Door 96   def open 97   end 98   def close 99   end100   def is_open?101   end102 end103 104 class TimedDoor < Door105   def door_time_out?106   end107 end108 109 class TimerClient110   def time_outid111   end  112 end113 114 class DoorTimerAdapter < TimerClient115   def initialize timed_door116     @its_timed_door = timed_door117   end118   def time_out id119     @its_timed_door.door_time_out id120   end121 end
Dependency Inversion Principle HIGH LEVEL MODULES SHOULD NEVER DEPEND UPON LOW LEVEL MODULES. BOTH SHOULD DEPEND ON ABSTRACTIONS ABSTRACTIONS SHOULD NEVER DEPEND UPON DETAILS. DETAILS SHOULD DEPEND UPON ABSTRACTIONS # A simple violation where button is directly dependent on lamp.129 class Lamp130   def turn_on!131   end132   def turn_off!133   end134 end135 136 class Button137   def initialize lamp138     @its_lamp = lamp139   end140   def detect!141     button_on = get_state142     @its_lamp.turn_on! if button_on143     @its_lamp.turn_off! unless button_on144   end145 end

Más contenido relacionado

Destacado

Коледен карнавал
Коледен карнавалКоледен карнавал
Коледен карнавалIskra Petkova
 
The world without the literal notation for floating-point numbers
The world without the literal notation for floating-point numbersThe world without the literal notation for floating-point numbers
The world without the literal notation for floating-point numbersKenta Murata
 
Lights, Camera, Action: producing digital video
Lights, Camera, Action: producing digital videoLights, Camera, Action: producing digital video
Lights, Camera, Action: producing digital videoChris Willmott
 
iOS design patterns: blocks
iOS design patterns: blocksiOS design patterns: blocks
iOS design patterns: blocksAlessio Roberto
 
Beginningi os part1-bobmccune
Beginningi os part1-bobmccuneBeginningi os part1-bobmccune
Beginningi os part1-bobmccuneMobile March
 
The Future is Not Out of Reach: Trends & Transformations
The Future is Not Out of Reach: Trends & TransformationsThe Future is Not Out of Reach: Trends & Transformations
The Future is Not Out of Reach: Trends & TransformationsDavid King
 
Practical, Pluggable Types
Practical, Pluggable TypesPractical, Pluggable Types
Practical, Pluggable TypesMarcus Denker
 
Freak Out, Geek Out, or Seek Out: Trends, Transformation & Change in Libraries
Freak Out, Geek Out, or Seek Out: Trends, Transformation & Change in LibrariesFreak Out, Geek Out, or Seek Out: Trends, Transformation & Change in Libraries
Freak Out, Geek Out, or Seek Out: Trends, Transformation & Change in LibrariesDavid King
 

Destacado (9)

Коледен карнавал
Коледен карнавалКоледен карнавал
Коледен карнавал
 
Animals
AnimalsAnimals
Animals
 
The world without the literal notation for floating-point numbers
The world without the literal notation for floating-point numbersThe world without the literal notation for floating-point numbers
The world without the literal notation for floating-point numbers
 
Lights, Camera, Action: producing digital video
Lights, Camera, Action: producing digital videoLights, Camera, Action: producing digital video
Lights, Camera, Action: producing digital video
 
iOS design patterns: blocks
iOS design patterns: blocksiOS design patterns: blocks
iOS design patterns: blocks
 
Beginningi os part1-bobmccune
Beginningi os part1-bobmccuneBeginningi os part1-bobmccune
Beginningi os part1-bobmccune
 
The Future is Not Out of Reach: Trends & Transformations
The Future is Not Out of Reach: Trends & TransformationsThe Future is Not Out of Reach: Trends & Transformations
The Future is Not Out of Reach: Trends & Transformations
 
Practical, Pluggable Types
Practical, Pluggable TypesPractical, Pluggable Types
Practical, Pluggable Types
 
Freak Out, Geek Out, or Seek Out: Trends, Transformation & Change in Libraries
Freak Out, Geek Out, or Seek Out: Trends, Transformation & Change in LibrariesFreak Out, Geek Out, or Seek Out: Trends, Transformation & Change in Libraries
Freak Out, Geek Out, or Seek Out: Trends, Transformation & Change in Libraries
 

Similar a A S.O.L.I.D. Primer

Programming for a better world
Programming for a better worldProgramming for a better world
Programming for a better worldjhansi reddy
 
Taking r to its limits. 70+ tips
Taking r to its limits. 70+ tipsTaking r to its limits. 70+ tips
Taking r to its limits. 70+ tipsIlya Shutov
 
Clean Code - Part 2
Clean Code - Part 2Clean Code - Part 2
Clean Code - Part 2Knoldus Inc.
 
Lecture 01 - Introduction and Review.ppt
Lecture 01 - Introduction and Review.pptLecture 01 - Introduction and Review.ppt
Lecture 01 - Introduction and Review.pptMaiGaafar
 
Object Oriented Principle’s
Object Oriented Principle’sObject Oriented Principle’s
Object Oriented Principle’svivek p s
 
Refactoring: Improving the design of existing code
Refactoring: Improving the design of existing codeRefactoring: Improving the design of existing code
Refactoring: Improving the design of existing codeKnoldus Inc.
 
Qtp classes-in-mumbai
Qtp classes-in-mumbaiQtp classes-in-mumbai
Qtp classes-in-mumbaivibrantuser
 
Write a C++ program to get input of a data set of 14 double valu.pdf
Write a C++ program to get input of a data set of 14 double valu.pdfWrite a C++ program to get input of a data set of 14 double valu.pdf
Write a C++ program to get input of a data set of 14 double valu.pdfarshiartpalace
 
Describe and evaluate a company’s pricing and retail strategy. Inc.docx
Describe and evaluate a company’s pricing and retail strategy. Inc.docxDescribe and evaluate a company’s pricing and retail strategy. Inc.docx
Describe and evaluate a company’s pricing and retail strategy. Inc.docxtheodorelove43763
 
SOLID - Principles of Object Oriented Design
SOLID - Principles of Object Oriented DesignSOLID - Principles of Object Oriented Design
SOLID - Principles of Object Oriented DesignRiccardo Cardin
 
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
 
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
 
Linear programming models - U2.pptx
Linear programming models - U2.pptxLinear programming models - U2.pptx
Linear programming models - U2.pptxMariaBurgos55
 
principles of object oriented class design
principles of object oriented class designprinciples of object oriented class design
principles of object oriented class designNeetu Mishra
 
CIS 1403 lab 3 functions and methods in Java
CIS 1403 lab 3 functions and methods in JavaCIS 1403 lab 3 functions and methods in Java
CIS 1403 lab 3 functions and methods in JavaHamad Odhabi
 

Similar a A S.O.L.I.D. Primer (20)

Programming for a better world
Programming for a better worldProgramming for a better world
Programming for a better world
 
JDK and AWT
JDK and AWTJDK and AWT
JDK and AWT
 
Taking r to its limits. 70+ tips
Taking r to its limits. 70+ tipsTaking r to its limits. 70+ tips
Taking r to its limits. 70+ tips
 
Clean Code - Part 2
Clean Code - Part 2Clean Code - Part 2
Clean Code - Part 2
 
Lecture 01 - Introduction and Review.ppt
Lecture 01 - Introduction and Review.pptLecture 01 - Introduction and Review.ppt
Lecture 01 - Introduction and Review.ppt
 
Object Oriented Principle’s
Object Oriented Principle’sObject Oriented Principle’s
Object Oriented Principle’s
 
Refactoring: Improving the design of existing code
Refactoring: Improving the design of existing codeRefactoring: Improving the design of existing code
Refactoring: Improving the design of existing code
 
Qtp classes-in-mumbai
Qtp classes-in-mumbaiQtp classes-in-mumbai
Qtp classes-in-mumbai
 
Write a C++ program to get input of a data set of 14 double valu.pdf
Write a C++ program to get input of a data set of 14 double valu.pdfWrite a C++ program to get input of a data set of 14 double valu.pdf
Write a C++ program to get input of a data set of 14 double valu.pdf
 
Describe and evaluate a company’s pricing and retail strategy. Inc.docx
Describe and evaluate a company’s pricing and retail strategy. Inc.docxDescribe and evaluate a company’s pricing and retail strategy. Inc.docx
Describe and evaluate a company’s pricing and retail strategy. Inc.docx
 
Bound and Checked
Bound and CheckedBound and Checked
Bound and Checked
 
SOLID - Principles of Object Oriented Design
SOLID - Principles of Object Oriented DesignSOLID - Principles of Object Oriented Design
SOLID - Principles of Object Oriented Design
 
Design p atterns
Design p atternsDesign p atterns
Design p atterns
 
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
 
An Introduction to the SOLID Principles
An Introduction to the SOLID PrinciplesAn Introduction to the SOLID Principles
An Introduction to the SOLID Principles
 
Linear programming models - U2.pptx
Linear programming models - U2.pptxLinear programming models - U2.pptx
Linear programming models - U2.pptx
 
principles of object oriented class design
principles of object oriented class designprinciples of object oriented class design
principles of object oriented class design
 
MA3696 Lecture 9
MA3696 Lecture 9MA3696 Lecture 9
MA3696 Lecture 9
 
CIS 1403 lab 3 functions and methods in Java
CIS 1403 lab 3 functions and methods in JavaCIS 1403 lab 3 functions and methods in Java
CIS 1403 lab 3 functions and methods in Java
 
SOLID
 SOLID SOLID
SOLID
 

Último

AI presentation and introduction - Retrieval Augmented Generation RAG 101
AI presentation and introduction - Retrieval Augmented Generation RAG 101AI presentation and introduction - Retrieval Augmented Generation RAG 101
AI presentation and introduction - Retrieval Augmented Generation RAG 101vincent683379
 
Microsoft CSP Briefing Pre-Engagement - Questionnaire
Microsoft CSP Briefing Pre-Engagement - QuestionnaireMicrosoft CSP Briefing Pre-Engagement - Questionnaire
Microsoft CSP Briefing Pre-Engagement - QuestionnaireExakis Nelite
 
1111 ChatGPT Prompts PDF Free Download - Prompts for ChatGPT
1111 ChatGPT Prompts PDF Free Download - Prompts for ChatGPT1111 ChatGPT Prompts PDF Free Download - Prompts for ChatGPT
1111 ChatGPT Prompts PDF Free Download - Prompts for ChatGPTiSEO AI
 
Breaking Down the Flutterwave Scandal What You Need to Know.pdf
Breaking Down the Flutterwave Scandal What You Need to Know.pdfBreaking Down the Flutterwave Scandal What You Need to Know.pdf
Breaking Down the Flutterwave Scandal What You Need to Know.pdfUK Journal
 
How Red Hat Uses FDO in Device Lifecycle _ Costin and Vitaliy at Red Hat.pdf
How Red Hat Uses FDO in Device Lifecycle _ Costin and Vitaliy at Red Hat.pdfHow Red Hat Uses FDO in Device Lifecycle _ Costin and Vitaliy at Red Hat.pdf
How Red Hat Uses FDO in Device Lifecycle _ Costin and Vitaliy at Red Hat.pdfFIDO Alliance
 
TEST BANK For, Information Technology Project Management 9th Edition Kathy Sc...
TEST BANK For, Information Technology Project Management 9th Edition Kathy Sc...TEST BANK For, Information Technology Project Management 9th Edition Kathy Sc...
TEST BANK For, Information Technology Project Management 9th Edition Kathy Sc...marcuskenyatta275
 
The Metaverse: Are We There Yet?
The  Metaverse:    Are   We  There  Yet?The  Metaverse:    Are   We  There  Yet?
The Metaverse: Are We There Yet?Mark Billinghurst
 
Behind the Scenes From the Manager's Chair: Decoding the Secrets of Successfu...
Behind the Scenes From the Manager's Chair: Decoding the Secrets of Successfu...Behind the Scenes From the Manager's Chair: Decoding the Secrets of Successfu...
Behind the Scenes From the Manager's Chair: Decoding the Secrets of Successfu...CzechDreamin
 
Salesforce Adoption – Metrics, Methods, and Motivation, Antone Kom
Salesforce Adoption – Metrics, Methods, and Motivation, Antone KomSalesforce Adoption – Metrics, Methods, and Motivation, Antone Kom
Salesforce Adoption – Metrics, Methods, and Motivation, Antone KomCzechDreamin
 
Free and Effective: Making Flows Publicly Accessible, Yumi Ibrahimzade
Free and Effective: Making Flows Publicly Accessible, Yumi IbrahimzadeFree and Effective: Making Flows Publicly Accessible, Yumi Ibrahimzade
Free and Effective: Making Flows Publicly Accessible, Yumi IbrahimzadeCzechDreamin
 
Designing for Hardware Accessibility at Comcast
Designing for Hardware Accessibility at ComcastDesigning for Hardware Accessibility at Comcast
Designing for Hardware Accessibility at ComcastUXDXConf
 
Introduction to FDO and How It works Applications _ Richard at FIDO Alliance.pdf
Introduction to FDO and How It works Applications _ Richard at FIDO Alliance.pdfIntroduction to FDO and How It works Applications _ Richard at FIDO Alliance.pdf
Introduction to FDO and How It works Applications _ Richard at FIDO Alliance.pdfFIDO Alliance
 
AI revolution and Salesforce, Jiří Karpíšek
AI revolution and Salesforce, Jiří KarpíšekAI revolution and Salesforce, Jiří Karpíšek
AI revolution and Salesforce, Jiří KarpíšekCzechDreamin
 
Google I/O Extended 2024 Warsaw
Google I/O Extended 2024 WarsawGoogle I/O Extended 2024 Warsaw
Google I/O Extended 2024 WarsawGDSC PJATK
 
What's New in Teams Calling, Meetings and Devices April 2024
What's New in Teams Calling, Meetings and Devices April 2024What's New in Teams Calling, Meetings and Devices April 2024
What's New in Teams Calling, Meetings and Devices April 2024Stephanie Beckett
 
Speed Wins: From Kafka to APIs in Minutes
Speed Wins: From Kafka to APIs in MinutesSpeed Wins: From Kafka to APIs in Minutes
Speed Wins: From Kafka to APIs in Minutesconfluent
 
Optimizing NoSQL Performance Through Observability
Optimizing NoSQL Performance Through ObservabilityOptimizing NoSQL Performance Through Observability
Optimizing NoSQL Performance Through ObservabilityScyllaDB
 
The Value of Certifying Products for FDO _ Paul at FIDO Alliance.pdf
The Value of Certifying Products for FDO _ Paul at FIDO Alliance.pdfThe Value of Certifying Products for FDO _ Paul at FIDO Alliance.pdf
The Value of Certifying Products for FDO _ Paul at FIDO Alliance.pdfFIDO Alliance
 
Oauth 2.0 Introduction and Flows with MuleSoft
Oauth 2.0 Introduction and Flows with MuleSoftOauth 2.0 Introduction and Flows with MuleSoft
Oauth 2.0 Introduction and Flows with MuleSoftshyamraj55
 
Integrating Telephony Systems with Salesforce: Insights and Considerations, B...
Integrating Telephony Systems with Salesforce: Insights and Considerations, B...Integrating Telephony Systems with Salesforce: Insights and Considerations, B...
Integrating Telephony Systems with Salesforce: Insights and Considerations, B...CzechDreamin
 

Último (20)

AI presentation and introduction - Retrieval Augmented Generation RAG 101
AI presentation and introduction - Retrieval Augmented Generation RAG 101AI presentation and introduction - Retrieval Augmented Generation RAG 101
AI presentation and introduction - Retrieval Augmented Generation RAG 101
 
Microsoft CSP Briefing Pre-Engagement - Questionnaire
Microsoft CSP Briefing Pre-Engagement - QuestionnaireMicrosoft CSP Briefing Pre-Engagement - Questionnaire
Microsoft CSP Briefing Pre-Engagement - Questionnaire
 
1111 ChatGPT Prompts PDF Free Download - Prompts for ChatGPT
1111 ChatGPT Prompts PDF Free Download - Prompts for ChatGPT1111 ChatGPT Prompts PDF Free Download - Prompts for ChatGPT
1111 ChatGPT Prompts PDF Free Download - Prompts for ChatGPT
 
Breaking Down the Flutterwave Scandal What You Need to Know.pdf
Breaking Down the Flutterwave Scandal What You Need to Know.pdfBreaking Down the Flutterwave Scandal What You Need to Know.pdf
Breaking Down the Flutterwave Scandal What You Need to Know.pdf
 
How Red Hat Uses FDO in Device Lifecycle _ Costin and Vitaliy at Red Hat.pdf
How Red Hat Uses FDO in Device Lifecycle _ Costin and Vitaliy at Red Hat.pdfHow Red Hat Uses FDO in Device Lifecycle _ Costin and Vitaliy at Red Hat.pdf
How Red Hat Uses FDO in Device Lifecycle _ Costin and Vitaliy at Red Hat.pdf
 
TEST BANK For, Information Technology Project Management 9th Edition Kathy Sc...
TEST BANK For, Information Technology Project Management 9th Edition Kathy Sc...TEST BANK For, Information Technology Project Management 9th Edition Kathy Sc...
TEST BANK For, Information Technology Project Management 9th Edition Kathy Sc...
 
The Metaverse: Are We There Yet?
The  Metaverse:    Are   We  There  Yet?The  Metaverse:    Are   We  There  Yet?
The Metaverse: Are We There Yet?
 
Behind the Scenes From the Manager's Chair: Decoding the Secrets of Successfu...
Behind the Scenes From the Manager's Chair: Decoding the Secrets of Successfu...Behind the Scenes From the Manager's Chair: Decoding the Secrets of Successfu...
Behind the Scenes From the Manager's Chair: Decoding the Secrets of Successfu...
 
Salesforce Adoption – Metrics, Methods, and Motivation, Antone Kom
Salesforce Adoption – Metrics, Methods, and Motivation, Antone KomSalesforce Adoption – Metrics, Methods, and Motivation, Antone Kom
Salesforce Adoption – Metrics, Methods, and Motivation, Antone Kom
 
Free and Effective: Making Flows Publicly Accessible, Yumi Ibrahimzade
Free and Effective: Making Flows Publicly Accessible, Yumi IbrahimzadeFree and Effective: Making Flows Publicly Accessible, Yumi Ibrahimzade
Free and Effective: Making Flows Publicly Accessible, Yumi Ibrahimzade
 
Designing for Hardware Accessibility at Comcast
Designing for Hardware Accessibility at ComcastDesigning for Hardware Accessibility at Comcast
Designing for Hardware Accessibility at Comcast
 
Introduction to FDO and How It works Applications _ Richard at FIDO Alliance.pdf
Introduction to FDO and How It works Applications _ Richard at FIDO Alliance.pdfIntroduction to FDO and How It works Applications _ Richard at FIDO Alliance.pdf
Introduction to FDO and How It works Applications _ Richard at FIDO Alliance.pdf
 
AI revolution and Salesforce, Jiří Karpíšek
AI revolution and Salesforce, Jiří KarpíšekAI revolution and Salesforce, Jiří Karpíšek
AI revolution and Salesforce, Jiří Karpíšek
 
Google I/O Extended 2024 Warsaw
Google I/O Extended 2024 WarsawGoogle I/O Extended 2024 Warsaw
Google I/O Extended 2024 Warsaw
 
What's New in Teams Calling, Meetings and Devices April 2024
What's New in Teams Calling, Meetings and Devices April 2024What's New in Teams Calling, Meetings and Devices April 2024
What's New in Teams Calling, Meetings and Devices April 2024
 
Speed Wins: From Kafka to APIs in Minutes
Speed Wins: From Kafka to APIs in MinutesSpeed Wins: From Kafka to APIs in Minutes
Speed Wins: From Kafka to APIs in Minutes
 
Optimizing NoSQL Performance Through Observability
Optimizing NoSQL Performance Through ObservabilityOptimizing NoSQL Performance Through Observability
Optimizing NoSQL Performance Through Observability
 
The Value of Certifying Products for FDO _ Paul at FIDO Alliance.pdf
The Value of Certifying Products for FDO _ Paul at FIDO Alliance.pdfThe Value of Certifying Products for FDO _ Paul at FIDO Alliance.pdf
The Value of Certifying Products for FDO _ Paul at FIDO Alliance.pdf
 
Oauth 2.0 Introduction and Flows with MuleSoft
Oauth 2.0 Introduction and Flows with MuleSoftOauth 2.0 Introduction and Flows with MuleSoft
Oauth 2.0 Introduction and Flows with MuleSoft
 
Integrating Telephony Systems with Salesforce: Insights and Considerations, B...
Integrating Telephony Systems with Salesforce: Insights and Considerations, B...Integrating Telephony Systems with Salesforce: Insights and Considerations, B...
Integrating Telephony Systems with Salesforce: Insights and Considerations, B...
 

A S.O.L.I.D. Primer

  • 1. A ‘SOLID’ Primer By KristofferRoupé Jun 2009
  • 2.
  • 3. If the AreaComputer changes the way areas are computed, so does the Rectangle.  7 class Rectangle  8   area  9   draw 10 end 11  12 class Renderer 13   def render rectangle 14      rectangle.draw 15   end 16 end 17  18 class AreaComputer 19   def compute rectangle 20      rectangle.area 21   end 22 end
  • 4. Open/ClosedPrinciple SOFTWARE ENTITIES (CLASSES, MODULES, FUNCTIONS, ETC.) SHOULD BE OPEN FOR EXTENSION BUT CLOSED FOR MODIFICATION. It says that you should design modules that never change. When requirements change, you extend the behavior of such modules by adding new code, not by changing old code that already works.  34 module Drawable 35   def draw  36   end 37 end 38  39 class Square 40   include Drawable 41 end 42  43 class Circle 44   include Drawable 45 end 46  47 def draw_all_shapes(shapes) 48   shapes.each { |shape| shape.draw } 49 end
  • 5. Liskov Substitution Principle FUNCTIONS THAT USE POINTERS OR REFERENCES TO BASE CLASSES MUST BE ABLE TO USE OBJECTS OF DERIVED CLASSES WITHOUT KNOWING IT  56# simple violation of LSP 57 def draw_shape(shape) 58   shape.draw_circleif shape.method_defined? 'draw_cicle' 59   shape.draw_rectangleif shape.method_defined? 'draw_rectangle'  60 end From this code it is quite obvious that the function has to know what kind of shape it is drawing. (Notice the ugly conditional statements)
  • 6. Liskov Substitution Principle cond. A little more subtle violation of LSP. You would expect that a rectangle should act as the methods and properties given, even if it is a derived class. In this case the test would fail, hence breaking the contract of the parent.  62 # more subtle violation 63 class Rectangle 64   def height value 65     @height = value 66   end 67   def width value 68     @width = value 69   end 70 end 71  72 class Square < Rectangle 73   def height value 74     @heigth = @width = value 75   end 76   def width value 77     height = value 78   end 79 end 80  81 describe Square do 82   it "should not violate the contract of it's parent" do 83     rectangle = Square.new 84     rectangle.height = 4 85     rectangle.width = 2 86     area = rectangle.height * rectangle.width 87     area.should be 8 88   end 89 end
  • 7. Interface Segregation Principle CLIENTS SHOULD NOT BE FORCED TO DEPEND UPON INTERFACES THAT THEY DO NOT USE This principle deals with the disadvantages of “fat” interfaces. Classes that have “fat” interfaces are classes whose interfaces are not cohesive. In other words, the interfaces of the class can be broken up into groups of member functions  95 class Door 96   def open 97   end 98   def close 99   end100   def is_open?101   end102 end103 104 class TimedDoor < Door105   def door_time_out?106   end107 end108 109 class TimerClient110   def time_outid111   end  112 end113 114 class DoorTimerAdapter < TimerClient115   def initialize timed_door116     @its_timed_door = timed_door117   end118   def time_out id119     @its_timed_door.door_time_out id120   end121 end
  • 8. Dependency Inversion Principle HIGH LEVEL MODULES SHOULD NEVER DEPEND UPON LOW LEVEL MODULES. BOTH SHOULD DEPEND ON ABSTRACTIONS ABSTRACTIONS SHOULD NEVER DEPEND UPON DETAILS. DETAILS SHOULD DEPEND UPON ABSTRACTIONS # A simple violation where button is directly dependent on lamp.129 class Lamp130   def turn_on!131   end132   def turn_off!133   end134 end135 136 class Button137   def initialize lamp138     @its_lamp = lamp139   end140   def detect!141     button_on = get_state142     @its_lamp.turn_on! if button_on143     @its_lamp.turn_off! unless button_on144   end145 end
  • 9. Dependency Inversion Principle 146 # A solution to that problem...147 module ButtonClient148  def turn_on!149  def turn_off!150 end151 152 class Button153   def initialize button_client154     @its_client = button_client155   end156   def detect157     button_on = get_state158     @its_client.turn_on! if button_on159     @its_client.turn_off! unless button_on160   end161   def get_state162   end163 end164 165 class Lamp < ButtonClient166   def turn_on!167   end168   def turn_off!169   end170 end171 172 class ButtonImplementation < Button173   def initialize button_client174   end175   def get_state176   end177 end