You are on page 1of 15

6/2/2022

Faculty of Engineering, Science and Technology


Department of Information Technology

CPT305 Data and Process Modeling

LECTURE 13
OBJECT ORIENTED DESIGN

Design Problems
 Mainly there are three kinds of problems
Rigidity

Immobility

Fragility

1
6/2/2022

Rigidity

 Occurs when classes depend highly on each


other
 Modifying a small portion would affect the
other classes.
 When changes are made, we have to change
the whole thing.

Immobility

 Occurs when it is hard to use in another


application.
 Very risky to separate from its original system.
 Immovable

2
6/2/2022

Fragility

 Occurs when the software breaks with the


modification of one part of the software.
 A small change can break the whole software

Design Principles
 To overcome these problems, we have some
guidelines that we can follow called design
principles
 These are the principles upon which object
oriented programming is built on
 Mainly you can apply these five principles to
have a good design
 SOLID principles

3
6/2/2022

Single Responsibility Principle

 The SRP dictates that classes should have


only a single reason to change.
 states that every class computer program
should have responsibility over a single part of
that program’s functionality, and it
should encapsulate that part.
 It works in OOP by refactoring methods into
separate classes

Single Responsibility Principle

 The SRP is a widely quoted justification for


refactoring, because SRP limits the impact of
change that might be happen when we’re trying to
refactor .
 Any change required of a code system will
naturally need changes in the body of the code at
a number of different points.
 SRP minimizes the number of classes that need
to change.
 This allows you to hide the change as much as
possible behind encapsulation boundaries

4
6/2/2022

 rectangle class that has two methods; Area () &


Draw ().
 if there are changes in the GUI, we must modify
the Rectangle class, then we are obliged to test
again the other application that attacks the same
class

Open Close Principle


 In object-oriented programming, the open–
closed principle states "software entities
(classes, modules, functions, etc.) should be
open for extension, but closed for
modification"; that is, such an entity can allow
its behavior to be extended without modifying
its source code.

5
6/2/2022

Open Close Principle


How it works in OOP
 Open for extension – This means that the

behavior of the module can be extended. As the


requirements of the application change, we are
able to extend the module with new behaviors that
satisfy those changes.

 Closed for modification – Extending the


behavior of a module does not result in changes
to the source or binary code of the module.

Open Close Principle


 For example, without modifying the module, it
should be possible to add fields to the data
structures it contains or new elements to the
set of functions it performs.

6
6/2/2022

Liskov Substitute principle


 It states that a superclass object should be
replaceable with a subclass object without
breaking the functionality of the software

 The Liskov Substitution Principle helps us


model good inheritance hierarchies.

7
6/2/2022

Interface Segregation Principle (ISP)

 “Clients should not be forced to depend upon


interfaces that they do not use.”
 ISP main objective is to prevent bloated interfaces
that define methods for multiple responsibilities

8
6/2/2022

How ISP works in OOP


 ISP Code Violations:
 BulkyInterface
 Unused Dependencies

 Methods throwing Exceptions

 Refactoring Code Smells


 SplitFat Interfaces into specific ones
 Abstract away the unwanted methods

Example of ISP
 Burger Place (Without ISP)

9
6/2/2022

Example of ISP

Refactoring Code Smells

Abstracting Away using


adapter pattern

Dependency Inversion Principle

 Technically, dependency inversion comprises


two separate principles:
 "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."

10
6/2/2022

Dependency Inversion Principle

 Bad design often derives from degradation


due to new requirement and maintenance
 Rigidity

 Fragility

 Immobility

 Interdependence of the modules

Dependency Inversion Principle

11
6/2/2022

Dependency Inversion Principle

 Module containing high level policy should be


independent upon low level detail modules

 We have to use abstraction to limit dependency

Design Patterns
 A design pattern is a general solution for a
common software design problem
 These are tested and proven to be more
effective when included in the design
 There are three kinds
Creational

Structural

Behavioral

12
6/2/2022

Creational Compiled by: Safwan

 Singleton
 What it is?
Ensures that a class that is responsible for creating an
object is allowed to create only one instance, and
provide a global point of access to it.

 Singleton should be considered only if all three of the


following criteria are satisfied:
❖ Ownership of the single instance cannot be
reasonably assigned
❖ Lazy initialization is desirable
❖ Global access is not otherwise provided for

Creational
 Example
The office of the President of the United States is a
Singleton. The United States Constitution specifies the
means by which a president is elected, limits the term of
office, and defines the order of succession. As a result,
there can be at most one active president at any given
time. Regardless of the personal identity of the active
president, the title, "The President of the United States" is
a global point of access that identifies the person in the
office.

13
6/2/2022

Structural

 Façade
 What it is?
❖ Defines a unified, higher level interface to a
subsystem that makes it easier to use.
 Example
Consumers encounter a Facade when ordering from a
catalog. The consumer calls one number and speaks
with a customer service representative. The customer
service representative acts as a Facade, providing an
interface to the order fulfillment department, the billing
department, and the shipping department.

Behavioral
 Strategy
 What it is?
❖ Define a family of algorithms, encapsulate each one, and
make them interchangeable.
❖ Strategy lets the algorithm vary independently from the
clients that use it.
❖ Capture the abstraction in an interface, bury
implementation details in derived classes.

14
6/2/2022

Behavioral
 Example
Several options exist such as driving one's own car, taking a
taxi, an airport shuttle, a city bus, or a limousine service. For
some airports, subways and helicopters are also available as
a mode of transportation to the airport. Any of these modes
of transportation will get a traveler to the airport, and they
can be used interchangeably. The traveler must chose the
Strategy based on tradeoffs between cost, convenience, and
time.

15

You might also like