100% found this document useful (1 vote)
700 views103 pages

Low Level Design

This document discusses object-oriented programming (OOP) concepts, design principles, and design patterns. It covers key OOP concepts like classes and objects, access modifiers (public, private, protected), constructors, static keyword, this keyword, inheritance, polymorphism, encapsulation, and abstraction. It also discusses common OOP design principles like DRY, KISS, and YAGNI. It provides examples of common design patterns like Factory Method, Singleton, Builder, Observer, and Abstract Factory.

Uploaded by

Shehar Bano
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
700 views103 pages

Low Level Design

This document discusses object-oriented programming (OOP) concepts, design principles, and design patterns. It covers key OOP concepts like classes and objects, access modifiers (public, private, protected), constructors, static keyword, this keyword, inheritance, polymorphism, encapsulation, and abstraction. It also discusses common OOP design principles like DRY, KISS, and YAGNI. It provides examples of common design patterns like Factory Method, Singleton, Builder, Observer, and Abstract Factory.

Uploaded by

Shehar Bano
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 103

Low

Level
= Design
- Kapil Yadav

- OOPs concepts

- Design Principles

- Design Patterns
Kapil Yadav
-

INDEX

1 .
Classes and
objects .

2- Access Modifiers in Ctt .


public

Private (friend class and
friend fume] .

→ Protected

3 . Constructor


Default

Parameterized

copy
4. static
keyword
5 .
This keyword
6 .

New keyword
7. Const
keyword
8- Final
keyword
9.
Explicit keyword

10 .
Inheritance
Inheritance
single

Multiple

Inheritance
→ Inheritance
Multilevel

Hierarchical Inheritance

Hybrid Inheritance .

11 "

Polymorphism

Compile Time
Polymorphism
Function
overloading


operator overloading
Kapil Yadav

polymorphism
→ Run time

virtual Functions

12 .
Encapsulation

13 .
Data Abstraction

1. OOPS DESIGN PRINCIPLES



DRY
• KISS
• YAGNI

2. COHESION & COUPLING


3. CQS
4. SOLID

OOPS DESIGN PATTERN


Pattern :
17
Factory Method
Design
-

2. SINGLETON DESIGN PATTERN : -

Builder
3.
Design Pattern
Design Pattern
4. Observer
5 Abstract
.

Factory Design

linkedin.com/in/kapilyadav22
Kapil Yadav
-

class : -

Blueprint

→ state /Property /field/data member
design / type
*m⑦ behaviour / function / data function .

Humans hand

→§ }→
Ex - 2
state /properties
Leg
2

↓ 2
eyes
behaviour → walk eat
, speak , .

Architect class ,
gives Blueprint
→ is the
using
Blueprint ,
we make the house is object .

memory / space
takes class does 't
Object

.
,

Object is an implementation / seal


entity/
instance of class .

→ class is a
logical component .


Object takes
memory from heap .

linkedin.com/in/kapilyadav22
Kapil Yadav

linkedin.com/in/kapilyadav22
Kapil Yadav

Access Modifiers in C++ : -

One of the main features of 00ps is data


Hiding .

Data
Hiding
: -
Data
hiding refers to
restricting access

to data members of a class .


This is to prevent other

functions and classes from tampering with the class


data .

→ The access modifiers of C++ allows us to determine

which class members are accessible to other classes and

functions ,
and which are not .

→ There are 3
types of access modifiers available in
C++ .

I. Public

2 .
Private
3. Protected

By default ,
The access modifier of the members will be Private .

1. Public :
keyword used create
The
public is to
public
-

members (data and functions] .

accessible
The
public members from part of the

are
any
program .

The public members be accessed from the


anywhere in

can
program
using
direct
memory
access
operator C.) with the
object
of that class .

linkedin.com/in/kapilyadav22
Kapil Yadav

linkedin.com/in/kapilyadav22
Kapil Yadav

Private Access Modifier : -

The
private keyword is used to create
private members

(data and
functions ] .


The private members can
only be accessed from
within the class .


However , friend classes and friend functions can access

private members .

data members of class


we the
private

can access a

indirectly using the public member functions of the

class .

linkedin.com/in/kapilyadav22
Kapil Yadav

Friend class ? -
A friend class can access private and
class it
protected members of other in which is

defined as friend .
It is sometimes used to allow
a
particular class to access
private members of
other class .

linkedin.com/in/kapilyadav22
Kapil Yadav

Friend Function : -

linkedin.com/in/kapilyadav22
Kapil Yadav

linkedin.com/in/kapilyadav22
Kapil Yadav

Protected Access Modifier : -

→ Protected Access Modifier is similar to


private access modifier ,

→ But the difference is


protected members can be access

within the class and from the desired class as well .

linkedin.com/in/kapilyadav22
Kapil Yadav
Kapil Yadav

CONSTRUCTOR.com/stnectoris a special method that is invoked


automatically
at the time
of object creation .

initialize data
objects

It is used to the members new
of .

return
Constructors don't have
type

any
.

Constructor have class


itself
→ the
same name as .


don't
If we
specify a constructor, C++
compiler generates
a default constructor for object .


It should be placed in public section
of
class .

→ It can be overloaded .

Constructors in C++

Default Parameterized copy


we can do constructor private .
But what will

happen ?

linkedin.com/in/kapilyadav22
Kapil Yadav
Kapil Yadav

→ Do constructor returns
any
value ?
No constructor does not return value
,
any
.
Kapil Yadav

STATIC KEYWORD : -

↳ static variables : variables in a


function ,

variables in a class

class and functions


static Members of class : -

objects
in a class .

class static member is accessed class


In
using

, ,

not
object .

class Human {

static ént count ;

Human C)
{ count tt ;

y
3; 11 initialize static member of class Human

int Human : : count -0


;

int main C)
E coat <<
Human : : count -
Lendl ;

ng

Q ? Can static data member


I access without
creating an

object ?
Anis .
Yes .

linkedin.com/in/kapilyadav22
Kapil Yadav

Static function → can


change static Data Member .
.

Cannot static Data Member


change the value
of non -
.
Kapil Yadav

NOTE : -
A non
-
static member function can
modify
a static data member as
long as the data member 's

visibility allows it .

keyword method

we cannot use this in static .

Q? why main function in Java is static ?


And

linkedin.com/in/kapilyadav22
Kapil Yadav

THIS
keyword :
-


Object Pointer A pointer contains address
of
-

called Pointer
an
object is
object .

local
this
object pointer in every
→ is a

instance member
function containing address
of the caller
object .

→ this
pointer cannot be
modify .

caller
→ 9T is used to refer object in member

function .

Friend functions do not have this pointer



,

because
friends are not members of a class .

Only member
functions have a this
pointer .

linkedin.com/in/kapilyadav22
Kapil Yadav

linkedin.com/in/kapilyadav22
Kapil Yadav

NEW KEYWORD : -

→ The new is a
memory
allocation operator ,

which is used to allocate at the


memory
runtime .

→ The
memory initialized by
the new operator is

allocated in a
heap .

returns
It the
starting address of the

memory,
variable
which
get assigned to the .

Syntax type variable new


type (parameter list) ;
-
=
-

variable
Type datatype of
• -

a .


variable -
name of the variable .

parameter - list -

is the list of values that are

initialized to a variable .

int *
pts =
new int ;

WHAT IS MALLOC ?
→ Malbec ) is function that allocates at
a
memory
the runtime .


Syntax
-

type variable name -


-

Ctypetamalloclsizeofctype));
variable
type it és the
datatype of

: the .


variable -
name : it defines the name of the variable

that points to the


memory .

(type *) for that


typecasting
• : It is used so we can

get the
pointer of specified type
a that

to the
points memory
.

linkedin.com/in/kapilyadav22
Kapil Yadav

NOTE :
-
The mallocc ) function returns the void pointer ,

$0
typecasting is
required to
assign a different
type to the
pointer The sizeof
.
operator is

required in the mallee function as the Malbec)

function returns the raw


memory ,
so the
sizeofc)
operator will tell the Malloch) function ,
how

much
memory
is
required for the allocation .

Allocation New
→ Memory using

delete p ;

linkedin.com/in/kapilyadav22
Kapil Yadav

Allocation mallee
Memory using
: -


If the sufficient memory is not available ,
mallee function returns the NULL
pointer .


