No tienes acceso a esta clase

¡Continúa aprendiendo! Únete y comienza a potenciar tu carrera

Adquiere por un año todos los cursos, escuelas y certificados por un precio especial.

Antes: $249

Currency
$219/año

Paga en 4 cuotas sin intereses

Paga en 4 cuotas sin intereses
Comprar ahora

Termina en:

0D
15H
48M
26S

Builder: pros y contras

21/27
Recursos

Aportes 3

Preguntas 0

Ordenar por:

¿Quieres ver más aportes, preguntas y respuestas de la comunidad?

o inicia sesión.

Comparto mi solución n.n

type AvailableColors =
    | "red"
    | "black"
    | "gray"
    | "blue"
    | "sky blue"
    | "white"
    | "default";

// STEP 1
interface CarProductionLine {
    setAirBags(howMany: number): CarProductionLine;
    setColor(color: AvailableColors): CarProductionLine;
    setEdition(edition: EditionsType): CarProductionLine;
    resetProductionLine(): void;
}

// STEP 2
type CarCatalog = "mastodon" | "rhino";
type ConstructorParams = { modelToCustomizeInLine: CarCatalog };
type EditionsType = "cvt" | "signature" | "sport" | "default";
class SedanProductionLine implements CarProductionLine {
    private sedanCar!: BaseCar;
    private modelToCustomizeInLine!: CarCatalog;

    constructor({ modelToCustomizeInLine }: ConstructorParams) {
        this.setModelToBuild(modelToCustomizeInLine);
        this.resetProductionLine();
    }

    setAirBags(howMany: number): SedanProductionLine {
        this.sedanCar.airBags = howMany;
        return this;
    }

    setColor(color: AvailableColors): SedanProductionLine {
        this.sedanCar.color = color;
        return this;
    }

    setEdition(edition: EditionsType): SedanProductionLine {
        this.sedanCar.edition = edition;
        return this;
    }

    setModelToBuild(model: CarCatalog) {
        this.modelToCustomizeInLine = model;
    }

    resetProductionLine() {
        this.sedanCar =
            this.modelToCustomizeInLine === "mastodon"
                ? new MastodonSedanCar()
                : new RhinoSedanCar();
    }

    build(): BaseCar {
        const sedanCar = this.sedanCar;
        this.resetProductionLine();
        return sedanCar;
    }
}
class HatchBackProductionLine implements CarProductionLine {
    private hatchbackCar!: BaseCar;
    private modelToCustomizeInLine!: CarCatalog;

    constructor({ modelToCustomizeInLine }: ConstructorParams) {
        this.setModelToBuild(modelToCustomizeInLine);
        this.resetProductionLine();
    }

    setAirBags(howMany: number): HatchBackProductionLine {
        this.hatchbackCar.airBags = howMany;
        return this;
    }

    setColor(color: AvailableColors): HatchBackProductionLine {
        this.hatchbackCar.color = color;
        return this;
    }

    setEdition(edition: EditionsType): HatchBackProductionLine {
        this.hatchbackCar.edition = edition;
        return this;
    }

    setModelToBuild(model: CarCatalog) {
        this.modelToCustomizeInLine = model;
    }

    resetProductionLine() {
        this.hatchbackCar =
            this.modelToCustomizeInLine === "mastodon"
                ? new MastodonHatchbackCar()
                : new RhinoHatchbackCar();
    }

    build(): BaseCar {
        const hatchbackCar = this.hatchbackCar;
        this.resetProductionLine();
        return hatchbackCar;
    }
}

// STEP 3
class BaseCar {
    private _edition!: EditionsType;
    private _model!: string;
    private _airBags: number = 2;
    private _color: AvailableColors = "black";

    set airBags(howMany: number) {
        this._airBags = howMany;
    }

    set color(color: AvailableColors) {
        this._color = color;
    }

    set edition(edition: EditionsType) {
        this._edition = edition;
    }

    set model(model: string) {
        this._model = model;
    }
}

class MastodonSedanCar extends BaseCar {
    constructor() {
        super();
        this.model = "sedan";
    }
}
class RhinoSedanCar extends BaseCar {
    constructor() {
        super();
        this.model = "sedan";
    }
}

class MastodonHatchbackCar extends BaseCar {
    constructor() {
        super();
        this.model = "hatchback";
    }
}
class RhinoHatchbackCar extends BaseCar {
    constructor() {
        super();
        this.model = "hatchback";
    }
}

// STEP 4
export class Director {
    private productionLine!: CarProductionLine;

    setProductionLine(productionLine: CarProductionLine) {
        this.productionLine = productionLine;
    }

    constructCvtEdition(): void {
        this.productionLine
            .setAirBags(4)
            .setColor("sky blue")
            .setEdition("cvt");
    }

    constructSignatureEdition(): void {
        this.productionLine
            .setAirBags(8)
            .setColor("gray")
            .setEdition("signature");
    }

    constructSportEdition(): void {
        this.productionLine.setAirBags(1).setColor("white").setEdition("sport");
    }
}

type modelCar = "sedan" | "hatchback";
function appBuilder(director: Director, model: modelCar) {
    const productionsLines = {
        sedan: SedanProductionLine,
        hatchback: HatchBackProductionLine,
    };
    const ProductionLine = productionsLines[model];

    const mastodonProductionLine = new ProductionLine({
        modelToCustomizeInLine: "mastodon",
    });

    director.setProductionLine(mastodonProductionLine);

    director.constructCvtEdition();
    const mastodonModelCvt = mastodonProductionLine.build();
    console.log(mastodonModelCvt);

    director.constructSignatureEdition();
    const mastodonModelSignature = mastodonProductionLine.build();
    console.log(mastodonModelSignature);

    director.constructSportEdition();
    const mastodonModelSport = mastodonProductionLine.build();
    console.log(mastodonModelSport);
}

appBuilder(new Director(), "hatchback");

Le hice un custom a la solucion de la prueba, donde por medio de la funcion que se inicializadora, agrega dinamicamente las caracteristicas de un fordFiesta HB de esta forma permite seguir creando mas carros HB con las caracteristicas que se requieran:


interface Carro{
    edition : string;
    model:string;
    airBags: number;
    color:string;
}


interface HBProductionLine{
    car: Carro;
    internalModel: string;
    setAirBags(airbags:number) :any;
    setColor(color:string):any;
    setEdition(edition:string):any;
    resetProductLine():void
    build():Carro
}

type ModelCar = {model:string}

class HBFordFiestaProductionLine implements HBProductionLine{
    car: Carro = {
        edition : "",
        model:"",
        airBags: 0,
        color:""
    };
    internalModel: string = "";
    constructor({model}: ModelCar){
        this.car.model = model;
        this.setInternalModel(model);
        this.resetProductLine()
    }
    setAirBags(airbags: number) {
        this.car.airBags = airbags;
    }
    setColor(color: string) {
        this.car.color = color;
    }
    setEdition(edition: string) {
        this.car.edition = edition;
    }
    setInternalModel(model:string){
        this.internalModel = model;
    }
    resetProductLine(): void {
       if(this.internalModel){

       }
    }
    build(): Carro {
       return this.car;
    }
    
}


class CarroFordHB implements Carro{
    edition: string;
    model: string;
    airBags: number;
    color: string;
    constructor(edition: string, model: string, airBags: number, color: string){
        this.airBags = airBags;
        this.model = model;
        this.color = color;
        this.edition = edition;
    }
}

class Orquestator {
    buildCarFordHB(car: CarroFordHB): CarroFordHB {
        const HBFordFiesta = new HBFordFiestaProductionLine({model: car.model})
        HBFordFiesta.setAirBags(car.airBags);
        HBFordFiesta.setColor(car.color);
        HBFordFiesta.setEdition(car.edition);
        return HBFordFiesta.build()
    }
}

const main = (orq:Orquestator) =>{
    const carFordFiesta = new CarroFordHB("st","2018",2,"grey");
    const carBuilderFord = orq.buildCarFordHB({airBags: carFordFiesta.airBags, color: carFordFiesta.color, edition: carFordFiesta.edition, model: carFordFiesta.model})
    console.log(carBuilderFord) // { edition: 'st', model: '2018', airBags: 2, color: 'grey' }
}

main(new Orquestator())

Comparto mi codigo TS

// Types
type BaseColors = 'blue' | 'black' | 'white';
type Editions = 'CVT' | 'Signature' | 'Sport';
type CarModel = 'mastodon' | 'rhino';

//  Interface productionLine
interface CarProductionLine {
    setAirbags(number: number): CarProductionLine;
    setColor(color: BaseColors): CarProductionLine;
    setEdition(edition: Editions): CarProductionLine;
    resetProductionLine(): void;
    build(): BaseCar;
}


// BaseCar class
class BaseCar {
    private _airbags: number = 2;
    private _color: BaseColors = 'white';
    private _edition!: Editions;
    private _model!: string;

