Low Level Design: Kapil Yadav
Low Level Design: Kapil Yadav
Low Level Design: Kapil Yadav
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 : -
contains
→
usually the
objects as
argument .
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
.
→ when there is no
copy
constructor found compiler
,
its constructor
supply own
copy
.
our
→ Human Kapil =
Chitti 11 copy constructor invoked .
✓
Our
Kapil
=
chilli 11 copy constructor not invoked ✗ .
constructor invoked ✓
→ Human Kapil (chilli ) 11 copy
our
.
→
when create & in line > then
we
copy
same
copy
constructor
assignment invoked but in
copy
constructor
,
doesn't invoke .
function that
initializes an
object using another
class
object of same .
do need constructor ?
Why we
copy
→
provides
C++ default copy constructor and
assignment
class
operator with .
constructor and
write
copy assignment operator .
both the
Now
objects points to the
→
,
same
memory,
and
changes in one
reflects in another object .
→ The
Major disastrous effect is, when we delete one
of these
objects ,
another object still
points the to
same
memory ,
that
leak
will be a
dangling pointer ,
and
memory
is also
possible in this approach .
teak when
Memory create in
→ → It occurs we a
memory
heap and
forget to delete it .
int *
ptr =
( into) mallee ( sizeofcint));
→ so we
,
need to define our own
copy
constructor
only if an
object has
pointers or
any
runtime
allocation of the resources like filehandle,
a network connection , etc .
Output of above
program
will be 10 . we modified the
value of -61
using set value Go ] , but
changes also reflected
in t2 .
NOTE : -
The default copy
constructor creates
only
shallow
copy
.
→ User -
defined copy
constructor can creates Deep
copy .
→ To handle the case
,
we can either do
assignment
write
operator overloading
constructor
,
or we can
our own
copy
.
an
object and assign it with other
object
in same line .
Test E3 =
-4 ;
need
we to
assignment operator overloading .
test tics) ,
1-2 ;
tz tl
=
;
overloading assignment
→
while operator, we must check
itself may
lead to unexpected zesults .
ex -
tz =
tz ;
do self check
→ To avoid it
,
we
assignment .
{
=
{ aptr-alt.pt );
Shallow
copy shallow
object
:
In is
copy
-
an
,
→
If some variables are
dynamically allocated
memory
from heap section
,
then the copied object variable
will also
reference the same
memory
location .
will
→
This create
ambiguity and sun -
time emos
,
dangling pointer .
Also it
required allocate
dynamicallyother
→ is to
,
✗ -0 ,
y
-0
,
leading to code duplication .
Constructors .
comma -
separated list
followed by a colon .
initializes list
←
→
There are situations where
initialization of data
members inside the constructor doesn't work
and initializes list must be used .
such
Following are cases : -
-t=t ✗
3) For initialization of member objects which do not have default constructor:
→
If class one have default constructor and
,
object of
class one is data member two then there is
a
of ,
no
need
of initializes list .
→
But if default constructor is not there in class one
,
initialize
" "
we must use
initializes list to a .
4) For initialization of base class members :
parameterized class be
constructor
of base
only
→ The can
-5
"
-
I
OUTPUT:
Base parameterised constructor
Base copy constructor
Base default constructor
Base assignment operator
My class parameter constructor
→
2 3
OUTPUT:
Base parameterised constructor
Base copy constructor
Base copy constructor
My class parameter constructor
Structure Padding I
It is to
speed CPO optimization
-
way up
a .
more
empty bytes between the
memory address to
{
1
OUTPUT 12 ( not 7)
why
=
a b- -
i i i i e -
-
O 1 2 34 5 6 7 8 9 10 11
struct Base {
Chara
;
int p;
char b
;
9 - - -
P P P P b - -
-
O 1 2 34 5 6 7 8 9 10 11
I at
→ The
processor does not read
byte a time . It
I word clock
=
4
bytes .
in 1
cycle .
→
for 64-bit
processor, it seats 8
bytes at a time
,
I word =
dbytes.in 1 clock
cycle .
bit
processor then ,
the pictorial
sepxsentation memory for
the the atone
of
structure would be :
-
6
I
a
1st CPU
→ In
cycle ,
one
byte of a, one
byte of 6 ,
be
and 2
bytes of c can accessed
cycle , first 2
bytes & in second
cycle ,
another two .
want variable it
suppose only
→ we to access C
, ,
can
be access in 1 CPU
cycle ,
because its
size is
46yd,
but Here it is
taking 2 CPU
cycles .
structure
padding concept
→ to
Due this reason , was
introduced , CPU
to save the
cycles .
structure
padding automatically
→
is done by the
compiler .
data slots 9
How
gets memory
1
Byte
=
can be stored at
multiple of 1
memory
slot .
stored at multiple of
2
Byte can
slot
be
.
2
memory
4
Bytes can be stored at
multiple of 4
memory
slot .
stored
8
Bytes can be at
multiple of d
memory
slot .
How to avoid structure
Padding in C ?
→
It is an inbuilt process that is
automatically
the need avoid
done
by compiler . Sometimes we to
it as it makes the
size of structure
greater
than of structure members
the
size the .
We avoid it in two :
can
ways
-
packed directive
Using ttpoagma
°
.
Using attribute .
Abstract class : _
like ÷
class Person
{ public :
→
if i dont want to
initialize the function fun
anywhere .
Udo
void funds 03
nothing fane
__ .
A
NOTE :-
we cannot create an
object of those classes
which have do
nothing fine .
nothing function
do .
in child class .
funcO=#ñ7_
M
abstract class .
→
An abstract class in C++ can also be defined
struct
using keyword
.
struct Person {
eg .
of abstract class ]
create
object .
⑨ 2) Abstract class ?
why we use
sense / Use of
inside
it
any
.
G-✗ → Name ,
Address, Phone Number is the information
inside Person Abstract class, but
only
this is of no use .
Name Address, Phone No
Person
-
< ¥
student
Faculty
Roll
salary
no
class
,
common information will be store at
single
place .
→ Also we need to
define the purse virtual function in
child class .
Template : -
Template is a
keyword used to define
function template and class template .
→ It is a
way
to make our function or class
generalize as far as data
type is concern .
class Template :
-
class template is also known as
class
generic _
Instance class
of : -
class
type otj ;
< >
_ name
Type casting / Type conversion : _
Type casting is
basically a conversion from one
to
type another .
automatically
→
be done
It in two
by
:
can
ways
the compiler and
manually by the
programmer
.
two
types
?
There of type conversion -
are
1.
Implicit type conversion :
-
also known as
• '
automatic
type conversion .
data
automatically converted from typeto
→ It one
→ All data
type is
automatically upgraded to the
largest type without
losing any information
.
unsigned int →
long →
unsigned
loose
→ It
possible for implicit conversions to
is
implicitly converted to
unsigned ) and overflow ,
done
In C++ it can be
by two ways :
-
Conversion
by assignment
•
•
conversion
by cast operator
done
Conversion
by assignment : This is
by
-
Syntax : -
ctg.pe) expression .
operator is an
unary operator
which forces
data to another
one
type be converted into
data
type .
C++
supports four types of casting : -
1- Static cast
cast
2.
Dynamic
3- Const cast
4. Reinterpret cast
static
1. cast : -
It is a
compile-time cast that
converts data to
one
type another .
that
responsibility to ensure the conversion was
involved ?
Ans ) cast is hard to find in code but
Because C-
style ,
we
static cast
can search -
keyword .
2. Use static_cast when conversion between types is provided through conversion
operator or conversion constructor
We should typecast line 22 and 23 using static_cast as we have used in line 24 and 25
3. Static cast is more restrictive than C-Style implicit cast.
Example - char* to int* is allowed in C style implicit cast but not with static_cast.
→ B ar
I I 111
C
one
particular block ,
but after typecasting ,
we
got
nearest also , which
access to
bytes were not
allowed .
( we can
corrupt memory , segmentation fault] .
The above program will compiled successfully, that should not happen, we
have wrongly downcast, we should use dynamic cast in this case.
6. static_cast should be preferred when converting to void* OR from void*
Bottom Line:
work
dynamic cast, be
→ must
To on there one
→
works class
A dynamic - cast
only polymorphic base because
Syntax : -
→
If the cast
fails and
newtype is a
pointer type ,
→ If the cast
fails and
newtype is a reference type ,
it throws an
exception that matches a handler of
bad cast
type Std : :
_
Down
casting base class
pointer Cor reference)
:
Casting
-
to a derived class
pointer (or referenced is known as
clash
down
casting .
Base class → Derived
time
it checks object types at men using
RTTI
( Run time
type information ).
→
If that will cast to
we are sure we never
wrong
cast and
object then we should always avoid this use
static _
cast .
Const Cast ; _
used to
change
→ Const cast can be the const or
const cast (v )
Syntax
: -
_
at >
where T must be a
pointer reference
,
or
pointer -
to
-
member
type .
"
2. when we need to call some 3
party library
taking variables /
it const
where is
object as non -
but not
changing that .
reinterpret_cast
It is to convert
pointer of data type into
→ use some
a
the
pointer type .
→
It takes
only one parameter .
k Y
3
y
y Y
7-
8 C
y
to
11
→ The result
of a
reinterpret - cast cannot
→
If this cast then it
we use
type of
become non -
portable product .
x 4
y y
c y
b 4
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 .
member
functions how this
pointer
Only
.
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 .
4M£ HAT
int
IS
*
pts
MALLOC
=
new
?
int ;
,☒☒
( → Malbec ) is a function that allocates
memory
Cir
at
the runtime .
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
virtual functions of
→
we can not access non
derived base
class
through class pointer .
NOTE : -
There are non virtual function in base
class ,
which are also in derived class then
,
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