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
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(); |
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
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; |
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
export interface Clonable { | |
clone(): this; | |
} |
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
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(); |
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
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; |
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
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(); |
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
import { Prototype } from "./prototype"; | |
export class ConcretePrototype2 implements Prototype { | |
operation() { | |
console.log("Operation from ConcretePrototype2"); | |
} | |
clone(): Prototype { | |
return new ConcretePrototype2(); | |
} |
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
import { Prototype } from "./prototype"; | |
export class ConcretePrototype1 implements Prototype { | |
operation() { | |
console.log("Operation from ConcretePrototype1"); | |
} | |
clone(): Prototype { | |
return new ConcretePrototype1(); | |
} |
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
export interface Prototype { | |
operation(): void; | |
clone(): Prototype; | |
} |
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
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); |
NewerOlder