Playground - Mejora el código usando builder pattern
Clase 72 de 99 • 30 días de JavaScript
Contenido del curso
Clase 72 de 99 • 30 días de JavaScript
Contenido del curso
Osvaldo Martinez Guillen
Carina Payleman
Alejandro Anaya
Joan Alexander Valerio Rodríguez
Randy Ochoa
Daniel Romao
David Ochoa
willmar fernando romero marin
Pablo Perez
Alexis Corrales
Angel Javier Sanchez Tenjo
Elias Rayas Gonzalez
Raul Carrillo Garrido
Pablo Pincay Alvarez
JAIME EDUARDO DIAZ TOBON
Esteban Alzate Atehortua
Andrés Camilo Vélez Mejía
Gabriel Luna
Jose Carlos Machado
Abril Darynka Tapia Sosa
Todo bien, listo.
export class CarBuilder { // Tu código aquí 👇 constructor() { this.year = 0; this.model = ''; this.brand = ''; this.color = ''; this.price = 0; this.isAvailable = null; } setYear(year) { this.year = year; return this; } setModel(model) { this.model = model; return this; } setBrand(brand) { this.brand = brand; return this; } setColor(color) { this.color = color; return this; } setPrice(price) { this.price = price; return this; } setIsAvailable(is) { this.isAvailable = is; return this; } build() { return { year: this.year, model: this.model, brand: this.brand, color: this.model, price: this.price, isAvailable: this.isAvailable }; } }
export class CarBuilder { constructor(year, model, brand, color, price, isAvailable) { this.year = 0; this.model = ''; this.brand = ''; this.color = ''; this.price = 0; this.isAvailable = true; } setYear(year) { this.year = year; return this; } setModel(model) { this.model = model; return this; } setBrand(brand) { this.brand = brand; return this; } setColor(color) { this.color = color; return this; } setPrice(price) { this.price = price; return this; } setIsAvailable(isAvailable) { this.isAvailable = isAvailable; return this; } build() { return { year: this.year, model: this.model, brand: this.brand, color: this.color, price: this.price, isAvailable: this.isAvailable }; } }
🛡️🛡️Escudo anti-spoilers🛡️🛡️
Mi solución al reto:
export class CarBuilder { constructor() { } build() { return this } setYear(value) { this.year = value return this } setModel(value) { this.model = value return this } setBrand(value) { this.brand = value return this } setColor(value) { this.color = value return this } setPrice(value) { this.price = value return this } setIsAvailable(value) { this.isAvailable = value return this } }
Excelente el reto !!!
Solucion:
export class CarBuilder { // Tu código aquí 👇 constructor(year, model, brand, color, price, isAvailable) { this.year = 0 this.model = "" this.brand = "" this.color = "" this.price = 0 this.isAvailable = null } setYear(año) { this.year = año return this } setModel(modelo) { this.model = modelo return this } setBrand(brand) { this.brand = brand return this } setColor(color) { this.color = color return this } setPrice(price) { this.price = price return this } setIsAvailable(isAvailable) { this.isAvailable = isAvailable return this } build() { return { year: this.year, model: this.model, brand: this.brand, color: this.color, price: this.price, isAvailable: this.isAvailable } } }```
Mi solucion: . . . . . . . . . .
export class CarBuilder { constructor() { this.year = 0; this.model = ''; this.brand = ''; this.color = ''; this.price = 0; this.isAvailable = null; } setYear(year) { this.year = year; return this; } setModel(model) { this.model = model; return this; } setBrand(brand) { this.brand = brand; return this; } setColor(color) { this.color = color; return this; } setPrice(price) { this.price = price; return this; } setIsAvailable(boolean) { this.isAvailable = boolean; return this; } build() { return { year: this.year, model: this.model, brand: this.brand, color: this.color, price: this.price, isAvailable: this.isAvailable, } } }
🛡️🛡️🛡️Escudo anti spoilers🛡️🛡️🛡️
Mejora el codigo usando builder
!Spoiler Shield:format(gif)/cdn.vox-cdn.com/uploads/chorus_image/image/55278741/gatsby.0.gif)
export class CarBuilder { // Tu código aquí 👇 constructor() { this.year = 0; this.model = ""; this.brand = ""; this.color = ""; this.price = 0; this.isAvailable = undefined; } setYear(year) { this.year = year; return this; } setModel(model) { this.model = model; return this; } setBrand(brand) { this.brand = brand; return this; } setColor(color) { this.color = color; return this; } setPrice(price) { this.price = price; return this; } setIsAvailable(isAvailable) { this.isAvailable = isAvailable; return this; } build() { return { year: this.year, model: this.model, brand: this.brand, color: this.color, price: this.price, isAvailable: this.isAvailable, }; } }
en las plantillas esta el poder de la industria del software. me doy cuenta que hay que practicar hasta ver las generalidades de las plantillas y en ese entonces se tendrá la vision de un arquitecto
export class CarBuilder { constructor() { this.year = 0; this.model = ""; this.brand = ""; this.color = ""; this.price = 0; this.isAvailable = false; } setYear(year) { this.year = year; return this; } setModel(model) { this.model = model; return this; } setBrand(brand) { this.brand = brand; return this; } setColor(color) { this.color = color; return this; } setPrice(price) { this.price = price; return this; } setIsAvailable(isAvailable) { this.isAvailable = isAvailable; return this; } build() { return { year: this.year, model: this.model, brand: this.brand, color: this.color, price: this.price, isAvailable: this.isAvailable, }; } }
export class CarBuilder { // Tu código aquí 👇 constructor(year, model, brand, color, price, isAvailable) { this.year = 0; this.model = ''; this.brand = ''; this.color = ''; this.price = 0; this.isAvailable = false; } setYear(year) { this.year = year; return this; } setModel(model) { this.model = model; return this; } setBrand(brand) { this.brand = brand; return this; } setColor(color) { this.color = color; return this; } setPrice(price) { this.price = price; return this; } setIsAvailable(isAvailable) { this.isAvailable = isAvailable; return this; } build() { return { year: this.year, model: this.model, brand: this.brand, color: this.color, price: this.price, isAvailable: this.isAvailable, }; } }
Hola la solución....
✅ ✅ ✅ ✅ ✅ ✅ ✅ ✅ ✅ ✅ ✅ ✅
exercise.js
export class CarBuilder { constructor() { this.year = 0; this.model = ""; this.brand = ""; this.color = ""; this.price = 0; this.isAvailable = true; } setYear(year) { this.year = year; return this; } setModel(model) { this.model = model; return this; } setBrand(brand) { this.brand = brand; return this; } setColor(color) { this.color = color; return this; } setPrice(price) { this.price = price; return this; } setIsAvailable(isAvailable) { this.isAvailable = isAvailable; return this; } build() { return { year: this.year, model: this.model, brand: this.brand, color: this.color, isNew: this.isNew, price: this.price, isAvailable: this.isAvailable, }; } }
Solucion
class CarBuilder { // Tu código aquí 👇 setYear(Year) { this.Year = Year; return this; } setModel(Model) { this.Model = Model; return this; } setBrand(Brand) { this.Brand = Brand; return this; } setColor(Color) { this.Color = Color; return this; } setPrice(Price) { this.Price = Price; return this; } setIsAvailable(IsAvailable) { this.IsAvailable = IsAvailable; return this; } build() { return { year: this.Year, model: this.Model, brand: this.Brand, color: this.Color, price: this.Price, isAvailable: this.IsAvailable } } }
. . . . . . . . . . . . .
class Car { constructor(year, model, brand, color, price, isAvailable) { this.year = year; this.model = model; this.brand = brand; this.color = color; this.price = price; this.isAvailable = isAvailable; } } export class CarBuilder { constructor() { this.year = null; this.model = null; this.brand = null; this.color = null; this.price = null; this.isAvailable = null; } setYear(year) { this.year = year; return this; } setModel(model) { this.model = model; return this; } setBrand(brand) { this.brand = brand; return this; } setColor(color) { this.color = color; return this; } setPrice(price) { this.price = price; return this; } setIsAvailable(isAvailable) { this.isAvailable = isAvailable; return this; } build() { return new Car( this.year, this.model, this.brand, this.color, this.price, this.isAvailable ); } }
SPOILER <3
.
. . . .
. . . . . .
. . . . . .
.
export class CarBuilder { // Tu código aquí 👇 constructor() { this.year = 0; this.model = ""; this.brand = ""; this.color = ""; this.price = 0; this.isAvailable = false; } setYear(year) { this.year = year return this } setModel(model) { this.model = model return this } setBrand(brand) { this.brand = brand return this } setColor(color) { this.color = color return this } setPrice(price) { this.price = price return this } setIsAvailable(isAvailable) { this.isAvailable = isAvailable return this } build() { return { year, model, brand, color, print, isAvailable } } }
++MI SOLUCION++ 💪 . . . . . . . . . . . . . . . . . . . . . . .
export class CarBuilder { constructor() { this.year = 0; this.model = ""; this.brand = ""; this.color = ""; this.price = 0; this.isAvailable = false; } setYear(year) { this.year = year; return this; } setModel(model) { this.model = model; return this; } setBrand(brand) { this.brand = brand; return this; } setColor(color) { this.color = color; return this; } setPrice(price) { this.price = price; return this; } setIsAvailable(isAvailable) { this.isAvailable = isAvailable; return this; } build() { return { year: this.year, model: this.model, brand: this.brand, color: this.color, price: this.price, isAvailable: this.isAvailable }; } }
Solución: . . . . . . . .
. . . . . . . . . . .
export class CarBuilder { // Tu código aquí 👇 constructor() { this.year = 0; this.model =0; this.brand =""; this.color =""; this.price =0; this.isAvailable = true; } setYear(year){ this.year= year; return this; } setModel(model){ this.model= model; return this; } setBrand(brand){ this.brand= brand; return this; } setColor(color){ this.color= color; return this; } setPrice(price){ this.price= price; return this; } setIsAvailable(isAvailable){ this.isAvailable= isAvailable; return this; } build(){ return{ year: this.year, model: this.model, brand: this.brand, color: this.color, price: this.price, isAvailable: this.isAvailable, } } }
export class CarBuilder { // Tu código aquí 👇 constructor() { this.year = 0; this.model = ''; this.brand = ''; this.color = ''; this.price = 0; this.isAvailable = false; } setYear(year) { this.year = year; return this; } setModel(model) { this.model = model; return this; } setBrand(brand) { this.brand = brand; return this; } setColor(color) { this.color = color; return this; } setPrice(price) { this.price = price; return this; } setIsAvailable(isAvailable) { this.isAvailable = isAvailable; return this; } build() { return { year: this.year, model: this.model, brand: this.brand, color: this.color, price: this.price, isAvailable: this.isAvailable, }; } }
Aquí mi solución: . . . . . . . . . . . . .
export class CarBuilder { constructor() { this.year = 0; this.model = ''; this.brand = ''; this.color = ''; this.price = 0; this.isAvailable = false; } setYear(year) { this.year = year; return this; } setModel(model) { this.model = model; return this; } setBrand(brand) { this.brand = brand; return this; } setColor(color) { this.color = color; return this; } setPrice(price) { this.price = price; return this; } setIsAvailable(isAvailable) { this.isAvailable = isAvailable; return this; } build() { return { year: this.year, model: this.model, brand: this.brand, color: this.color, price: this.price, isAvailable: this.isAvailable } } }
Me quedó bien claro como funciona el patrón constructor jeje... aquí dejo mi código... :)
export class CarBuilder { // Tu código aquí 👇 constructor(year, model, brand, color, price, isAvailable) { this.year = year; this.model = model; this.brand = brand; this.color = color; this.price = price; this.isAvailable = isAvailable; } setYear(year) { this.year = year; return this; } setModel(model) { this.model = model; return this; } setBrand(brand) { this.brand = brand; return this; } setColor(color) { this.color = color; return this; } setPrice(price) { this.price = price; return this; } setIsAvailable(isAvailable) { this.isAvailable = isAvailable; return this; } build() { return { year: this.year, model: this.model, brand: this.brand, color: this.color, price: this.price, isAvailable: this.isAvailable } } }
✨ . . . . . . . . . . . . . .
export class CarBuilder { // Tu código aquí 👇 constructor(year, model, brand, color, price, isAvailable) { this.year = year; this.model = model; this.brand = brand; this.color = color; this.price = price; this.isAvailable = isAvailable; } setYear(year) { this.year = year; return this } setModel(model) { this.model = model; return this } setBrand(brand) { this.brand = brand; return this } setColor(color) { this.color = color; return this } setPrice(price) { this.price = price return this } setIsAvailable(available) { this.isAvailable = available return this } build() { return { year: this.year, model: this.model, brand: this.brand, color: this.color, price: this.price, isAvailable: this.isAvailable } } }