<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Mario Sobhy</title>
    <description>The latest articles on DEV Community by Mario Sobhy (@mari0000).</description>
    <link>https://dev.to/mari0000</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F328664%2Fe2751313-1ac1-44b3-a746-5dfa872192f7.jpg</url>
      <title>DEV Community: Mario Sobhy</title>
      <link>https://dev.to/mari0000</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mari0000"/>
    <language>en</language>
    <item>
      <title>SOLID Principles Explained</title>
      <dc:creator>Mario Sobhy</dc:creator>
      <pubDate>Tue, 04 Apr 2023 12:56:57 +0000</pubDate>
      <link>https://dev.to/mari0000/solid-principles-explained-1063</link>
      <guid>https://dev.to/mari0000/solid-principles-explained-1063</guid>
      <description>&lt;p&gt;The SOLID principles are a set of guidelines for writing clean and maintainable object-oriented code. The acronym SOLID stands for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Single Responsibility Principle&lt;/li&gt;
&lt;li&gt;Open/Closed Principle&lt;/li&gt;
&lt;li&gt;Liskov Substitution Principle&lt;/li&gt;
&lt;li&gt;Interface Segregation Principle&lt;/li&gt;
&lt;li&gt;Dependency Inversion Principle&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I’ll explain each of these principles with examples in Ruby:&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Single Responsibility Principle (SRP)&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;The Single Responsibility Principle states that a class should have only one reason to change. In other words, a class should have only one responsibility.&lt;/p&gt;

&lt;p&gt;Let’s say we have a User class that handles both authentication and profile management:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class User
  def authenticate(username, password)
    # some functionality
  end

  def update_profile(attributes)
    # some functionality
  end
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This violates the SRP because the User class has two responsibilities: authentication and profile management. To follow the SRP, we should split this class into two:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Authenticator
  def authenticate(username, password)
    # some functionality
  end
end

class UserProfileManager
  def update_profile(attributes)
    # some functionality
  end
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;so now each class has only one responsibility.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Open/Closed Principle&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;The Open/Closed Principle (OCP) states that software entities (classes, modules, functions, etc.) should be open for extension but closed for modification. This means that you should be able to add new features to the software without changing the existing code.&lt;/p&gt;

&lt;p&gt;Here’s an example of how to apply the OCP in Ruby:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Bad example - not following OCP
class Car
  def initialize(make, model, year)
    @make = make
    @model = model
    @year = year
  end

  def start_engine
    puts "Starting engine for #{@make} #{@model}"
  end

  def shift_gears
    puts "Shifting gears for #{@make} #{@model}"
  end
end

class ElectricCar &amp;lt; Car
  def initialize(make, model, year)
    super(make, model, year)
  end

  def start_engine
    puts "Electric car does not have an engine."
  end
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, we have a Car class that has a &lt;code&gt;start_engine&lt;/code&gt; and &lt;code&gt;shift_gears&lt;/code&gt; method. We also have an &lt;code&gt;ElectricCar&lt;/code&gt; class that inherits from &lt;code&gt;Car&lt;/code&gt;. The problem is that the &lt;code&gt;start_engine&lt;/code&gt; method is not applicable to an electric car, as they don't have an engine. So, we need to modify the &lt;code&gt;Car&lt;/code&gt; class to handle this new type of car.&lt;/p&gt;

&lt;p&gt;Let’s refactor this code to follow the OCP:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Good example - following OCP
class Car
  def initialize(make, model, year)
    @make = make
    @model = model
    @year = year
  end

  def shift_gears
    puts "Shifting gears for #{@make} #{@model}"
  end
end

class ElectricCar &amp;lt; Car
  def initialize(make, model, year)
    super(make, model, year)
  end

  def start_engine
    puts "Electric car does not have an engine."
  end
end

class GasCar &amp;lt; Car
  def initialize(make, model, year)
    super(make, model, year)
  end

  def start_engine
    puts "Starting engine for #{@make} #{@model}"
  end
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, we removed the &lt;code&gt;start_engine&lt;/code&gt; method from the &lt;code&gt;Car&lt;/code&gt; class and created two new classes: &lt;code&gt;ElectricCar&lt;/code&gt; and &lt;code&gt;GasCar&lt;/code&gt;. Each class now handles its own implementation of the &lt;code&gt;start_engine&lt;/code&gt; method, depending on the type of car. This way, we can add new types of cars without modifying the existing code.&lt;/p&gt;

&lt;p&gt;In conclusion, the Open/Closed Principle encourages us to design software that is open to extension but closed to modification. By following this principle, we can create more flexible and maintainable software.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Liskov Substitution Principle (LSP)&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;The Liskov Substitution Principle (LSP) is a principle in object-oriented programming that states that objects of a superclass should be replaceable with objects of a subclass without affecting the correctness of the program. In other words, a subclass should be able to substitute for its superclass without any unexpected behavior.&lt;/p&gt;

&lt;p&gt;Let’s take an example of a class hierarchy in Ruby:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Shape
  def area
  end
end

class Rectangle &amp;lt; Shape
  def area
    # calculates area of rectangle
  end
end

class Square &amp;lt; Shape
  def area
    # calculates area of square
  end
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, we have a superclass called &lt;code&gt;Shape&lt;/code&gt; and two subclasses called &lt;code&gt;Rectangle&lt;/code&gt; and &lt;code&gt;Square&lt;/code&gt;. According to the LSP, we should be able to substitute an instance of &lt;code&gt;Rectangle&lt;/code&gt; or &lt;code&gt;Square&lt;/code&gt; for instance of &lt;code&gt;Shape&lt;/code&gt; without any issues.&lt;/p&gt;

&lt;p&gt;Let’s say we have a function that takes an instance of &lt;code&gt;Shape&lt;/code&gt; and calculates its area:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def print_area(shape)
  puts "The area is: #{shape.area}"
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We should be able to pass an instance of &lt;code&gt;Rectangle&lt;/code&gt; or &lt;code&gt;Square&lt;/code&gt; to this function without any issues:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;rectangle = Rectangle.new
print_area(rectangle)

square = Square.new
print_area(square)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This code will run without any errors because &lt;code&gt;Rectangle&lt;/code&gt; and &lt;code&gt;Square&lt;/code&gt; both implement the &lt;code&gt;area&lt;/code&gt; method, which is defined in the &lt;code&gt;Shape&lt;/code&gt; superclass.&lt;/p&gt;

&lt;p&gt;However, if we were to violate the LSP by creating a subclass that does not behave properly when substituted for its superclass, we could run into issues. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Triangle &amp;lt; Shape
  def area
    raise "Triangles don't have a defined area!"
  end
end

triangle = Triangle.new
print_area(triangle)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This code will raise an error because &lt;code&gt;Triangle&lt;/code&gt; does not properly implement the &lt;code&gt;area&lt;/code&gt; method. This violates the LSP because we cannot substitute an instance of &lt;code&gt;Triangle&lt;/code&gt; for instance of &lt;code&gt;Shape&lt;/code&gt; without unexpected behavior.&lt;/p&gt;

&lt;p&gt;Therefore, it is important to follow the LSP when creating class hierarchies to ensure that the behavior of a subclass can be predicted and that it can be substituted for its superclass without causing issues.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Interface Segregation Principle(ISP)&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;The Interface Segregation Principle (ISP) states that clients should not be forced to depend on interfaces they do not use. In other words, it’s better to have multiple smaller interfaces that are focused on a specific task rather than one large interface that tries to do everything.&lt;/p&gt;

&lt;p&gt;Let’s consider an example in Ruby where we have a class &lt;code&gt;Printer&lt;/code&gt; that can print documents. It has two methods, &lt;code&gt;print_pdf&lt;/code&gt; and &lt;code&gt;print_word&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Printer
  def print_pdf(pdf_file)
    # Code to print PDF file
  end

  def print_word(word_file)
    # Code to print Word file
  end
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This implementation violates the ISP because it forces clients to depend on methods they may not use. For instance, if a client only needs to print Word files, it still has to depend on the &lt;code&gt;print_pdf&lt;/code&gt; method.&lt;/p&gt;

&lt;p&gt;To apply ISP, we can refactor our code to create separate interfaces for each method. For example, we can create two interfaces &lt;code&gt;PdfPrinter&lt;/code&gt; and &lt;code&gt;WordPrinter&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;module PdfPrinter
  def print_pdf(pdf_file)
    # Code to print PDF file
  end
end

module WordPrinter
  def print_word(word_file)
    # Code to print Word file
  end
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, we can include only the required interface in our client classes instead of including the entire &lt;code&gt;Printer&lt;/code&gt; class. For example, if we have a client class that only needs to print Word files, we can include the &lt;code&gt;WordPrinter&lt;/code&gt; module:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class WordDocument
  include WordPrinter
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Similarly, if we have another client class that only needs to print PDF files, we can include the &lt;code&gt;PdfPrinter&lt;/code&gt; module:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class PdfDocument
  include PdfPrinter
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This way, we have segregated our interfaces based on client needs.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Dependency Inversion Principle (DIP)&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;The Dependency Inversion Principle(DIP) states:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;“High-level modules should not depend on low-level modules. Both should depend on abstractions. Abstractions should not depend on details. Details should depend on abstractions.”&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In other words, code should be designed so that high-level modules (like classes or modules) do not depend directly on lower-level modules or details of implementation, but rather on abstractions or interfaces that can be implemented by any number of lower-level modules. This allows for greater flexibility, extensibility, and maintainability of code.&lt;/p&gt;

&lt;p&gt;Here’s an example of how you might implement the Dependency Inversion Principle in Ruby:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# High-level module (depends on abstraction)
class PaymentProcessor
  def initialize(payment_gateway)
    @payment_gateway = payment_gateway
  end

  def process_payment(amount)
    @payment_gateway.charge(amount)
  end
end

# Abstraction (does not depend on details)
class PaymentGateway
  def charge(amount)
    raise NotImplementedError
  end
end

# Low-level modules (implement abstraction)
class StripeGateway &amp;lt; PaymentGateway
  def charge(amount)
    # Code to charge using Stripe API
  end
end

class PaypalGateway &amp;lt; PaymentGateway
  def charge(amount)
    # Code to charge using PayPal API
  end
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, the &lt;code&gt;PaymentProcessor&lt;/code&gt; class is a high-level module that needs to process payments. However, it doesn't depend on any specific payment gateway implementation - instead, it depends on an abstraction, the &lt;code&gt;PaymentGateway&lt;/code&gt; class. This allows any number of lower-level modules (like &lt;code&gt;StripeGateway&lt;/code&gt; or &lt;code&gt;PaypalGateway&lt;/code&gt;) to implement the &lt;code&gt;PaymentGateway&lt;/code&gt; interface, and be used by the &lt;code&gt;PaymentProcessor&lt;/code&gt; without any modifications.&lt;/p&gt;

&lt;p&gt;By following the Dependency Inversion Principle, this code is more flexible and easier to maintain. If a new payment gateway implementation needs to be added, for example, it can be implemented using the &lt;code&gt;PaymentGateway&lt;/code&gt; interface and used by the &lt;code&gt;PaymentProcessor&lt;/code&gt; without any changes to the &lt;code&gt;PaymentProcessor&lt;/code&gt; itself.&lt;/p&gt;

&lt;p&gt;In the end, I hope you found this article helpful in understanding the SOLID principles and how they can be applied in any programming language. Do you have any additional tips or examples for applying these principles in your own projects? I would love to hear your thoughts and feedback in the comments below.&lt;/p&gt;

</description>
      <category>ruby</category>
      <category>designpatterns</category>
      <category>oop</category>
      <category>solidprinciples</category>
    </item>
  </channel>
</rss>
