Created
February 5, 2017 21:52
-
-
Save naughty-code/7134fc812e000716f1f54a8c50099142 to your computer and use it in GitHub Desktop.
Ejemplo de patrones de diseño
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Primer diseño, sin usar el patrón */ | |
class Monster | |
{ | |
// Stuff... | |
}; | |
class Ghost : public Monster {}; | |
class Demon : public Monster {}; | |
class Sorcerer : public Monster {}; | |
/* Implementando */ | |
class Spawner | |
{ | |
public: | |
virtual ~Spawner() {} | |
virtual Monster* spawnMonster() = 0; | |
}; | |
class GhostSpawner : public Spawner | |
{ | |
public: | |
virtual Monster* spawnMonster() | |
{ | |
return new Ghost(); | |
} | |
}; | |
class DemonSpawner : public Spawner | |
{ | |
public: | |
virtual Monster* spawnMonster() | |
{ | |
return new Demon(); | |
} | |
}; | |
//lo mismo para SorcererSpawner... | |
/* usando el patrón */ | |
class Monster | |
{ | |
public: | |
virtual ~Monster() {} | |
virtual Monster* clone() = 0; | |
// Other stuff... | |
}; | |
class Ghost : public Monster { | |
public: | |
Ghost(int health, int speed) | |
: health_(health), | |
speed_(speed) | |
{} | |
virtual Monster* clone() | |
{ | |
return new Ghost(health_, speed_); | |
} | |
private: | |
int health_; | |
int speed_; | |
}; | |
// igual para las clases Demon y sorcerer | |
/* implementando los spawners */ | |
class Spawner | |
{ | |
public: | |
Spawner(Monster* prototype) | |
: prototype_(prototype) | |
{} | |
Monster* spawnMonster() | |
{ | |
return prototype_->clone(); | |
} | |
private: | |
Monster* prototype_; | |
}; | |
/* Para usar el spawner, hacemos lo siguiente: */ | |
Monster* ghostPrototype = new Ghost(15, 3); //instanciamos el monstruo que usaremos como prototipo | |
Spawner* ghostSpawner = new Spawner(ghostPrototype); // le pasamos nuestro prototipo al spawner y listo... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* funcion constructora */ | |
function Weapon(range, damage) { | |
this.range = range; | |
this.damage = damage; | |
} | |
var sword = new Weapon(10, 16); //no solo crea un objeto y le agrega los atributos range y damage | |
//adicionalmente le agrega un atributo llamado Weapon.prototype | |
//que representa el prototipo de todos los objetos Weapon | |
/* Para agregar comportamiento, normalmente lo colocamos en este prototipo */ | |
Weapon.prototype.attack = function(target) { | |
if (distanceTo(target) > this.range) { | |
console.log("Out of range!"); | |
} else { | |
target.health -= this.damage; | |
} | |
} | |
/* Cuando el acceso a un atributo en un objeto falla, se delega el acceso al prototipo para ver si este lo contiene, en este caso | |
cuando llamamos a sword.attack(target), este fallará y buscara en sword.prototype para encontrar el metodo attack() */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment