diff --git a/cypress/integration/solid2/ejemplo1.js b/cypress/integration/solid2/ejemplo1.js new file mode 100644 index 0000000..6f20b58 --- /dev/null +++ b/cypress/integration/solid2/ejemplo1.js @@ -0,0 +1,9 @@ +class ProductosEnAlacena { + productos = ["Piña", "Manzanas", "Harina"]; + existeProducto(producto) { + // indexOf nos devuelve la posición del producto en el array, + // si la posición es -1 significa que no existe el producto + return this.productos.indexOf(producto) !== -1; + } + } + \ No newline at end of file diff --git a/cypress/integration/solid2/ejemplo2.js b/cypress/integration/solid2/ejemplo2.js new file mode 100644 index 0000000..c0cd485 --- /dev/null +++ b/cypress/integration/solid2/ejemplo2.js @@ -0,0 +1,11 @@ +class ProductosEnAlacena { + productos = ["Piña", "Manzanas", "Harina"]; + existeProducto(producto) { + // indexOf nos devuelve la posición del producto en el array, + // si la posición es -1 significa que no existe el producto + return this.productos.indexOf(producto) !== -1; + } + agregarProducto(producto) { + this.productos.push(producto); + } + } \ No newline at end of file diff --git a/cypress/integration/solid2/herencia.js b/cypress/integration/solid2/herencia.js new file mode 100644 index 0000000..144ccf2 --- /dev/null +++ b/cypress/integration/solid2/herencia.js @@ -0,0 +1,28 @@ +class Person{ + //static sCount=0 //1 + constructor(name){ + this.name=name; + this.sCount++; + } + // Método de instancia // 2 + getName(){ + console.log(this.name); + } + static sTest(){ + console.log("static method test"); + } + } + + class Man extends Person{ + constructor(name){ + super(name); + this.sex="male"; + } + } + var man=new Man("David"); + man.getName(); + //man.sTest() + Man.sTest(); + + + \ No newline at end of file diff --git a/cypress/integration/solid2/pdf1.js b/cypress/integration/solid2/pdf1.js new file mode 100644 index 0000000..8df4398 --- /dev/null +++ b/cypress/integration/solid2/pdf1.js @@ -0,0 +1,32 @@ +// O : Open/Closed Principle +// Clases abiertas para ser extendidas, pero cerradas para ser modificadas +// Ejemplo 1: + +class Pdf { + constructor(name, size) { + this.name = name; + this.size = size; + } + + // ... + } + class Png { + constructor(name) { + this.name = name; + } + + // ... + } + class Printer { + function printFile(file) { + if (file instanceof Pdf) { + // Print Pdf... + } else if (file instanceof Png) { + // Print Png... + } + } + } + + + + diff --git a/cypress/integration/solid2/pdf2.js b/cypress/integration/solid2/pdf2.js new file mode 100644 index 0000000..f7b7001 --- /dev/null +++ b/cypress/integration/solid2/pdf2.js @@ -0,0 +1,42 @@ +// O : Open/Closed Principle +// Clases abiertas para ser extendidas, pero cerradas para ser modificadas +// Ejemplo 1: + +class Printable { + function print() { + // ... + } +} + +class Pdf extends Printable { + constructor(name, size) { + super(); + this.name = name; + this.size = size; + } + + // Override + function print() { + // ... + } +} + +class Png extends Printable { + constructor(name) { + super(); + this.name = name; + } + + // Override + function print() { + // ... + } +} + +class Printer { + function printFile(file) { + file.print(); + } +} + + diff --git a/cypress/integration/solid2/pokemon1.js b/cypress/integration/solid2/pokemon1.js new file mode 100644 index 0000000..0cfb79e --- /dev/null +++ b/cypress/integration/solid2/pokemon1.js @@ -0,0 +1,71 @@ +class Pokemon { + #name = ""; + #type = "" + + constructor(name, type) { + this.#name = name; + this.#type = type; + } + + get name() { + return this.#name; + } + + get type() { + return this.#type; + } + +} + +/* + Se genera una clase para procesar los ataques de + los pokemons dependiento del tipo de este. +*/ +class ProcessAttack { + /* + Se crea método que permite procesar los ataques + de un listado de pokemons. + */ + allPokemonAttack(pokemonList) { + const ATTACKS = pokemonList.reduce((output, pokemon) => { + let attack = ""; + const { name, type } = pokemon; + /* + Se crea un listado de casos que permite asignar + un ataque dependiendo el tipo de pokemon. + */ + switch(type) { + case "Electric": + attack = "Impactrueno ⚡️"; + break; + case "Fire": + attack = "Aliento igneo 🔥"; + break; + case "Water": + attack = "Pitsola de agua 🔫"; + break; + attack = "Ataque base"; + default: + } + return `${output}${name}, ${attack}\n`; + }, ""); + return ATTACKS; + } +} +// Se crean instancias de la clase pokemon +const flareon = new Pokemon("Flareon", "Fire"); +const jolteon = new Pokemon("Jolteon", "Electric"); +const vaporeon = new Pokemon("Vaporeon", "Water"); + +// Creamos una instancia de la clase ProcessAttack +const Attack = new ProcessAttack() +// invocamos al método allPokemonAttack y enviamos las +// instancia de la clase pokemon +const MSG = Attack.allPokemonAttack([flareon, jolteon, vaporeon]); +console.log(MSG); +/* + Salida + Flareon, Aliento igneo 🔥 + Jolteon, Impactrueno ⚡️ + Vaporeon, Pitsola de agua 🔫 +*/ \ No newline at end of file diff --git a/cypress/integration/solid2/pokemon2.js b/cypress/integration/solid2/pokemon2.js new file mode 100644 index 0000000..e334e1b --- /dev/null +++ b/cypress/integration/solid2/pokemon2.js @@ -0,0 +1,119 @@ +class Pokemon { + #name = ""; + #type = "" + + constructor(name, type) { + this.#name = name; + this.#type = type; + } + + get name() { + return this.#name; + } + + get type() { + return this.#type; + } + + /* + Cargamos el metodo attack para que todas + las clases que hereden de pokemon puedan utilizarlo + */ + get attack() { + const { name } = this; + return `${name}, Ataque base`; + } + +} + +/* + Por cada tipo de pokemon crearemos una nueva clase + la cual heredara de la clase pokemon +*/ +class TypeElectric extends Pokemon { + constructor(name) { + /* + Invocamos el constructor de la clase pokemon + y pasamos por defecto el tipo Electric + */ + super(name, "Electric"); + } + + get attack() { + const { name } = this; + return `${name}, Impactrueno ⚡️`; + } +} + +class TypeFire extends Pokemon { + constructor(name) { + /* + Invocamos el constructor de la clase pokemon + y pasamos por defecto el tipo Fire + */ + super(name, "Fire"); + } + + get attack() { + const { name } = this; + return `${name}, Aliento igneo 🔥`; + } +} + +class TypeWater extends Pokemon { + constructor(name) { + /* + Invocamos el constructor de la clase pokemon + y pasamos por defecto el tipo Water + */ + super(name, "Water"); + } + + get attack() { + const { name } = this; + return `${name}, Pitsola de agua 🔫`; + } +} + +/* + Se genera una clase para procesar los ataques de + los pokemons dependiento del tipo de este. +*/ +class ProcessAttack { + /* + Se crea método que permite procesar los ataques + de un listado de pokemons. + */ + allPokemonAttack(pokemonList) { + /* + En este caso solo basta con recibir el listado de pokemons + para porder ejecutar un ataque, ya que cada elemento del listado + cuenta con su propio ataque. + */ + const ATTACKS = pokemonList.reduce((output, pokemon) => { + let msg = ""; + const { attack } = pokemon; + return `${output}${attack}\n`; + }, ""); + return ATTACKS; + } +} + +// Creamos una instancia de la clase ProcessAttack +const Attack = new ProcessAttack() + +// invocamos al método allPokemonAttack y enviamos las +// instancia te cada tipo de pokemon +const MSG = Attack.allPokemonAttack([ + new TypeFire("Flareon"), + new TypeElectric("Jolteon"), + new TypeWater("Vaporeon"), + new TypeElectric("Pikachu") +]); +console.log(MSG); +/* + Salida + Flareon, Aliento igneo 🔥 + Jolteon, Impactrueno ⚡️ + Vaporeon, Pitsola de agua 🔫 +*/ \ No newline at end of file