de allocate
using free
Allocated can be
memory
function .

linkedin.com/in/kapilyadav22
Kapil Yadav

Difference between New and Maller


EW MALLOC

1. New
operator construct 1 . Mallee is a
function ,
it does

(it calls the constructor


an
object not call the .

constructor to
initialize
an
object .

2. delete operator to
destroy
2.
free function to deallocate
the
the
object .

memory .

3. new is an
operator 3 . It is predefined function
a

in stdlib.tn header file .

4. new can be overloaded . 4 . Mallock ) cannot be overloaded .

5-
If sufficient memory is not 5 . Malloch ) will seteem a NULL

available , new will throw pointers .

an
exception

6. we need to
specify 6
. We need to
specify number
allocated
number of objects .

of bytes
to be .

7. allocated be reallocated
memory by new 7 . It can
using
cannot be resized .
ocallocc)
function .

8. Execution time of new is 8 . E T


-
is more than new .

less than mallee

linkedin.com/in/kapilyadav22
Kapil Yadav

FINAL
keyword : _

→ a-+11 allows built-in


facility to
prevent
overriding of virtual function using final specifier .

overriding final
'
error -

functions
'

'

derive
error : -
cannot
from
'
final
' '
base '
Base in
' '
derived Derived
type

linkedin.com/in/kapilyadav22
Kapil Yadav

CONST KEYWORD ! -

→ Const
keywords used to define the constant
value that cannot
change during program
execution .

Use of const
keyword with different parameters : -


Use const variable

const with
Use
pointers

Use const with variables


pointer


Use const with function arguments

Use const with class member functions

class data members



Use const with .

Use class
objects

const with .

1. Consent variable
const int a- 20 ;

a -_ at 105
OUTPUT : -
error .

2. Const pointer
cannot address of const
we
change the the

pointer after its


initialization ,
which means the

pointer will
alwaysis point to the same address

once the pointer initialized as the const

pointer .

linkedin.com/in/kapilyadav22
Kapil Yadav

3. Pointer to constant variable

→ It means pointer points to the value of


a const variable that cannot
change .

Const into a;
]→ both pointer
are
to

char constant variable


constancy ;
Kapil Yadav

4. Constant function Arguments

linkedin.com/in/kapilyadav22
Kapil Yadav

5. Const
pointer pointing to a const variable : _

const
Syntax const datatypes varname ;
-
Kapil Yadav

6. Pass const
argument value to a non const
parameter of
- -

a function cause error : -

'
→ •
into ?
from
'
error : -
invalid conversion const intro to

For return Tetum


7. const
type : the
type of
-

the function is const and so it returns a

const value to
integer us .

→ There will be no issue whether

we
pass const or non
-

const
variable to the
function because
the value will be oeteerned

by the
function will be

constant
automatically .

As the
the
argument of
function is non
-
const .

linkedin.com/in/kapilyadav22
Kapil Yadav

8. For return
const
type and const
parameter : -

→ Here ,
both const and non -
const values can be

passed as the const


parameter to the
function ,

but not allowed to


we are then
change the
value
of
a
passed variable because the
parameter
is const .

Otherwise
,
we will face the error .

"

y is a const war its value can't be


changed .

linkedin.com/in/kapilyadav22
Kapil Yadav

Inheritance ? -

In C++, there are 5


types of Inheritance .

1.
Single
2. Multilevel
3. Hierarchical
4.
Multiple 3- Not in Java
5-
Hybrid
17 Base class
Single : -


☒ → derived class

output : 35 60

linkedin.com/in/kapilyadav22
Kapil Yadav

2) Multilevel 1-

Car

t
Toyota


Innova

linkedin.com/in/kapilyadav22
Kapil Yadav

37 Hierarchical ?

Employee

< ]

Intern FTE

linkedin.com/in/kapilyadav22
Kapil Yadav

Multiples speaks speaks .

MOM DAD

cH%D
classes
There Be
ambiguity the Base

can ,
when

functions have same name to soothe this : -

use
scope resolution operator .

scope
g→ oesolution
operator
Kapil Yadav

Hybrid ?
-

Vehicle

car derived from Vehicle

Car
Racing
Ferrari derived from Cart
*
Racing
Ferrari

linkedin.com/in/kapilyadav22
Kapil Yadav

ADVANTAGES : -

Reusability

code .

→ don't
Improves code
readability as we need to

rewrite the code code 100k$ Cleaner and


same
again ,
Treatable .

Inheritance classes
supports extensibility
→ as new can

classes
be
easily added to
existing
.

linkedin.com/in/kapilyadav22
Kapil Yadav

POLYMORPHISM : _

POLY MORPH
↓ ↓

Forms
Many .

→ In Ctt
, polymorphism is
mainly divided into two
types : -


Compile-time polymorphism .


Runtime Polymorphism .

Polymorphism

N
L

Compile Time Run Time

✓ ☒ ✓
Virtual
Function Operator
Functions
Overloading Overloading
1. Compile-time polymorphism : -
This
type of polymorphism
is achieved
by function overloading or
operator
overloading .

Function
Overloading
• : -

→ when there are


multiple functions with the same name

but different parameters ,


then the functions are said to be

overloaded .

Functions be overloaded
by
→ •
can .

changing the number of arguments


OR / AND
Kapil Yadav

changing the
type of arguments

.

linkedin.com/in/kapilyadav22
Kapil Yadav

Q) Can we overload main method ?


To overload main() function in C++, it is necessary to use class and declare the
main as member function. Note that main is not reserved word in programming
languages like C, C++, Java and C#. For example, we can declare a variable
whose name is main, try below example:

linkedin.com/in/kapilyadav22
Kapil Yadav

Operator Overloading : -

with
C++ has
ability
the to
provide the operators a

special meaning for a data


type, this
ability is known

as operator overloading
.

How to overload theoperator :-


To overload an
operator a operator function
,
is defined
inside a class .

class class

{
-
name

% "ᵗ" Pⁿʰ " "

return
type operator Cargs)

:
3;

Kapil Yadav
Kapil Yadav

linkedin.com/in/kapilyadav22
Kapil Yadav

linkedin.com/in/kapilyadav22
Kapil Yadav
Kapil Yadav

R-ntmeymrp.cm?-

It is achieved
by function overriding .

Function
Overriding :

-

derived class
Function
overriding has

occurs when a a

definition for one


of the member functions of the base

class . That base function is said to te overrides .

Rules →
Must do inheritance .

→ same
function name / same
parameter .

⑦7 can we override static method ?


Kapil Yadav
linkedin.com/in/kapilyadav22
Kapil Yadav

Function ?
Virtual A virtual function is member
-

function ,
which is declare within a base class and

is overridden derived class


by a .

→ when refer to a derived class object using a


we

reference to the base class can call


pointer a or
, we

a virtual function for that object and execute


the desired class 's version of the function .

Run-time
They mainly use to achieve
→ are

polymorphism The resolving


. of function call is done at
runtime .

Functions declared virtual


with
keyword
→ are a

in the base class .

Points to remember ? -

1. virtual functions cannot be static .

2. Virtual function can be a friend function of


another class .

3 .

They are
always defined in the base class and

class (
may / maynot
overridden in a derived . we

override in the derived class , optional ) .

4. A class have virtual destructor, but it


may
cannot have a virtual constructor .

linkedin.com/in/kapilyadav22
Kapil Yadav

OUTPUT -
This is call to
parent class area

linkedin.com/in/kapilyadav22
Kapil Yadav

OUTPUT : 25
Square area :
-
.

linkedin.com/in/kapilyadav22
Kapil Yadav
Use of Virtual Function ?

It allows us to create a list
of base class pointers
and call methods derived class without
of any
even

knowing the kind of derived class


object .


working of virtual functions (concepts of VTABZE and

VPTR) .

If a class contains a virtual function then the

compiler itself does two


things .

If the class is created then


1 .

object of a

virtual pointer CVPTR) is inserted as a data


member of the class to point to VTABLE of
that class .

For each new


object created , a new virtual

pointer is inserted as a data member of that


virtual
class .

pointer is inherited
by derived
classes . .

Irrespective of not ,
2 .

object is created or

class contains as member static


array
a a

of function pointers called VTABLE .

Virtual Table -
A table created at compile time for
derived
every single class
containing the most versions

of virtual function only .

A virtual table contains


entry for

one

each virtual
function that can be
class
called
by objects of the .

linkedin.com/in/kapilyadav22
Kapil Yadav

linkedin.com/in/kapilyadav22
Kapil Yadav

Virtual Destructor :-

Deleting a derived class

object using a
pointer of base class
type
that
has a non -
virtual destructor oesults in undefined
behaviour .

OUTPUT
constructing
: -
base

constructing derived

Destructing base

linkedin.com/in/kapilyadav22
Kapil Yadav

class used make


NOTE :
-
The base
pointer was to
class
the derived
object .

Destructor has to be made virtual otherwise


,

Derive Destructor will not call .

OUTPUT ? -

constructing base

derived
constructing
derived
Destructing
Destructing base
Kapil Yadav

⑦7 what about virtual constructor ?


Because when constructor of
It is not possible .
a

a class is executed there is no virtual table

in the
memory , means no virtual pointer defined
yet .

,
so constructor should
always be non -
virtual .

Because not
the
object created virtual

is
,

construction is impossible .


The compiler must know the type of object
before creating it .

Constructors
'
Error -

cannot be declared virtual ?


Kapil Yadav

Encapsulation : -

Encapsulation is defined as
wrapping up
of data and information under single
a unit .

Role of access
specifiers in encapsulation : -

The
process of implementing encapsulation can be scut divided
-

into two
steps : -

1. The data members should be labeled the


as
private using
private access specifiers .
Kapil Yadav

2. The member function which manipulates the data members

should be labeled as
public using the public access specifier .

→ Data encapsulation led to the important OOP


concept of
data
hiding .

tread " " "


→ we can write
only or write
only methods
data
to
implement hiding .

linkedin.com/in/kapilyadav22
Kapil Yadav

Abstraction ? Date abstraction is


process of providing
-
a

only the essential details to the outside world

details
and
hiding the internal

C++
provides great level of abstraction for

a . ex -

pow function is used to calculate the power of


a number without
knowing the
algorithm the function
follows .

Data Abstraction be achieved in two :


can
ways
-

••
Abstraction classes
using .


Abstraction in header files .

linkedin.com/in/kapilyadav22
Kapil Yadav

not allowed
directly
→ to
we are access
my,z
however them member
function
using
, we can access

of the class .

Advantages of Data Abstraction 2-

→ A does not need to write the low level


programmer
code .

→ Data abstraction avoids code duplication .

→ Increases Zeus
ability .


Implementation details class
of the are
protected from
the inadvertent user level errors .

linkedin.com/in/kapilyadav22
Kapil Yadav ①

OOPS DESIGN PRINCIPLES

Clean code ? →
easily readable
by other
developers .

Why ?
How ?

meaningful variable name
, function ,
class etc .

→ comments .

→ Modular

functions

short,

→ structured , readable , simple .

DESIGN PRINCIPLES : _

DRY Don't
repeat yourself
→ :
.

→ Kiss
keep it simple stupid
:
.


Abstraction :

Curly 's law


→ :

Boy Scout law ÷



Do better code
quality than what it was
-
.

Features of ?
Good
Design
-

l . Code Reuse : -

'

is
2.
Extensibility
:
change the
only constant
thing
_

in
programmer
's life ?

linkedin.com/in/kapilyadav22
Kapil Yadav

Naming Intension
revealing
→ -
names .


descriptive names .

-
int ✗
int getsumc ) ; a

~ ✗
b
int
getanswe.ro ; int

② lines]
Verb
Function Name dedicate
single
→ -
short to task
, ,
.


should have fewer arg cements ( Use Helper function ) .

• sort , several
, swap, pow .


Ward 's principle
-

Class Name
-

Noun

descriptive
meaningful


intent
revealing
-

linkedin.com/in/kapilyadav22 RCnMartin_
Kapil Yadav

DRY PRINCIPLE ( Don't repeat Yourself)

law variable should


Curly 's → A mean one
thing ,
and

one
thing only .

② KISS ( Keep it
simple stupid)

if (a)
{ if (b)
{ ifcc)
{ if (d)
{ 3
else {3
3
}
3


if (a && 6 && c && d) is
simple .

Benefits ? -

easy
to read
/ understand .


less time to code .

Bugs chances less


are .

debug / modify / update


linkedin.com/in/kapilyadav22
?⃝
Kapil Yadav

③ YAGNI : -
You ain't
gonna
need it .

"

Always implement things which are


actually
needed
just foresee
"
not the ones
, you .


Don't
engineering
Do over .

Benefits : -


Slave time .


concise code .

the root of all Evils



Preoptimisalin is .

Boy -
Scout Law ? -
The code
quality tends to
degrade
with each
change .
-

(tech debt .

'

leave behind better state
Always the code in a

found
"
than it
you .

linkedin.com/in/kapilyadav22
Kapil Yadav

COHESION & COUPLING : _

Cohesion related &


: -
It is the
degree of how
strongly
focused are the various responsibilities of a module .

→ Maximum cohesion .

Coupling
: -
It is the degree to which each module depend
on other modules .

HUMAN → CAR


Required low
coupling .

Command -

Query separation :(CQS)

Command the state but doesn't return



changes any
value .

Query return the stale without


changing the state
→ .

CQS → It states that method should be


every
a

command that performs an action, or a


query
that returns data to the caller, but not both .

linkedin.com/in/kapilyadav22
Kapil Yadav

Is there
exception of CQS ?
any

SOLID -
a set of principles .

S
single responsibility principle
-
.

0
Open closed
principle
-
-
.

L -
Liskov Substitution Principle
I Interface Segregation Principle
-

D-
Dependency Inversion Principle .

Single Responsibility principle :


-

class should have


A
just one reason to
change .

→ module ( module set functions class


Any of
means a
, ,

code ) should
package ,
source have a reason to
change by
only one actor .

have class have


Let's
say
we a
Employee ,
some

function calculate
salary ,
calculate hours , savec-mpdate.CI .

Calculates
alary C) CFO
-

calculatehours C) -

Technical
savec-mpdato.CI HR
-

linkedin.com/in/kapilyadav22
Kapil Yadav

used and it
If calculates
alanya by
→ is CFO
acquired
a method
getRegularHours O and make a
change in

will also calculate


getregulartloursc] ,
it
change in

Hours C) ,
which is
managed by someone else .

linkedin.com/in/kapilyadav22
Kapil Yadav

here class
→ what
happened was ,
one
exposed two or three

different methods ,
which were
corresponding to different
stakeholders of the software .

One Actor doesn't have to know about the other

actor, but still


change in the one them is
ofthe
reflected the other .

It is
voélating Single
Responsibility Principle .

Single Responsibility that


'

principle the code



means
, you
writing
code
are
, if that
requested change in one

stakeholder ,
which
fulfill one business
Requirement ,

till then it is
fine .

class have
That means ou r can more than one
public
method as
long as the
change in those
public method
is
requested by one stakeholder / group of stakeholders .

→ Create 3
different class ,
the calculation
of salary
doesn't
depend
need to know about / doesn't need on method
,
any
which is used in calculate hours .

By decomposing one class into multiple classes, we

can
actually adhere
single
sits in
oesponsiblity ,
where
the business
requirement one
particular
class , and request to
change those
logics comes from
one actor
only .

OPEN -
CLOSED PRINCIPLE
"

software entities such as classes, modules , functions etc ,
.

should be
open for extension
,
but closed for modification .

functionality should be
by

Any new implemented


classes, of
adding new attributes and methods, instead

changing the current ones or


existing ones .

Meyer originated

Bertrand the term OCP .

→ Robert c. Martin considered this as most important


principle .

Implementation Guidelines : -

→ The
simplest way to apply OCP is to
implement the

new
functionality on new derived classes .

→ Allow clients to class


access the
original with

abstract interface .

WHY OCP ?

Not
If followed

End
testing the entire
functionality

up
QA need test flow

Team to the entire .

linkedin.com/in/kapilyadav22

Costly Process for the
Organization
Breaks well
single roesponsibility

the as .

→ Maintenance overheads increase the classes


on .

1-

doesn't
It 's
design allow behaviour

to extend the unless

we
change the code .
for ex -

if we want to add new

sensormodel , we have to
change distance sensor class to

accommodate the new


functionality ,

by defining

we start what we want a distancesensor to

do below
in
general .
In code ,
Distance sensor is
generic, it

doesn't have a model


, therefore it doesn't make
any
sense

to concrete instance it
of
a .

Therefore we defined , Distance sensor as a


pure
abstract

class or in
javea we can
say interface .
→ when we create
specializations of
the interface for
,
each

Distance sensor model that need,


the
everytime
we
of
we want to add new sensor
,
we can create a new

child class ,
that implements the distance sensors interface
without
changing any existing code .

linkedin.com/in/kapilyadav22
LISKOV SUBSTITUTION PRINCIPLE ( LSP)
for
"
must substitutable their base
subtypes be

types ?
"

subtype of T objects of typet


→ s is then
a ,

"

may
be
replaced with objects of type
S

Derived substitutable
types be
completely

must

for their base


types .

→ LSP is a
particular definition of a
subtyping
relation , called C. ) behavioral
strong subtyping .

Introduced
by Barbara Leskov

.

→ Extension
of the
Open close principle .

Implementation Guidelines

No
exceptions be
by the
subtype
→ new can thrown .

→ clients should not know which specific subtype


they are
calling .

derived
New classes
just extend without
replacing

the
functionality of old classes .

linkedin.com/in/kapilyadav22
Kapil Yadav
Kapil Yadav

ISP Interface Segregation Principle


-

"

Dependency of one class to another should be on

smallest possible interface ?



clients should not be
forced to
implement interface
don't
they use .

fat interface

one need to be
split to
many
smaller and relevant interfaces so that clients can

know about the interfaces that are relevant

to them .


The ISP was first used and
formulated by
Robert for
c. Martin while
consulting Xerox

Case
Study
Problem ? -

had created
printer system that
→ Xerox a new

could perform a
variety of tasks such as

stapling and
faxing along with the
regular
printing task .

→ The
software for this
system was created

from the ground up .


Modifications and
Deployment to the
system
became more
complex .
SOLUTION
→ One
large Job class is
segregated to multiple
interfaces depending on the
requirement .

linkedin.com/in/kapilyadav22
public Animal
Dog
:

Animal
{ feed C);
{ feed :

Pete] ;
pet ; 3
3;

Lion :
public Animal

{ feed C) ;
pete
;✗
3

→ Pet cannot be used for Lion


,
so don't add pet function
in base class .

linkedin.com/in/kapilyadav22
Kapil Yadav

D-
Dependency inversion Principle .

"
(Interfaces)
"

Depend on Abstraction not on concrete classes .

→ level modules should not


depend low-level
High on
-

modules . Both should


depend on abstractions .


Abstractions should not depend on details . Details

should depend on
abstracl.im#- interfaces

interaction level low level


between
high and
NOTE : -
The

should be abstract
modules
thought of as an

interaction between them .

linkedin.com/in/kapilyadav22
Kapil Yadav

OOPS DESIGN PATTERN



formalised Best practices to write clean code .

Creation Factory Abstract Factory Singleton


-

, ,

structural
Bridge Adaptive, Composite
-

,
> Behavioural
Interpreter
_

, strategy ,
observer .

7)
Factory Method
Design Pattern : -

Factory Method design pattern


that define interface abstract class
stays just an or

for let subclass


creating an
object but the decide

which class to instantiate .

→ ( subclasses are
responsible to create the instances
of
the class ?
→ This pattern is also known as Virtual constructor .
Kapil Yadav

→ In above code, if user wants to add Tempo bus Metoo


, ,

info , he/she needs to add it in multiple if else ,

linkedin.com/in/kapilyadav22
Kapil Yadav

our user doesn't want to do it .

→ Now ,
we are
going to create a
factory it will
,

creation
take care
of
entire
logic without
,

it to client/ user, client will not


exposing so

bother .
Kapil Yadav

→ we created the function as static , so that we can

access the function without the


object of class .

will
logic factory if
→ Now , write and
we our in ,

client
any change required ,
will not bothered , we

only need to do
change in
factory .

Library to decide
→ be
should which
responsible obj type
create based
to on
input .

→ client should
just call
library's factory and
pass type
without
worrying
about actual
implementation of creation

of object .

Pattern :
Advantages of Factory Design -

Factory Design Pattern allows the sub-class to choose

the
type of objects to create .

loose
promotes the
couplingclasses
by eliminating the

It -

need to bind
application specific
-
into code the .

Usage of Factory Design Pattern : _


when a class doesn't know what sub-classes will
be required to create .

classes
when class wants that its sub
specify

a -

created
the
objects to be .
Kapil Yadav

Ex-2_: -

Plan
Generate bill
rate
getRate()
calculateBill()

mainC)

