En programación orientada a objetos, (casi?) siempre es mejor partir la funcionalidad mediante objetos Estrategia, en vez de utilizar herencia.
Inheritance is evil, and must be destroyed: part 1
El autor también ha creado una librería javascript para animación, basándose en ese concepto:
Bernie's Better Animation Class
Inheritance is evil, and must be destroyed: part 1
This looks like the simplest approach, and it is at first. However, your dark powers are locked up inside the DarkJedi class. If you need to make a DarkDroid and a DarkSpaceship that can both also crush townspeople, you're in trouble. These classes obviously can't extend Jedi, so you have to duplicate townspeople crushing functionality across your whole DarkArmy or split it out into utility functions that you call from every crushTownspeople method. Either way, it gets complicated.Now suppose you had done it like this:
// good
class Jedi {
function drawSabre():Sabre { ... }
}
class DarkPowers {
function crushTownspeople():void { ... }
}
class DarkJedi extends Jedi {
// DarkJedi has-a DarkPowers
public var darkPowers:DarkPowers = new DarkPowers();
}
dj:DarkJedi = new DarkJedi();
dj.darkPowers.crushTownspeople();
El autor también ha creado una librería javascript para animación, basándose en ese concepto:
Bernie's Better Animation Class
I checked several different libraries and found them all to be lacking.
In particular, they don't seem to realise that inheritance is evil, and must be destroyed. By providing base classes for an effect and requiring users to subclass it to make new effects, they create a proliferation of classes and make it too hard to create new effects that the library designer hasn't thought of (scriptaculous gets round this by thinking of every effect you might want, which is why it is so large).
Powered by ScribeFire.
No hay comentarios:
Publicar un comentario