Professional Documents
Culture Documents
Low Level Design
Low Level Design
Low Level Design
Level
= Design
- Kapil Yadav
- OOPs concepts
- Design Principles
- Design Patterns
Kapil Yadav
-
INDEX
1 .
Classes and
objects .
→
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
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
→
.
,
→ class is a
logical component .
→
Object takes
memory from heap .
linkedin.com/in/kapilyadav22
Kapil Yadav
linkedin.com/in/kapilyadav22
Kapil Yadav
Data
Hiding
: -
Data
hiding refers to
restricting access
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
-
accessible
The
public members from part of the
•
are
any
program .
linkedin.com/in/kapilyadav22
Kapil Yadav
linkedin.com/in/kapilyadav22
Kapil Yadav
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 .
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
linkedin.com/in/kapilyadav22
Kapil Yadav
Kapil Yadav
initialize data
objects
→
It is used to the members new
of .
return
Constructors don't have
type
→
any
.
→
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++
→
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 : -
variables in a class
objects
in a class .
not
object .
class Human {
Human C)
{ count tt ;
y
3; 11 initialize static member of class Human
int main C)
E coat <<
Human : : count -
Lendl ;
ng
object ?
Anis .
Yes .
linkedin.com/in/kapilyadav22
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 .
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 .
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 ,
→ 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 .
variable
Type datatype of
• -
a .
•
variable -
name of the variable .
parameter - list -
initialized to a variable .
int *
pts =
new int ;
WHAT IS MALLOC ?
→ Malbec ) is function that allocates at
a
memory
the runtime .
→
Syntax
-
Ctypetamalloclsizeofctype));
variable
type it és the
datatype of
•
: the .
•
variable -
name : it defines the name of the variable
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
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
1. New
operator construct 1 . Mallee is a
function ,
it does
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
5-
If sufficient memory is not 5 . Malloch ) will seteem a NULL
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 .
linkedin.com/in/kapilyadav22
Kapil Yadav
FINAL
keyword : _
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 function arguments
•
Use const with class member functions
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 will
alwaysis point to the same address
pointer .
linkedin.com/in/kapilyadav22
Kapil Yadav
Const into a;
]→ both pointer
are
to
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
- -
'
→ •
into ?
from
'
error : -
invalid conversion const intro to
const value to
integer us .
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
Otherwise
,
we will face the error .
"
linkedin.com/in/kapilyadav22
Kapil Yadav
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
MOM DAD
cH%D
classes
There Be
ambiguity the Base
→
can ,
when
use
scope resolution operator .
scope
g→ oesolution
operator
Kapil Yadav
Hybrid ?
-
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
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
✓ ☒ ✓
Virtual
Function Operator
Functions
Overloading Overloading
1. Compile-time polymorphism : -
This
type of polymorphism
is achieved
by function overloading or
operator
overloading .
Function
Overloading
• : -
overloaded .
Functions be overloaded
by
→ •
can .
OR / AND
Kapil Yadav
changing the
type of arguments
•
.
linkedin.com/in/kapilyadav22
Kapil Yadav
linkedin.com/in/kapilyadav22
Kapil Yadav
Operator Overloading : -
with
C++ has
ability
the to
provide the operators a
as operator overloading
.
→
To overload an
operator a operator function
,
is defined
inside a class .
class class
{
-
name
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
Rules →
Must do inheritance .
→ same
function name / same
parameter .
Function ?
Virtual A virtual function is member
-
function ,
which is declare within a base class and
Run-time
They mainly use to achieve
→ are
Points to remember ? -
3 .
They are
always defined in the base class and
class (
may / maynot
overridden in a derived . we
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
→
working of virtual functions (concepts of VTABZE and
VPTR) .
object of a
pointer is inherited
by derived
classes . .
Irrespective of not ,
2 .
object is created or
Virtual Table -
A table created at compile time for
derived
every single class
containing the most versions
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 :-
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
OUTPUT ? -
constructing base
derived
constructing
derived
Destructing
Destructing base
Kapil Yadav
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 -
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 : -
should be labeled as
public using the public access specifier .
linkedin.com/in/kapilyadav22
Kapil Yadav
details
and
hiding the internal
C++
provides great level of abstraction for
•
a . ex -
••
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 .
→ Increases Zeus
ability .
→
Implementation details class
of the are
protected from
the inadvertent user level errors .
linkedin.com/in/kapilyadav22
Kapil Yadav ①
Clean code ? →
easily readable
by other
developers .
Why ?
How ?
→
meaningful variable name
, function ,
class etc .
→ comments .
→ Modular
→
functions
→
short,
DESIGN PRINCIPLES : _
DRY Don't
repeat yourself
→ :
.
→ Kiss
keep it simple stupid
:
.
→
Abstraction :
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
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 .
are .
linkedin.com/in/kapilyadav22
?⃝
Kapil Yadav
③ YAGNI : -
You ain't
gonna
need it .
"
→
Don't
engineering
Do over .
Benefits : -
•
Slave time .
•
concise code .
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
→ Maximum cohesion .
Coupling
: -
It is the degree to which each module depend
on other modules .
HUMAN → CAR
→
Required low
coupling .
Command -
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 .
code ) should
package ,
source have a reason to
change by
only one actor .
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
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 .
It is
voélating Single
Responsibility Principle .
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
→
Meyer originated
→
Bertrand the term OCP .
Implementation Guidelines : -
→ The
simplest way to apply OCP is to
implement the
new
functionality on new derived classes .
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 .
1-
doesn't
It 's
design allow behaviour
→
to extend the unless
we
change the code .
for ex -
sensormodel , we have to
change distance sensor class to
by defining
→
we start what we want a distancesensor to
do below
in
general .
In code ,
Distance sensor is
generic, it
to concrete instance it
of
a .
class or in
javea we can
say interface .
→ when we create
specializations of
the interface for
,
each
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 ?
"
"
may
be
replaced with objects of type
S
Derived substitutable
types be
completely
→
must
→ 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 .
derived
New classes
just extend without
replacing
→
the
functionality of old classes .
linkedin.com/in/kapilyadav22
Kapil Yadav
Kapil Yadav
"
fat interface
→
one need to be
split to
many
smaller and relevant interfaces so that clients can
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
→
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
linkedin.com/in/kapilyadav22
Kapil Yadav
D-
Dependency inversion Principle .
"
(Interfaces)
"
→
Abstractions should not depend on details . Details
should depend on
abstracl.im#- interfaces
should be abstract
modules
thought of as an
linkedin.com/in/kapilyadav22
Kapil Yadav
, ,
structural
Bridge Adaptive, Composite
-
,
> Behavioural
Interpreter
_
, strategy ,
observer .
7)
Factory Method
Design Pattern : -
→ ( subclasses are
responsible to create the instances
of
the class ?
→ This pattern is also known as Virtual constructor .
Kapil Yadav
linkedin.com/in/kapilyadav22
Kapil Yadav
→ Now ,
we are
going to create a
factory it will
,
creation
take care
of
entire
logic without
,
bother .
Kapil Yadav
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 -
the
type of objects to create .
loose
promotes the
couplingclasses
by eliminating the
•
It -
need to bind
application specific
-
into code the .
•
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)
linkedin.com/in/kapilyadav22
Kapil Yadav
linkedin.com/in/kapilyadav22
Kapil Yadav
linkedin.com/in/kapilyadav22
Kapil Yadav
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 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
Kapil Yadav
→ code fails In
Multithreading Case .
same
,
so total 2 instances will be created .
linkedin.com/in/kapilyadav22
Use -
Mute , Kapil Yadav
Kapil Yadav
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
-
: -
, 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
17 constructor
Make
copy as
private .
27 operator private
Make
equal to ,
overloading as .
astrict to operations
uses
copy .
linkedin.com/in/kapilyadav22
Kapil Yadav
[ T-sxstn.cl
users
linkedin.com/in/kapilyadav22
Kapil Yadav
structure
→ whenever we are
building very complex
which has a lot
of configurations in it .
Advantages of Builder
Design Pattern : _
clear
It provides separation between the construction
•
and
srepzesentation of an
object
.
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
"
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