extends extends extends asks

Domestic Commercial Institutional GetPlanFactory


Plan Plan Plan
creates

getRate() getRate() getRate()

linkedin.com/in/kapilyadav22
Kapil Yadav

linkedin.com/in/kapilyadav22
Kapil Yadav

linkedin.com/in/kapilyadav22
Kapil Yadav

2. SINGLETON DESIGN PATTERN : -

→ New Instance Created . No .

of instances are 1

this
message
is
from user 1 .


New csnstance created .
No .

of instances are 2

this
message is from user 2 .

→ I don't want users to be able to create


my
logger object .


I want to restrict them to able to access

logger constructor .

class
1. Restrict users
from creating object of .


By making the constructor private , we are

oestoicting users
from creating object or

calling the constructor itself .


Kapil Yadav

Kapil Yadav

→ code fails In
Multithreading Case .

Suppose there are two threads


working paralleling
and
they try to create a
logger Instance ,

the first will create a


logger instance because it

is well , same second thread will also do the

same
,
so total 2 instances will be created .

linkedin.com/in/kapilyadav22
Use -
Mute , Kapil Yadav
Kapil Yadav

Our code is now safe on


multithreading but,

we don't need mutex all the time because


,

first time ,
when
logger instance = NOLL
,
and multiple
lock
threads

trying to access
loggesinstanee , we need ,
but

don't lock,
once
loggerinstance is created, we need
double
so
put check
ifcloggerinstance __=nullptD
then lock,
only

use
Kapil Yadav

Double checked
locking
-
: -

check for loggersrstance nullptr


= -

, if we
only
lock null,
need when
loggesirstance check
-_ =
a ,
so

this condition ,
because locks are
expensive .

linkedin.com/in/kapilyadav22
Kapil Yadav

[ ]

linkedin.com/in/kapilyadav22
Kapil Yadav

POINTS TO KEEP IN MIND : -

→ We want to astrict users to access the constructor


but there multiple
are
ways to access the
private
constructor

17 constructor
Make
copy as
private .

27 operator private
Make
equal to ,
overloading as .

From 01-1-11 we also need to use =delel= to


,

astrict to operations
uses
copy .

linkedin.com/in/kapilyadav22
Kapil Yadav

[ T-sxstn.cl
users

linkedin.com/in/kapilyadav22
Kapil Yadav

3 BUILDER DESIGN PATTERN : -

structure
→ whenever we are
building very complex
which has a lot
of configurations in it .

Builder Pattern that


"
construct
complex
says a

object from simple objects using step-by-step approach?


Kapil Yadav

Advantages of Builder
Design Pattern : _

clear
It provides separation between the construction

and
srepzesentation of an
object
.

better control construction


It
provides

over

process .

supports to
change the internal representation of

It

objects .

linkedin.com/in/kapilyadav22
Kapil Yadav

linkedin.com/in/kapilyadav22
Kapil Yadav

linkedin.com/in/kapilyadav22
Kapil Yadav

linkedin.com/in/kapilyadav22
Kapil Yadav

linkedin.com/in/kapilyadav22
Kapil Yadav

4. OBSERVER DESIGN PATTERN : _

"
Pattern
just define
→ observer
An that one-to-one
says
states , all its
dependency so that when one
object changes
dependents are notified and updated automatically .

Ex -

group notifications .

linkedin.com/in/kapilyadav22
Kapil Yadav
Kapil Yadav

5. Abstract
Factory Design :


Factory design pattern was
creating concrete classes
or
object .

create
→ Abstract
factory design pattern is
going to

factories that is
going
to coeale
object .

linkedin.com/in/kapilyadav22
Kapil Yadav

linkedin.com/in/kapilyadav22
Kapil Yadav

linkedin.com/in/kapilyadav22
Kapil Yadav

linkedin.com/in/kapilyadav22
Kapil Yadav

You might also like