Object Oriented Programming Vs Procedural Programming
Requirement Spec There will be a shape on a GUI, a square, a circle and a triangle When the user clicks on the shape, the shape will rotate clockwise 360 degrees and play a .wav sound file specific to that shape 1 3 2
Step 1 r otate( shapeNum ) { //make the shape rotate 360 degrees } p laysound ( shapeNum ) { //use shapeNum to lookup which //WAV sound file to play and play it } Square r otate() { //code to rotate a square } p laysound () { //code to play the .wav // file for the square } Circle r otate() { //code to rotate a circle } p laysound () { //code to play the .wav // file for the circle } Triangle r otate() { //code to rotate a triangle } p laysound () { //code to play the .wav // file for the triangle } Procedural programming OO programming
Change in Spec A new shape is added – amoeba shape along with the others When the user clicks on the amoeba, the shape will rotate clockwise 360 degrees and play a .mp3 sound file and not a .wav file 1 3 2 4
Step 2 r otate( shapeNum ) { //make the shape rotate 360 degrees } p laysound ( shapeNum ) { //use shapeNum to lookup which //WAV sound file to play and play it } Square r otate() { //code to rotate a square } p laysound () { //code to play the .wav // file for the square } Circle r otate() { //code to rotate a circle } p laysound () { //code to play the .wav // file for the circle } Triangle r otate() { //code to rotate a triangle } p laysound () { //code to play the .wav // file for the triangle } Procedural programming OO programming r otate() will still work but the playsound () w ill need to be changed r otate( shapeNum ) { //make the shape rotate 360 degrees } p laysound ( shapeNum ) { //if the shape is not an amoeba use // shapeNum to look up which .wav // file to play it // else //play amoeba .mp3 file } Amoeba r otate() { //code to rotate the amoeba } p laysound () { //code to play the .mp3 // file for the amoeba }
Issue with Implementation The logic for the rotate function was implemented as: Determine the rectangle that surrounds the shape Calculate the center of the rectangle and rotate the shape around that point
Actual logic that client wanted For the amoeba shape the client wanted the rotate to be implemented differently The amoeba shape was suppose to rotate around a point at one end implemented required
Step 3 Square r otate() { //code to rotate a square } p laysound () { //code to play the .wav // file for the square } Circle r otate() { //code to rotate a circle } p laysound () { //code to play the .wav // file for the circle } Triangle r otate() { //code to rotate a triangle } p laysound () { //code to play the .wav // file for the triangle } Procedural programming OO programming Lot of code affected Previously tested code changed What if the specification changed again rotate( shapeNum , xpt,ypt ) { //If the shape is not amoeba //calc the center point //based on the rectangle //then rotate //else //use the xpt and ypt as the //rotation point offset //and then rotate } Amoeba i nt xpt,ypt ; rotate() { //code to rotate the amoeba // using amoeba’s x and y } p laysound () { //code to play the .mp3 // file for the amoeba }
Step 4 – Common behavior Square r otate() { //code to rotate a square } p laysound () { //code to play the .wav // file for the square } Circle r otate() { //code to rotate a circle } p laysound () { //code to play the .wav // file for the circle } Triangle r otate() { //code to rotate a triangle } p laysound () { //code to play the .wav // file for the triangle } OO programming Amoeba i nt xpt,ypt ; rotate() { //code to rotate the amoeba //using amoeba’s x and y } p laysound () { //code to play the .mp3 // file for the amoeba } What the classes have in common?
Step 5 – Base class design OO programming Shape rotate() { //common features } p laysound () { //common features } Design the base class
Step 6 – Relationship OO programming Shape rotate() p laysound () Inheritance : Base and derived class relationship Amoeba Triangle Rectangle Square
Step 6 – Overriding OO programming Shape rotate() p laysound () Overriding : Derived class redefines one of its inherited methods When it needs to change or extend the behavior of that method Amoeba r otate() { //amoeba specific //rotate code } playSound () { //amoeba specific //sound code} Triangle Rectangle Square