Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
SlideShare a Scribd company logo
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                2
                                      3
Step 1
         Procedural programming                          OO programming



rotate(shapeNum)                             Square
{
                                             rotate()
  //make the shape rotate 360 degrees                        Circle
                                             {
}                                               //code to rotate a square
                                                             rotate()
                                             }                              Triangle
                                                             {
playsound(shapeNum)                                             //code to rotate a circle
                                             playsound()                    rotate()
{                                            {
                                                             }
                                                                            {
  //use shapeNum to lookup which               //code to play the .wav //code to rotate a triangle
                                                             playsound()
  //WAV sound file to play and play it         // file for the square }
                                                             {
                                             }
}                                                              //code to play the .wav
                                                                            playsound()
                                                               // file for the circle
                                                                            {
                                                             }
                                                                              //code to play the .wav
                                                                              // file for the triangle
                                                                            }
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                                  4
                  2
                             3
Step 2
           Procedural programming                              OO programming

rotate(shapeNum)                               Square
{rotate(shapeNum)
 {//make the shape rotate 360 degrees          rotate()Circle
} //make the shape rotate 360 degrees          {
                                                  //code to rotate a square
                                                         rotate() Triangle
 }                                             }         {
playsound(shapeNum)                                         //code to rotateAmoeba
                                                                     rotate() a circle
{playsound(shapeNum)                           playsound()
                                                         }           {
                                               {                        //coderotate() a triangle
                                                                                  to rotate
 { //if the shape is not an amoeba use           //code to play the .wav {
                                                         playsound() }
   //shapeNum to look lookup which
   //use shapeNum to up which .wav               // file {for the square            //code to rotate the amoeba
   // file to play it to play and play it
   //WAV sound file                            }                                }
                                                           //code to play the .wav
                                                                     playsound()
// else
 }                                                         // file for the circle
                                                                     {
   //play amoeba .mp3 file                               }             //code playsound().wav
                                                                                to play the
                                                                                { the triangle
                                                                       // file for
}                                                                    }             //code to play the .mp3
                                                                                   // file for the amoeba
                                                                                }
rotate() will still work but the playsound()
will need to be changed
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
          Procedural programming                          OO programming

    rotate(shapeNum, xpt,ypt)              Square
    {
     //If the shape is not amoeba          rotate()Circle
         //calc the center point           {
                                              //code to rotate a square
                                                     rotate() Triangle
        //based on the rectangle           }         {
        //then rotate                                   //code to rotateAmoeba
                                                                 rotate() a circle
    //else                                 playsound()
                                                     }           {
                                           {                        //codeint xpt,ypt; triangle
                                                                              to rotate a
        //use the xpt and ypt as the         //code to play the .wav rotate()
                                                     playsound() }
        //rotation point offset              // file {for the square {
        //and then rotate                  }           //code to play the .wav to rotate the amoeba
                                                                 playsound()    //code
    }                                                  // file for the circle//using amoeba’s x and y
                                                                 {
                                                     }             //code }to play the .wav
                                                                   // file for the triangle
                                                                 }          playsound()
                                                                            {
•Lot of code affected                                                          //code to play the .mp3
•Previously tested code changed                                                // file for the amoeba
•What if the specification changed again                                    }
Step 4 – Common behavior
                                                                                 OO programming


Square                                                                   Amoeba

rotate()                                                                 int xpt,ypt;
{                                                                        rotate()
                     Circle
   //code to rotate a                                                    {
square                                                                     //code to rotate the
                     rotate()
}                                                                        amoeba
                     {                  Triangle                           //using amoeba’s x and y
                        //code to rotate a circle
playsound()                                                              }
                     }                  rotate()
{                                       {
  //code to play the .wav                                                playsound()
                     playsound()           //code to rotate a triangle
  // file for the square                                                 {
                     {                  }
}                                                                          //code to play the .mp3
                       //code to play the .wav
                                                                           // file for the amoeba
                       // file for the circle
                                        playsound()                      }
                     }                  {
                                          //code to play the .wav
                                          // file for the triangle
                                        }                                     What the classes have in common?
