Paradigmas de Linguagens de Programação Paradigma Orientado a Objetos Aula #7 (CopyLeft)2009 - Ismar Frango ismar@mackenzie.br
“ There are only two kinds of languages: the ones people complain about and the ones nobody uses.” Bjarne Stroustrup Binding em LOO
 
 
22/09/09
 
invariância covariância contravariância   
 
 
22/09/09 ?
?
Asteroid SpaceShip Thing Dispatching em LOO
abstract class   Thing  { public abstract void   collideWith ( Thing  other); } class   Asteroid  extends  Thing  { public void  collideWith ( Thing  other) { if (other  instanceof   Asteroid ) { // handle Asteroid-Asteroid collision } else if (other  instanceof   Spaceship ) { // handle Asteroid-Spaceship collision } } } class   Spaceship  extends  Thing  { public void   collideWith ( Thing  other) { if  (other  instanceof   Asteroid ) { // handle Spaceship-Asteroid collision } else  if (other  instanceof   Spaceship ) { // handle Spaceship-Spaceship collision } } }
abstract class  Thing { public abstract void   collideWith ( Thing  other); } class  Asteroid  extends  Thing { public void   collideWith ( Asteroid  other) { System.out.println("handle Asteroid-Asteroid collision"); } public void   collideWith ( Spaceship  s) { System.out.println("handle Asteroid-Spaceship collision"); } } class Spaceship extends Thing { public void   collideWith ( Asteroid  a) { System.out.println("handle Spaceship-Asteroid collision"); } public void   collideWith ( Spaceship  other) { System.out.println("handle Spaceship-Spaceship collision"); } } 
abstract class  Thing { public abstract void   collideWith ( Thing  other); } class  Asteroid  extends  Thing { public void   collideWith ( Thing  other) {} public void   collideWith ( Asteroid  other) { System.out.println("handle Asteroid-Asteroid collision"); } public void   collideWith ( Spaceship  s) { System.out.println("handle Asteroid-Spaceship collision"); } } class Spaceship extends Thing { public void   collideWith ( Thing  other) {} public void   collideWith ( Asteroid  a) { System.out.println("handle Spaceship-Asteroid collision"); } public void   collideWith ( Spaceship  other) { System.out.println("handle Spaceship-Spaceship collision"); } } 
Asteroid ExplodingAsteroid SpaceShip GiantSpaceShip Thing
class   SpaceShip  {}; class   GiantSpaceShip  :  public  SpaceShip {}; class   Asteroid  { public : virtual void   CollideWith (SpaceShip&) { cout << &quot;Asteroid hit a SpaceShip&quot; << endl; } virtual void   CollideWith (GiantSpaceShip&) { cout << &quot;Asteroid hit a GiantSpaceShip&quot; << endl; } }; class   ExplodingAsteroid  :  public   Asteroid  { public : virtual void   CollideWith (SpaceShip&) { cout << &quot;ExplodingAsteroid hit a SpaceShip&quot; << endl; } virtual void   CollideWith (GiantSpaceShip&) { cout << &quot;ExplodingAsteroid hit a GiantSpaceShip&quot; << endl; } };
class   SpaceShip  {}; class   GiantSpaceShip  :  public  SpaceShip {}; class   Asteroid  { public : virtual void   CollideWith (SpaceShip&) { cout << &quot;Asteroid hit a SpaceShip&quot; << endl; } virtual void   CollideWith (GiantSpaceShip&) { cout << &quot;Asteroid hit a GiantSpaceShip&quot; << endl; } }; class   ExplodingAsteroid  :  public   Asteroid  { public : virtual void   CollideWith (SpaceShip&) { cout << &quot;ExplodingAsteroid hit a SpaceShip&quot; << endl; } virtual void   CollideWith (GiantSpaceShip&) { cout << &quot;ExplodingAsteroid hit a GiantSpaceShip&quot; << endl; } }; Asteroid  theAsteroid;  SpaceShip  theSpaceShip;  GiantSpaceShip  theGiantSpaceShip; theAsteroid . CollideWith (theSpaceShip);  theAsteroid . CollideWith (theGiantSpaceShip);   Asteroid * theAsteroidReference =  new   ExplodingAsteroid ();  theAsteroidReference -> CollideWith (theSpaceShip);  theAsteroidReference -> CollideWith (theGiantSpaceShip);  SpaceShip & theSpaceShipReference = theGiantSpaceShip;  theAsteroid . CollideWith (theSpaceShipReference);  theAsteroidReference -> CollideWith (theSpaceShipReference);
class   SpaceShip   { virtual void   CollideWith ( Asteroid & inAsteroid)  { inAsteroid. CollideWith (* this ); }  }; class   GiantSpaceShip  :  public  SpaceShip {}; Asteroid hit a SpaceShip ExplodingAsteroid hit a SpaceShip SpaceShip & theSpaceShipReference = theGiantSpaceShip;  Asteroid & theAsteroidReference = theExplodingAsteroid;  theSpaceShipReference. CollideWith (theAsteroid);  theSpaceShipReference. CollideWith (theAsteroidReference);   class   SpaceShip   { virtual void   CollideWith ( Asteroid & inAsteroid)  { inAsteroid. CollideWith (* this ); }  }; class   GiantSpaceShip  :  public  SpaceShip  { virtual void   CollideWith ( Asteroid & inAsteroid)  { inAsteroid. CollideWith (* this ); }  }; Asteroid hit a GiantSpaceShip ExplodingAsteroid hit a GiantSpaceShip
O Padrão Visitor

Paradigmas de Linguagens de Programacao - Aula #7

  • 1.
    Paradigmas de Linguagensde Programação Paradigma Orientado a Objetos Aula #7 (CopyLeft)2009 - Ismar Frango ismar@mackenzie.br
  • 2.
    “ There areonly two kinds of languages: the ones people complain about and the ones nobody uses.” Bjarne Stroustrup Binding em LOO
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
    Asteroid SpaceShip ThingDispatching em LOO
  • 13.
    abstract class Thing { public abstract void collideWith ( Thing other); } class Asteroid extends Thing { public void collideWith ( Thing other) { if (other instanceof Asteroid ) { // handle Asteroid-Asteroid collision } else if (other instanceof Spaceship ) { // handle Asteroid-Spaceship collision } } } class Spaceship extends Thing { public void collideWith ( Thing other) { if (other instanceof Asteroid ) { // handle Spaceship-Asteroid collision } else if (other instanceof Spaceship ) { // handle Spaceship-Spaceship collision } } }
  • 14.
    abstract class Thing { public abstract void collideWith ( Thing other); } class Asteroid extends Thing { public void collideWith ( Asteroid other) { System.out.println(&quot;handle Asteroid-Asteroid collision&quot;); } public void collideWith ( Spaceship s) { System.out.println(&quot;handle Asteroid-Spaceship collision&quot;); } } class Spaceship extends Thing { public void collideWith ( Asteroid a) { System.out.println(&quot;handle Spaceship-Asteroid collision&quot;); } public void collideWith ( Spaceship other) { System.out.println(&quot;handle Spaceship-Spaceship collision&quot;); } } 
  • 15.
    abstract class Thing { public abstract void collideWith ( Thing other); } class Asteroid extends Thing { public void collideWith ( Thing other) {} public void collideWith ( Asteroid other) { System.out.println(&quot;handle Asteroid-Asteroid collision&quot;); } public void collideWith ( Spaceship s) { System.out.println(&quot;handle Asteroid-Spaceship collision&quot;); } } class Spaceship extends Thing { public void collideWith ( Thing other) {} public void collideWith ( Asteroid a) { System.out.println(&quot;handle Spaceship-Asteroid collision&quot;); } public void collideWith ( Spaceship other) { System.out.println(&quot;handle Spaceship-Spaceship collision&quot;); } } 
  • 16.
  • 17.
    class SpaceShip {}; class GiantSpaceShip : public SpaceShip {}; class Asteroid { public : virtual void CollideWith (SpaceShip&) { cout << &quot;Asteroid hit a SpaceShip&quot; << endl; } virtual void CollideWith (GiantSpaceShip&) { cout << &quot;Asteroid hit a GiantSpaceShip&quot; << endl; } }; class ExplodingAsteroid : public Asteroid { public : virtual void CollideWith (SpaceShip&) { cout << &quot;ExplodingAsteroid hit a SpaceShip&quot; << endl; } virtual void CollideWith (GiantSpaceShip&) { cout << &quot;ExplodingAsteroid hit a GiantSpaceShip&quot; << endl; } };
  • 18.
    class SpaceShip {}; class GiantSpaceShip : public SpaceShip {}; class Asteroid { public : virtual void CollideWith (SpaceShip&) { cout << &quot;Asteroid hit a SpaceShip&quot; << endl; } virtual void CollideWith (GiantSpaceShip&) { cout << &quot;Asteroid hit a GiantSpaceShip&quot; << endl; } }; class ExplodingAsteroid : public Asteroid { public : virtual void CollideWith (SpaceShip&) { cout << &quot;ExplodingAsteroid hit a SpaceShip&quot; << endl; } virtual void CollideWith (GiantSpaceShip&) { cout << &quot;ExplodingAsteroid hit a GiantSpaceShip&quot; << endl; } }; Asteroid theAsteroid; SpaceShip theSpaceShip; GiantSpaceShip theGiantSpaceShip; theAsteroid . CollideWith (theSpaceShip); theAsteroid . CollideWith (theGiantSpaceShip); Asteroid * theAsteroidReference = new ExplodingAsteroid (); theAsteroidReference -> CollideWith (theSpaceShip); theAsteroidReference -> CollideWith (theGiantSpaceShip); SpaceShip & theSpaceShipReference = theGiantSpaceShip; theAsteroid . CollideWith (theSpaceShipReference); theAsteroidReference -> CollideWith (theSpaceShipReference);
  • 19.
    class SpaceShip { virtual void CollideWith ( Asteroid & inAsteroid) { inAsteroid. CollideWith (* this ); } }; class GiantSpaceShip : public SpaceShip {}; Asteroid hit a SpaceShip ExplodingAsteroid hit a SpaceShip SpaceShip & theSpaceShipReference = theGiantSpaceShip; Asteroid & theAsteroidReference = theExplodingAsteroid; theSpaceShipReference. CollideWith (theAsteroid); theSpaceShipReference. CollideWith (theAsteroidReference); class SpaceShip { virtual void CollideWith ( Asteroid & inAsteroid) { inAsteroid. CollideWith (* this ); } }; class GiantSpaceShip : public SpaceShip { virtual void CollideWith ( Asteroid & inAsteroid) { inAsteroid. CollideWith (* this ); } }; Asteroid hit a GiantSpaceShip ExplodingAsteroid hit a GiantSpaceShip
  • 20.