Skip to content

Instantly share code, notes, and snippets.

View Caballerog's full-sized avatar
:octocat:
live$.pipe(map(person => person.teach(me)),tap(me => me.betterEachDay())))

Carlos Caballero Caballerog

:octocat:
live$.pipe(map(person => person.teach(me)),tap(me => me.betterEachDay())))
View GitHub Profile
import { VirtualPet } from "./virtual-pet";
// Create a virtual pet and initialize it
const neko = new VirtualPet('Neko', 'Red', ['Fly', 'Breath Fire', 'Invisibility']); // 3 seconds
neko.displayInfo();
const nya = neko.clone(); // a few milliseconds
nya.name = 'Nya';
nya.color = 'Yellow';
nya.displayInfo();
import { Clonable } from "./clonable";
export class VirtualPet implements Clonable {
name: string;
color: string;
abilities: string[];
energyLevel: number = 0;
constructor(name: string, color: string, abilities: string[], energyLevel: number = 100) {
this.name = name;
export interface Clonable {
clone(): this;
}
import { VirtualPet } from './virtual-pet';
// Create a virtual pet and initialize it
const neko = new VirtualPet('Neko', 'Gray', ['Fly', 'Breath Fire', 'Invisibility']); // 3 seconds
neko.displayInfo();
// Clone the virtual pet manually with all the attributes
const nya = new VirtualPet('Nya', 'Yellow', [...neko.abilities]); // 3 seconds
nya.displayInfo();
class VirtualPet {
name: string;
color: string;
abilities: string[];
energyLevel: number = 0;
constructor(name: string, color: string, abilities: string[]) {
this.name = name;
this.color = color;
this.abilities = abilities;
import { ConcretePrototype1 } from "./concrete.prototype1";
import { ConcretePrototype2 } from "./concrete-prototype2";
// Create instances of ConcretePrototype1 and ConcretePrototype2
const concretePrototype1 = new ConcretePrototype1();
const concretePrototype2 = new ConcretePrototype2();
// The operation method is called on the instances
concretePrototype1.operation();
concretePrototype2.operation();
import { Prototype } from "./prototype";
export class ConcretePrototype2 implements Prototype {
operation() {
console.log("Operation from ConcretePrototype2");
}
clone(): Prototype {
return new ConcretePrototype2();
}
import { Prototype } from "./prototype";
export class ConcretePrototype1 implements Prototype {
operation() {
console.log("Operation from ConcretePrototype1");
}
clone(): Prototype {
return new ConcretePrototype1();
}
export interface Prototype {
operation(): void;
clone(): Prototype;
}
import { Ingredient } from "./ingredient";
import { Recipe } from "./recipe";
// Usage
const ingredient1 = new Ingredient("Flour", "2 cups");
const ingredient2 = new Ingredient("Eggs", "3");
const recipe = new Recipe("Cake");
recipe.addComponent(ingredient1);
recipe.addComponent(ingredient2);