Step 5 – Base class design
       OO programming


      Shape

      rotate()
      {
        //common features
      }

      playsound()
      {
        //common features
      }




      Design the base class
Step 6 – Relationship
                OO programming

                Shape

                rotate()

                playsound()




Amoeba      Triangle                 Square                   Rectangle




              Inheritance : Base and derived class relationship
Step 6 – Overriding
                            OO programming

                            Shape

                            rotate()

                            playsound()




      Amoeba            Triangle                 Square                  Rectangle
rotate() {
//amoeba specific
//rotate code }

playSound() {        Overriding: Derived class redefines one of its inherited methods
//amoeba specific
//sound code}
                     When it needs to change or extend the behavior of that method

More Related Content

Viewers also liked

Introduction to Procedural Programming in C++
Introduction to Procedural Programming in C++Introduction to Procedural Programming in C++
Introduction to Procedural Programming in C++
Salahaddin University-Erbil
 
Oops
OopsOops
Basics of Object Oriented Programming
Basics of Object Oriented ProgrammingBasics of Object Oriented Programming
Basics of Object Oriented Programming
Abhilash Nair
 
Chapter 1
Chapter 1Chapter 1
Procedural programming
Procedural programmingProcedural programming
Procedural programming
Anbarasan Gangadaran
 
Manufacturing process of yogurt and dahi
Manufacturing process of yogurt and dahiManufacturing process of yogurt and dahi
Manufacturing process of yogurt and dahi
Najja Tariq
 
Scala: functional programming for the imperative mind
Scala: functional programming for the imperative mindScala: functional programming for the imperative mind
Scala: functional programming for the imperative mind
Sander Mak (@Sander_Mak)
 
Functional Programming Fundamentals
Functional Programming FundamentalsFunctional Programming Fundamentals
Functional Programming Fundamentals
Shahriar Hyder
 
Object-Oriented Programming Concepts
Object-Oriented Programming ConceptsObject-Oriented Programming Concepts
Object-Oriented Programming Concepts
Kwangshin Oh
 
Basic concepts of object oriented programming
Basic concepts of object oriented programmingBasic concepts of object oriented programming
Basic concepts of object oriented programming
Sachin Sharma
 
Introduction to Object Oriented Programming
Introduction to Object Oriented ProgrammingIntroduction to Object Oriented Programming
Introduction to Object Oriented Programming
Moutaz Haddara
 
Functional Programming Patterns (BuildStuff '14)
Functional Programming Patterns (BuildStuff '14)Functional Programming Patterns (BuildStuff '14)
Functional Programming Patterns (BuildStuff '14)
Scott Wlaschin
 

Viewers also liked (12)

Introduction to Procedural Programming in C++
Introduction to Procedural Programming in C++Introduction to Procedural Programming in C++
Introduction to Procedural Programming in C++
 
Oops
OopsOops
Oops
 
Basics of Object Oriented Programming
Basics of Object Oriented ProgrammingBasics of Object Oriented Programming
Basics of Object Oriented Programming
 
Chapter 1
Chapter 1Chapter 1
Chapter 1
 
Procedural programming
Procedural programmingProcedural programming
Procedural programming
 
Manufacturing process of yogurt and dahi
Manufacturing process of yogurt and dahiManufacturing process of yogurt and dahi
Manufacturing process of yogurt and dahi
 
Scala: functional programming for the imperative mind
Scala: functional programming for the imperative mindScala: functional programming for the imperative mind
Scala: functional programming for the imperative mind
 
Functional Programming Fundamentals
Functional Programming FundamentalsFunctional Programming Fundamentals
Functional Programming Fundamentals
 
Object-Oriented Programming Concepts
Object-Oriented Programming ConceptsObject-Oriented Programming Concepts
Object-Oriented Programming Concepts
 
Basic concepts of object oriented programming
Basic concepts of object oriented programmingBasic concepts of object oriented programming
Basic concepts of object oriented programming
 
Introduction to Object Oriented Programming
Introduction to Object Oriented ProgrammingIntroduction to Object Oriented Programming
Introduction to Object Oriented Programming
 
Functional Programming Patterns (BuildStuff '14)
Functional Programming Patterns (BuildStuff '14)Functional Programming Patterns (BuildStuff '14)
Functional Programming Patterns (BuildStuff '14)
 

Object oriented-programming-vs-procedural-programming

  • 1. Object Oriented Programming Vs Procedural Programming
  • 2. 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 2 3
  • 3. Step 1 Procedural programming OO programming rotate(shapeNum) Square { rotate() //make the shape rotate 360 degrees Circle { } //code to rotate a square rotate() } Triangle { playsound(shapeNum) //code to rotate a circle playsound() rotate() { { } { //use shapeNum to lookup which //code to play the .wav //code to rotate a triangle playsound() //WAV sound file to play and play it // file for the square } { } } //code to play the .wav playsound() // file for the circle { } //code to play the .wav // file for the triangle }
  • 4. 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 4 2 3
  • 5. Step 2 Procedural programming OO programming rotate(shapeNum) Square {rotate(shapeNum) {//make the shape rotate 360 degrees rotate()Circle } //make the shape rotate 360 degrees { //code to rotate a square rotate() Triangle } } { playsound(shapeNum) //code to rotateAmoeba rotate() a circle {playsound(shapeNum) playsound() } { { //coderotate() a triangle to rotate { //if the shape is not an amoeba use //code to play the .wav { playsound() } //shapeNum to look lookup which //use shapeNum to up which .wav // file {for the square //code to rotate the amoeba // file to play it to play and play it //WAV sound file } } //code to play the .wav playsound() // else } // file for the circle { //play amoeba .mp3 file } //code playsound().wav to play the { the triangle // file for } } //code to play the .mp3 // file for the amoeba } rotate() will still work but the playsound() will need to be changed
  • 6. 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
  • 7. 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
  • 8. Step 3 Procedural programming OO programming rotate(shapeNum, xpt,ypt) Square { //If the shape is not amoeba rotate()Circle //calc the center point { //code to rotate a square rotate() Triangle //based on the rectangle } { //then rotate //code to rotateAmoeba rotate() a circle //else playsound() } { { //codeint xpt,ypt; triangle to rotate a //use the xpt and ypt as the //code to play the .wav rotate() playsound() } //rotation point offset // file {for the square { //and then rotate } //code to play the .wav to rotate the amoeba playsound() //code } // file for the circle//using amoeba’s x and y { } //code }to play the .wav // file for the triangle } playsound() { •Lot of code affected //code to play the .mp3 •Previously tested code changed // file for the amoeba •What if the specification changed again }
  • 9. Step 4 – Common behavior OO programming Square Amoeba rotate() int xpt,ypt; { rotate() Circle //code to rotate a { square //code to rotate the rotate() } amoeba { Triangle //using amoeba’s x and y //code to rotate a circle playsound() } } rotate() { { //code to play the .wav playsound() playsound() //code to rotate a triangle // file for the square { { } } //code to play the .mp3 //code to play the .wav // file for the amoeba // file for the circle playsound() } } { //code to play the .wav // file for the triangle } What the classes have in common?
  • 10. Step 5 – Base class design OO programming Shape rotate() { //common features } playsound() { //common features } Design the base class
  • 11. Step 6 – Relationship OO programming Shape rotate() playsound() Amoeba Triangle Square Rectangle Inheritance : Base and derived class relationship
  • 12. Step 6 – Overriding OO programming Shape rotate() playsound() Amoeba Triangle Square Rectangle rotate() { //amoeba specific //rotate code } playSound() { Overriding: Derived class redefines one of its inherited methods //amoeba specific //sound code} When it needs to change or extend the behavior of that method