SOLID Principles
1. Single Responsibility Principle
(SRP)
 According to the single responsibility
principle, each Java class must perform only
one function.
 Multiple capabilities in a single class mashup
the code, and any changes made to the code
may influence the entire class.
 It specifies the code and makes it simple to
maintain.
Example of code before
implementing SRP:
Example of code after implementing
SRP:
Think about this example
If you have the following code for
school management, How would
you refactor the class to adhere to
SRP?
Answer:
2. Open-Closed Principle
 The Open-Closed Principle (OCP) states that a
class should be open for extension but closed for
modification. This means that the behavior of a class
can be extended without modifying its source code. In
other words, you can add new features to the class
without changing its existing behavior.
 Extending the class can be done in a variety of ways,
including,
• Inheriting from class.
• Overwriting the required behavior from the class.
• Extending certain behavior of the class.
Example of code before
implementing OCP:
Example of code after implementing
OCP:
Think about this example
If you have the following code for
payment processor, How can you
redesign the PaymentProcessor
class to follow the OCP?
Answer:
Answer:
3. Liskov Substitution Principle
(LSP)
 The LSP states that if a program is using a base
class (Like Interface), it should be able to use
any of its derived classes (Inherited Classes)
without knowing it. In other words, a derived class
should be able to substitute for its base class
without changing the behavior of the program.
 When a class is a subtype of another class, it
means that it inherits the behavior and properties
of the base class.
 However, if the derived class has different
behavior or properties than the base class, it can
cause unexpected behavior in the program.
Example of code before
implementing LSP:
Example of code after implementing
LSP:
Another answer:
Think about this example
Consider a class hierarchy where a
Bird class has a method fly(). You
have subclasses such as Sparrow
and Ostrich inheriting from Bird.
Given that not all birds can fly (e.g.,
ostriches cannot fly), how does this
design violate the Liskov
Substitution Principle? Propose a
solution to rectify this issue.
Answer:
4. Interface Segregation Principle
 The Interface Segregation Principle (ISP) is a
principle in object-oriented programming that
states that a client should not be forced to
depend on methods it does not use.
 the Interface Segregation Principle (ISP) is a
principle in object-oriented programming that
states that a client should not be forced to
depend on methods it does not use.
Example of code before
implementing ISP:
Example of code after implementing
ISP:
Think about this example
•A SmartDevice interface includes
methods for functionalities like
calling, browsing the internet, and
taking photos. Some devices, like
smart cameras, only need the photo-
taking capability. Explain how the
current design violates the Interface
Segregation Principle and suggest a
way to redesign the interface(s) to
resolve this violation.
Answer
Answer
For more details about SOLID principles
https://everyone-can-
code.github.io/solid/srp/https://everyone-
can-code.github.io/solid/srp/
Thank You.

Sec1_SOLID Principles_Software Engineering.pptx

  • 1.
  • 2.
    1. Single ResponsibilityPrinciple (SRP)  According to the single responsibility principle, each Java class must perform only one function.  Multiple capabilities in a single class mashup the code, and any changes made to the code may influence the entire class.  It specifies the code and makes it simple to maintain.
  • 4.
    Example of codebefore implementing SRP:
  • 5.
    Example of codeafter implementing SRP:
  • 6.
  • 7.
    If you havethe following code for school management, How would you refactor the class to adhere to SRP?
  • 8.
  • 9.
    2. Open-Closed Principle The Open-Closed Principle (OCP) states that a class should be open for extension but closed for modification. This means that the behavior of a class can be extended without modifying its source code. In other words, you can add new features to the class without changing its existing behavior.  Extending the class can be done in a variety of ways, including, • Inheriting from class. • Overwriting the required behavior from the class. • Extending certain behavior of the class.
  • 11.
    Example of codebefore implementing OCP:
  • 12.
    Example of codeafter implementing OCP:
  • 13.
  • 14.
    If you havethe following code for payment processor, How can you redesign the PaymentProcessor class to follow the OCP?
  • 16.
  • 17.
  • 18.
    3. Liskov SubstitutionPrinciple (LSP)  The LSP states that if a program is using a base class (Like Interface), it should be able to use any of its derived classes (Inherited Classes) without knowing it. In other words, a derived class should be able to substitute for its base class without changing the behavior of the program.  When a class is a subtype of another class, it means that it inherits the behavior and properties of the base class.  However, if the derived class has different behavior or properties than the base class, it can cause unexpected behavior in the program.
  • 20.
    Example of codebefore implementing LSP:
  • 21.
    Example of codeafter implementing LSP:
  • 22.
  • 23.
  • 24.
    Consider a classhierarchy where a Bird class has a method fly(). You have subclasses such as Sparrow and Ostrich inheriting from Bird. Given that not all birds can fly (e.g., ostriches cannot fly), how does this design violate the Liskov Substitution Principle? Propose a solution to rectify this issue.
  • 26.
  • 27.
    4. Interface SegregationPrinciple  The Interface Segregation Principle (ISP) is a principle in object-oriented programming that states that a client should not be forced to depend on methods it does not use.  the Interface Segregation Principle (ISP) is a principle in object-oriented programming that states that a client should not be forced to depend on methods it does not use.
  • 29.
    Example of codebefore implementing ISP:
  • 31.
    Example of codeafter implementing ISP:
  • 32.
  • 33.
    •A SmartDevice interfaceincludes methods for functionalities like calling, browsing the internet, and taking photos. Some devices, like smart cameras, only need the photo- taking capability. Explain how the current design violates the Interface Segregation Principle and suggest a way to redesign the interface(s) to resolve this violation.
  • 34.
  • 35.
  • 36.
    For more detailsabout SOLID principles https://everyone-can- code.github.io/solid/srp/https://everyone- can-code.github.io/solid/srp/ Thank You.