IS 17

Document Sample
IS 17 Powered By Docstoc
					         IS 17

Interfaces & Polymorphism

     Conceptual Example
                              IS 17

• You’re implementing an animated interactive game

• Game is total combat involving
   –   Klingons (evil guys), Astronauts (good guys)
   –   Spacecraft
   –   Particle beam weapons
   –   Bullets (various types)
   –   Bombs (various types)
   –   Killer vultures (drop exploding eggs)
   –   Celestial beasts (devour astronauts)
   –   You get the picture……
                          IS 17

• Big programming challenge is moving all the active
  players (e.g., Klingons, bullets, etc) around the screen as
  they engage in combat

• Being OO aficionados, we implement a class for each
  type of animated entity
                               IS 17

• All classes have the common behavior of moving across
  the screen (in x-y space)
   – Each class needs move()in its public interface

• All move according to different algorithms, so each class
  has a different implementation of move()
   –   Astronauts bounce
   –   Spacecraft zig-zag
   –   Bullets have a parabolic trajectory
   –   Bombs drop according to gravity
   –   You get the picture…
                                   IS 17

• For slick animation, we need to tell each animated entity
  to move itself every 0.1 second

• What are our options?

• Design option 1
   – Implement a separate ArrayList for each class of animated entity
       •   ArrayList<Klingon>
       •   ArrayList<Astronaut>
       •   ArrayList<Bullet>
       •   You get the picture….
                            IS 17

• Option 1 cont’d
   – Write a for loop for each ArrayList<..>
   – Iterate through the series of for loops, telling each animated
     entity to move itself
   – Requires lots of ArrayLists and for loops
   – If we want to add anymore animated entities to our game, we
     have to maintain our code by adding more ArrayLists and loops!
                                IS 17

• Design Option 2:
   – Use a single ArrayList that contains elements of type Object
       • ArrayList<Object>
       • Every class is of type Object

   – Problem: the move method is not defined in Object class and
     each object in ArrayList needs to invoke its own move method,
     we have to put conditional logic that checks for types in our
                          IS 17

• Option 2 cont’d: Loop pseudocode

   for(Object o: AnimatedEntities){

       if o is Klingon, call Klingon’s move()

       else if o is Bullet, call Bullet’s move()

       else if o is Astronaut, call Astronaut’s move()

   You get the picture!
                             IS 17

• Option 2 cont’d
   – What’s so bad about this you ask?
   – We have to write lots of conditional code
       • Easy to make mistakes

   – We have to maintain the conditional code every time we add or
     delete animated entities!

   – Classic example of brittle code!

   – Key point: Design programs to minimize conditional code that
     branches based on type checking. Switch statements are
     common offender!
                              IS 17

• Design Option 3 (The right choice!)
   – Use interfaces & polymorphism to reduce amount of code,
     making program easy to understand and maintain! How do we
     do this?

   – Create an interface that defines a type based on a common
     behavior, i.e., movability

   – Make each class implement this interface
      • Of course, each class must implement the interface’s methods

   – Use polymorphism to treat all animated entities as a common
     interface type
                             IS 17

• Option #3 cont’d:
   – Step 1: Declare the interface type

     public interface Movable{

         public void move();

                             IS 17

• Option #3 cont’d:
   – Step 2: Make each class implement the interface, e.g.,

     public class Klingon implements Movable{

           public void move(){

             //code that moves Klingons!
           …other Klingon methods….

                              IS 17

• Option #3 cont’d:
   – Step 3: Place all animated entities in an ArrayList typed to the
     interface type they have in common

     ArrayList<Movable> animatedEntities =
                         new ArrayList<Movable>();

     animatedEntities.add(new Klingon(..));
     animatedEntities.add(new Astronaut(..));
                              IS 17

• Option #3 cont’d:
   – Step 4: To move all animated entities, iterate through the loop
     and rely on polymorphism to call the proper implementation of
     move() based on the actual type of each element

     for(Movable m: animatedEntities) {


   – If m is type Klingon, will call Klingon’s move()
   – If m is type Astronaut, will call Astronaut’s move()
                            IS 17

• Option #3 cont’d:
   – What could be easier?
   – Only 1 ArrayList needed
   – No conditional logic needed in ArrayList
   – Can add new kinds of animated entities without having to modify
     either the ArrayList or the loop, as long as each new class
     implements Movable interface
   – Code is much more robust!
   – Less code maintenance
   – Code easier to understand (assuming you know interfaces!)
IS 17

Shared By: