Vous êtes sur la page 1sur 18

PROCEDURAL PROGRAMMING VS

OBJECT ORIENTED PROGRAMMING

Zara Tariq
Demo Lecture #01
Programmer
B love
working with
OOP.

Programmer
A only work
with PP.

Zara Tariq-2019 PAF-KIET 2


TASK TO BUILD AN APPLICATION
There will be following shapes on
GUI:
1. CIRCLE
2. SQUARE
3. TRIANGLE

When user click on any shape, it will


rotate to 360 degree and play wav
sound on that particular shape.

Zara Tariq-2019 PAF-KIET 3


Programmer A thought,

What procedures do we need?

Program Specs:
- Rotate 360 degree
- Play sound

After all, What is a program in Procedural


Programming?
Pile of procedures
Zara Tariq-2019 PAF-KIET 4
Programmer B think of KEY PLAYER,

+ SHAPES
+ USERS
HAVE
+ SOUND LIBRARY
+ CLICKING EVENT

Zara Tariq-2019 PAF-KIET 5


Programmer A (PP) Programmer B(OOP)
wrote a code in NO TIME wrote 3 classes
Square
Rotate(shapeNum)
Rotate()
{ { Circle
// make the shape rotate 360 // make the square rotate 360
degree Rotate()
degree } Triangle
{
} // make the circle rotate 360
playsound()
degree Rotate()
{ } {
playsound(shapeNum) // play the square WAV// make
soundthe triangle rotate 360
{ } playsound() degree
{ }
// use the shapeNum to // play the circle WAV sound
playsound()
lookup which WAV sound }
{
file to play and play it // play the triangle WAV sound
}
}
Zara Tariq-2019 PAF-KIET 6
I have nailed it!
Manager changed some specifications

NEW SPECS:
- Add New Shape – AMOEBA
- On user click,
- - Shape rotate to 360 degree
- - Play .mp3 sound file.

Zara Tariq-2019 PAF-KIET 7


Programmer A (PP) Programmer B(OOP)
added a code in NO TIME Added 1 new class
Square
Rotate(shapeNum)
{ Rotate()
Circle
// make the shape rotate 360 degree {
} // make theTriangle
Rotate() square rotate 360
// degree
{
} // make Rotate()
theAmoeba
circle rotate 360
playsound(shapeNum) {
// degree
{ playsound()
} // make the triangle rotate 360
Rotate()
// if the shape is not amoeba { // degree
{
// play }
the square
playsound() WAV the
// make sound
amoeba rotate 360
// use the shapeNum to
} { // degree
// lookup which WAV sound playsound()
// play the }circle WAV sound
// file to play and play it } {
//else // playplaysound()
the triangle WAV sound
} {
//play amoeba .mp3 file
// play the amoeba .mp3 sound
} }

Zara Tariq-2019 PAF-KIET 8


Programmer A with Programmer B(OOP)
PP approach
WHENEVER THERE IS ANY CHANGE IN PROGRAM

- Must make changes in the - Only have to create a new class


entire program - Time & resource efficient
- Run already tested program
repeatedly
- Time & resource costly

Zara Tariq-2019 PAF-KIET 9


Implementation Issue
The logic for rotating functions was
implemented as
- Determine the rectangle that
surrounds the shape
- Calculate the center of shape and
rotate the shape around that point.

Zara Tariq-2019 PAF-KIET 10


Actual logic that client wanted
- For the amoeba shape the client wanted the rotate to be
implemented differently

- The amoeba shape was supposed to rotate around a point at one


end.

Implemented Required
Zara Tariq-2019 PAF-KIET 11
Programmer A (PP) Programmer B (OOP)
Square
Rotate(shapeNum) Rotate()
{
Circle
// if the shape is not amoeba,
{
//calculate the center point and rotate // make theTriangle
Rotate() square rotate 360
//else // degree
{ Amoeba
// use the xPt and yPt as the rotation point} // make Rotate()
the circle{ rotate 360
}
//offset and then rotate Which approach is// more
{
// degree
make the
Int xpt,ypt;
triangle rotate 360
efficient?
playsound()
} Public:
{ // degree
Rotate()
Changing 1 class OR { sound
playsound(shapeNum) }
{
// play the square
playsound() WAV

Editing the entire file?


// if the shape is not amoeba use the shapeNum } { // make the amoeba rotate 360
// to lookup which WAV sound playsound()
// play the circle//WAV sound
degree using xp and yp
// file to play and play it } {
}
//else // play the triangle WAV sound
//play amoeba .mp3 file }
}
playsound()
{
// play the amoeba .mp3 sound
}
Zara Tariq-2019 PAF-KIET 12
Programmer A claim
that Programmer B
duplicated the code
in all 4 shapes

Programmer B
explains that these
are classes and not
procedure and he
did no duplications

Zara Tariq-2019 PAF-KIET 13


Circle
Amoeba
Square
Rotate() {
{ Int xpt, ypt;
Rotate()
// make the circle rotate 360 Public:
{
// degree Rotate()
// make the square rotate 360
} {
// degree
// make the amoeba rotate 360 degree
}
playsound() // using xp and yp
{ }
playsound()
// play the circle WAV sound {
} playsound()
// play the square WAV sound
Triangle {
}
// play the amoeba .mp3 sound
Rotate() }
{
// make the triangle rotate 360
// degree
}
What the classes have in
playsound() common?
{
// play the triangle WAV sound
}
Zara Tariq-2019 PAF-KIET 14
Shape Class
They are shapes and they rotate and play sound
Circle

rotate() So he abstracted out common features


{
// Common features
}
SHAPE
playsound() rotate()
{
// Common features
playSound()
}

Base/Parent class Amoeba Square Rectangle Triangle

Inheritance: Base & Derived Class Relationship

Zara Tariq-2019 PAF-KIET 15


SHAPE
rotate()
playSound()

Square Rectangle Triangle


Amoeba
Rotate(){
// Amoeba specific
Overriding:
// rotate code }
Derived class redefines one of it’s
playSound(){ inherited method when it needs to
// Amoeba specific
change the behavior of that method.
// playsound code }

Zara Tariq-2019 PAF-KIET 16


Programming with Programming with OO
Procedural
Advantages Advantages
- Relatively to implement - Easily understandable
- Easier way to track program flow - Easily extended as it is real world
- Need less memory model
- Offers reusability by using already
Disadvantages: created classes
- Data is exposed to whole program - Easier to test and maintained
- Security risks are high
- No reusability of code Disadvantages:
- Difficult to relate to real world - Needs proper planning in the
objectives beginning
- Difficult to create new data types and - Program designing is sometimes
reduces extensibility tricky

Zara Tariq-2019 PAF-KIET 17


Any Questions

Vous aimerez peut-être aussi