    set airbags(number: number) {
        this._airbags = number;
    }

    set color(color: BaseColors) {
        this._color = color;
    }

    set edition(edition: Editions) {
        this._edition = edition;
    }

    set model(model: string) {
        this._model = model;
    }
}

// Concrete model Class
class MastodonHatchbackCar extends BaseCar {
    constructor() {
        super()
        this.model = 'Hatchback';
    }
}

class RhinoHatchbackCar extends BaseCar {
    constructor() {
        super();
        this.model = 'Hatchback';
    }
}

// HatchBack concrete production Line
class HatchbackProductionLine implements CarProductionLine {

    private _hatchbackCar!: BaseCar;
    private _modelToBuild!: CarModel;

    constructor(carModel: CarModel) {
        this.modelTobuild(carModel);
        this.resetProductionLine();
    }

    setAirbags(number: number): HatchbackProductionLine {
        this._hatchbackCar.airbags = number;
        return this
    }

    setColor(color: BaseColors): HatchbackProductionLine {
        this._hatchbackCar.color = color;
        return this;
    }

    setEdition(edition: Editions): HatchbackProductionLine {
        this._hatchbackCar.edition = edition;
        return this;
    }

    resetProductionLine(): void {
        this._hatchbackCar =
            this._modelToBuild === 'mastodon' ?
                new MastodonHatchbackCar() :
                new RhinoHatchbackCar();
    }

    modelTobuild(carModel: CarModel) {
        this._modelToBuild = carModel;
    }

    build(): BaseCar {
        const HatchbackCar = this._hatchbackCar;
        this.resetProductionLine();
        return HatchbackCar;
    }
}

class Director {

    private _productionLine!: CarProductionLine;

    setProductionLine(productionLine: CarProductionLine) {
        this._productionLine = productionLine;
    }

    constructCvt() {
        this._productionLine
            .setAirbags(4)
            .setColor('blue')
            .setEdition('CVT')            
    };

    constructSignature() {
        this._productionLine
            .setAirbags(3)
            .setColor('black')
            .setEdition('Signature')
    };

    constructSport() {
        this._productionLine
            .setAirbags(2)
            .setEdition('Sport')
            .setColor('blue')
    };
}

// Applications
// mastodon hatchback builder
function hatchBackMastodonBuilder(director: Director) {
    
    if (!director) {
        console.log('--- No director provided ---');
        return;
      }

    const hatchBackMastodonProductionLine = new HatchbackProductionLine('mastodon');

    director.setProductionLine(hatchBackMastodonProductionLine);

    // CVT Construction
    director.constructCvt();
    const mastodonHatchbackCVT = hatchBackMastodonProductionLine.build()
    console.log('--- MASTODON HATCHBACK CVT ---');
    console.log(mastodonHatchbackCVT);
    
    // Signature Construction   
    director.constructSignature();
    const mastodonHatchbackSignature = hatchBackMastodonProductionLine.build()
    console.log('--- MASTODON HATCHBACK SIGNATURE ---');
    console.log(mastodonHatchbackSignature);

    // Sport Construction   
    director.constructSport();
    const mastodonHatchbackSport = hatchBackMastodonProductionLine.build()
    console.log('--- MASTODON HATCHBACK SPORT ---');
    console.log(mastodonHatchbackSport);
}

// rhino hatchback builder
function hatchBackRhinoBuilder(director: Director) {
    
    if (!director) {
        console.log('--- No director provided ---');
        return;
      }

    const hatchBackRhinoProductionLine = new HatchbackProductionLine('rhino');

    director.setProductionLine(hatchBackRhinoProductionLine);

    // CVT Construction
    director.constructCvt();
    const rhinoHatchbackCVT = hatchBackRhinoProductionLine.build()
    console.log('--- RHINO HATCHBACK CVT ---');
    console.log(rhinoHatchbackCVT);
    
    // Signature Construction   
    director.constructSignature();
    const rhinoHatchbackSignature = hatchBackRhinoProductionLine.build()
    console.log('--- RHINO HATCHBACK SIGNATURE ---');
    console.log(rhinoHatchbackSignature);

    // Sport Construction   
    director.constructSport();
    const rhinoHatchbackSport = hatchBackRhinoProductionLine.build()
    console.log('--- RHINO HATCHBACK SPORT ---');
    console.log(rhinoHatchbackSport);
};

// Implementation
hatchBackMastodonBuilder(new Director);
hatchBackRhinoBuilder(new Director);