Software Construction &
Development
(Week-6)
Mr. Muhammad Bilal
MS Software Engineering
LECTURER (Department of Software Engineering)
UOL- Lahore
Agenda of Week # 6
 SOLID Design Principles’ Intro
 SRP
 Decorator Design Pattern
SOLID Principles
SOLID principles are a set of guidelines for writing
clean, consistent, reusable, and maintainable code.
1. Single Responsibility Principle
2. Open Close Principle
3. Liskov Substitution Principle
4. Interface Segregation Principle
5. Dependency Inversion Principle
Single Responsibility
Principle
Single Responsibility principle
Single responsibility means that your class (any entity for
that matter, including a method in a class, or a function in
structured programming) should only do one thing.
A class should have only one reason to change, meaning it
should have only one responsibility.
Related to Coupling & Cohesion.
Single Responsibility principle
What a class does?
The more a class does, the more likely it will change.
The more a class changes, the more likely we get introduced
to bugs.
One of the simplest principles but one of the most difficult to
get right.
Single Responsibility principle
Encapsulation of Responsibility
Separation of Concerns
Reduced Complexity
Improved Reusability
Easier Testing
Single Responsibility principle
Requirement:
Manage user authentication and logging
Single Responsibility principle
UserAuthenticator
authenticate(uname, pwd)
log_authentication_attempt(self, success)
Before SRP implementation
Single Responsibility principle
UserAuthenticator
authenticate(unam
e, pwd)
Logger
log_authentication_
attempt(self,
username,
success)
After SRP implementation
Open Closed Principle
Open Closed Principle (Decorators)
Before discussing OCP, let’s see what is a decorator, how it is
used in python.
A decorator is a function that takes another function or a
class as input and extends or modifies its behavior without
explicitly modifying its code.
Decorators allow you to add functionality to existing
functions or classes dynamically, typically by wrapping them
with additional code.
Open Closed Principle (Decorators)
Analogy
An analogy for decorators can be seen in the context of
adding toppings to a pizza. Imagine you have a basic pizza,
and you want to customize it with different toppings.
Decorators act like the toppings that you add to the pizza
without changing the fundamental structure of the pizza
itself.
Open Closed Principle (Decorators)
Here's a real-life code example to illustrate decorators using a
simple function that calculates the square of a number:
# Decorator function
def add_suffix(func):
def wrapper(x):
result = func(x)
return result + " is the result"
return wrapper
# Original function
def calculate_square(x):
return x * x
# Decorating the function
calculate_square =
add_suffix(calculate_square)
# Using the decorated function
print(calculate_square(5)) # Output: 25 is
the result
In this example:
The add_suffix function is a decorator that takes another
function (func) as input.
It defines a nested function wrapper that adds a suffix to
the result of calling the original function (func).
The wrapper function modifies the behavior of the
original function by adding the suffix.
Finally, the original function calculate_square is
decorated by passing it to the add_suffix decorator.
When calculate_square is called, it invokes the modified
behavior defined by the decorator.
In essence, the decorator add_suffix wraps the original
function calculate_square with additional
functionality (adding a suffix to the result) without
modifying the original function's code directly.
Open Closed Principle (Decorators)
Open Closed Principle (Decorators)
For further readings about the Decorator Design Pattern you
must check the decorator_design_pattern.pdf file.
OR
https://refactoring.guru/design-patterns/decorator

design patterns in python week 6 scd.pdf

  • 1.
    Software Construction & Development (Week-6) Mr.Muhammad Bilal MS Software Engineering LECTURER (Department of Software Engineering) UOL- Lahore
  • 2.
    Agenda of Week# 6  SOLID Design Principles’ Intro  SRP  Decorator Design Pattern
  • 3.
    SOLID Principles SOLID principlesare a set of guidelines for writing clean, consistent, reusable, and maintainable code. 1. Single Responsibility Principle 2. Open Close Principle 3. Liskov Substitution Principle 4. Interface Segregation Principle 5. Dependency Inversion Principle
  • 4.
  • 5.
    Single Responsibility principle Singleresponsibility means that your class (any entity for that matter, including a method in a class, or a function in structured programming) should only do one thing. A class should have only one reason to change, meaning it should have only one responsibility. Related to Coupling & Cohesion.
  • 6.
    Single Responsibility principle Whata class does? The more a class does, the more likely it will change. The more a class changes, the more likely we get introduced to bugs. One of the simplest principles but one of the most difficult to get right.
  • 7.
    Single Responsibility principle Encapsulationof Responsibility Separation of Concerns Reduced Complexity Improved Reusability Easier Testing
  • 8.
  • 9.
    Single Responsibility principle UserAuthenticator authenticate(uname,pwd) log_authentication_attempt(self, success) Before SRP implementation
  • 10.
    Single Responsibility principle UserAuthenticator authenticate(unam e,pwd) Logger log_authentication_ attempt(self, username, success) After SRP implementation
  • 11.
  • 12.
    Open Closed Principle(Decorators) Before discussing OCP, let’s see what is a decorator, how it is used in python. A decorator is a function that takes another function or a class as input and extends or modifies its behavior without explicitly modifying its code. Decorators allow you to add functionality to existing functions or classes dynamically, typically by wrapping them with additional code.
  • 13.
    Open Closed Principle(Decorators) Analogy An analogy for decorators can be seen in the context of adding toppings to a pizza. Imagine you have a basic pizza, and you want to customize it with different toppings. Decorators act like the toppings that you add to the pizza without changing the fundamental structure of the pizza itself.
  • 14.
    Open Closed Principle(Decorators) Here's a real-life code example to illustrate decorators using a simple function that calculates the square of a number:
  • 15.
    # Decorator function defadd_suffix(func): def wrapper(x): result = func(x) return result + " is the result" return wrapper # Original function def calculate_square(x): return x * x # Decorating the function calculate_square = add_suffix(calculate_square) # Using the decorated function print(calculate_square(5)) # Output: 25 is the result In this example: The add_suffix function is a decorator that takes another function (func) as input. It defines a nested function wrapper that adds a suffix to the result of calling the original function (func). The wrapper function modifies the behavior of the original function by adding the suffix. Finally, the original function calculate_square is decorated by passing it to the add_suffix decorator. When calculate_square is called, it invokes the modified behavior defined by the decorator. In essence, the decorator add_suffix wraps the original function calculate_square with additional functionality (adding a suffix to the result) without modifying the original function's code directly. Open Closed Principle (Decorators)
  • 16.
    Open Closed Principle(Decorators) For further readings about the Decorator Design Pattern you must check the decorator_design_pattern.pdf file. OR https://refactoring.guru/design-patterns/decorator