IS 17 - PowerPoint PPT Presentation

1 / 16
About This Presentation
Title:

IS 17

Description:

Killer vultures (drop exploding eggs) Celestial beasts (devour astronauts) You get the picture. ... Bombs drop according to gravity. You get the picture... IS 17 ... – PowerPoint PPT presentation

Number of Views:69
Avg rating:3.0/5.0
Slides: 17
Provided by: sweval
Category:
Tags: dropped | egg

less

Transcript and Presenter's Notes

Title: IS 17


1
IS 17
  • Interfaces Polymorphism
  • Conceptual Example

2
IS 17
  • Youre 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

3
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

4
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

5
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
  • ArrayListltKlingongt
  • ArrayListltAstronautgt
  • ArrayListltBulletgt
  • You get the picture.

6
IS 17
  • Option 1 contd
  • Write a for loop for each ArrayListlt..gt
  • 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!

7
IS 17
  • Design Option 2
  • Use a single ArrayList that contains elements of
    type Object
  • ArrayListltObjectgt
  • 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
    loop.

8
IS 17
  • Option 2 contd Loop pseudocode
  • for(Object o AnimatedEntities)
  • if o is Klingon, call Klingons move()
  • else if o is Bullet, call Bullets move()
  • else if o is Astronaut, call Astronauts move()
  • ..
  • You get the picture!

9
IS 17
  • Option 2 contd
  • Whats 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!

10
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
    interfaces methods
  • Use polymorphism to treat all animated entities
    as a common interface type

11
IS 17
  • Option 3 contd
  • Step 1 Declare the interface type
  • public interface Movable
  • public void move()

12
IS 17
  • Option 3 contd
  • 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.

13
IS 17
  • Option 3 contd
  • Step 3 Place all animated entities in an
    ArrayList typed to the interface type they have
    in common
  • ArrayListltMovablegt animatedEntities
  • new ArrayListltMovablegt()
  • animatedEntities.add(new Klingon(..))
  • animatedEntities.add(new Astronaut(..))
  • .

14
IS 17
  • Option 3 contd
  • 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)
  • m.move()
  • If m is type Klingon, will call Klingons move()
  • If m is type Astronaut, will call Astronauts
    move()

15
IS 17
  • Option 3 contd
  • 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!)

16
IS 17
Write a Comment
User Comments (0)
About PowerShow.com