Skip to content

Instantly share code, notes, and snippets.

@distrill
Last active April 5, 2020 23:44
Show Gist options
  • Save distrill/8071cbca415f79d77de51aa2009cc3a4 to your computer and use it in GitHub Desktop.
Save distrill/8071cbca415f79d77de51aa2009cc3a4 to your computer and use it in GitHub Desktop.
// CryptoAnimal - interface we're extending with concrete instances and decorators
class CryptoAnimal {
// each animal has some sort of history that makes them who they are today
background() {
throw new Error('implement me pls');
}
// all of the things in that history make them less common
rarity() {
throw new Error('implement me pls');
}
// useful trinkets picked up along the way
talismans() {
throw new Error('implement me pls');
}
}
// CryptoKitty - concrete CryptoAnimal class, used as a base to build from with decorators
class CryptoKitty extends CryptoAnimal {
background() {
return ['Born a kitty'];
}
rarity() {
return 0.5;
}
talismans() {
return [];
}
}
// CryptoDoggo - concrete CryptoAnimal class, used as a base to build from with decorators
class CryptoDoggo extends CryptoAnimal {
background() {
return ['Born a doggo'];
}
rarity() {
return 0.4;
}
talismans() {
return ['Old tattered copy of "All Dogs Go To Heaven"'];
}
}
// Decorator class - used to define variants of our crypto animals
class CryptoDecorator extends CryptoAnimal {
constructor(animal) {
super();
this.animal = animal;
}
}
/*
The remaining classes are all decorations
These can be inifinite, they just have to extend the CryptoDecorator class
*/
class Ninja extends CryptoDecorator {
background() {
return [...this.animal.background(), 'Went to Ninja school'];
}
rarity() {
return this.animal.rarity() * 0.3;
}
talismans() {
return [...this.animal.talismans(), 'Sweet ninja star'];
}
}
class Samurai extends CryptoDecorator {
background() {
return [...this.animal.background(), 'Trained as a Samurai'];
}
rarity() {
return this.animal.rarity() * 0.05;
}
talismans() {
return this.animal.talismans();
}
}
class BookStudent extends CryptoDecorator {
background() {
return [...this.animal.background(), 'Studied the "Way of the Book"'];
}
rarity() {
return this.animal.rarity() * 0.15;
}
talismans() {
return [...this.animal.talismans(), 'University brochure titled "The Way of the Book"'];
}
}
class Bespectacled extends CryptoDecorator {
background() {
return [...this.animal.background(), 'Lost sharpness of vision, and picked up "Glasses of Fortitude"'];
}
rarity() {
return this.animal.rarity() * 0.15;
}
talismans() {
return [...this.animal.talismans(), 'Glasses of Fortitude'];
}
}
class Hippie extends CryptoDecorator {
background() {
return [...this.animal.background(), 'Went to Woodstock'];
}
rarity() {
return this.animal.rarity() * 0.45;
}
talismans() {
return [...this.animal.talismans(), 'Hash pipe'];
}
}
function run() {
const guy1 = new Bespectacled(new BookStudent(new Samurai(new CryptoDoggo())));
const guy2 = new Hippie(new Ninja(new CryptoKitty()));
console.log('guy 1:');
console.log('BACKGROUND:', guy1.background());
console.log('RARITY:', guy1.rarity());
console.log('TALISMANS:', guy1.talismans());
console.log('\n');
console.log('guy 2:');
console.log('BACKGROUND:', guy2.background());
console.log('RARITY:', guy2.rarity());
console.log('TALISMANS:', guy2.talismans());
}
run();
@distrill
Copy link
Author

distrill commented Apr 5, 2020

guy 1:
BACKGROUND: [
  'Born a doggo',
  'Trained as a Samurai',
  'Studied the "Way of the Book"',
  'Lost sharpness of vision, and picked up "Glasses of Fortitude"'
]
RARITY: 0.00045000000000000004
TALISMANS: [
  'Old tattered copy of "All Dogs Go To Heaven"',
  'University brochure titled "The Way of the Book"',
  'Glasses of Fortitude'
]


guy 2:
BACKGROUND: [
   'Born a kitty',
  'Went to Ninja school',
  'Went to Woodstock'
]
RARITY: 0.0675
TALISMANS: [
  'Sweet ninja star',
  'Hash pipe'
]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment