paradigmas de linguagens de programacao - aula #7
DESCRIPTION
Paradigmas de Linguagens de Programacao Aula #7 Prof. Ismar FrangoTRANSCRIPT
Paradigmas de Linguagens de Programação
Paradigma Orientado a ObjetosAula #7
(CopyLeft)2009 - Ismar Frango [email protected]
“There are only two kinds of languages: the ones people complain about and the ones nobody uses.”
Bjarne Stroustrup
Binding em LOO
12/04/23
invariância
covariância
contravariância
12/04/23
?
?
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 << "Asteroid hit a SpaceShip" << endl; } virtual void CollideWith(GiantSpaceShip&) { cout << "Asteroid hit a GiantSpaceShip" << endl; }}; class ExplodingAsteroid : public Asteroid {public: virtual void CollideWith(SpaceShip&) { cout << "ExplodingAsteroid hit a SpaceShip" << endl; } virtual void CollideWith(GiantSpaceShip&) { cout << "ExplodingAsteroid hit a GiantSpaceShip" << endl; }};
class SpaceShip {};class GiantSpaceShip : public SpaceShip {}; class Asteroid {public: virtual void CollideWith(SpaceShip&) { cout << "Asteroid hit a SpaceShip" << endl; } virtual void CollideWith(GiantSpaceShip&) { cout << "Asteroid hit a GiantSpaceShip" << endl; }}; class ExplodingAsteroid : public Asteroid {public: virtual void CollideWith(SpaceShip&) { cout << "ExplodingAsteroid hit a SpaceShip" << endl; } virtual void CollideWith(GiantSpaceShip&) { cout << "ExplodingAsteroid hit a GiantSpaceShip" << 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 SpaceShipExplodingAsteroid 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 GiantSpaceShipExplodingAsteroid hit a GiantSpaceShip
O Padrão Visitor