Skip to content

Instantly share code, notes, and snippets.

@ardnaile
Created June 3, 2025 22:29
Show Gist options
  • Save ardnaile/2e060082a5325beedb1dcbb3d716e73f to your computer and use it in GitHub Desktop.
Save ardnaile/2e060082a5325beedb1dcbb3d716e73f to your computer and use it in GitHub Desktop.
Projeto Final - Design Patterns
interface IComputadorBuilder {
setProcessador(processador: string): this;
setPlacaMae(placaMae: string): this;
setMemoriaRAM(memoriaRAM: string): this;
setArmazenamento(armazenamento: string): this;
setPlacaDeVideo(placaDeVideo: string): this;
setFonte(fonte: string): this;
setGabinete(gabinete: string): this;
setSistemaOperacional(sistemaOperacional: string): this;
setCooler(cooler: string): this;
build(): Computador;
}
class Computador {
constructor(
public processador: string,
public placaMae: string,
public memoriaRAM: string,
public armazenamento: string,
public placaDeVideo: string,
public fonte: string,
public gabinete: string,
public sistemaOperacional?: string,
public cooler?: string,
) {}
}
// Builder principal, cria um pc de forma personalizada
class ComputadorBuilder implements IComputadorBuilder {
private _processador?: string;
private _placaMae?: string;
private _memoriaRAM?: string;
private _armazenamento?: string;
private _placaDeVideo?: string;
private _fonte?: string;
private _gabinete?: string;
private _sistemaOperacional?: string;
private _cooler?: string;
setProcessador(processador: string): this {
this._processador = processador;
return this;
}
setPlacaMae(placaMae: string): this {
this._placaMae = placaMae;
return this;
}
setMemoriaRAM(memoriaRAM: string): this {
this._memoriaRAM = memoriaRAM;
return this;
}
setArmazenamento(armazenamento: string): this {
this._armazenamento = armazenamento;
return this;
}
setPlacaDeVideo(placaDeVideo: string): this {
this._placaDeVideo = placaDeVideo;
return this;
}
setFonte(fonte: string): this {
this._fonte = fonte;
return this;
}
setGabinete(gabinete: string): this {
this._gabinete = gabinete;
return this;
}
setSistemaOperacional(sistemaOperacional: string): this {
this._sistemaOperacional = sistemaOperacional;
return this;
}
setCooler(cooler: string): this {
this._cooler = cooler;
return this;
}
build(): Computador {
if (
!this._processador ||
!this._placaMae ||
!this._memoriaRAM ||
!this._armazenamento ||
!this._placaDeVideo ||
!this._fonte ||
!this._gabinete
) {
throw new Error("Todos os componentes obrigatórios devem ser definidos.");
}
return new Computador(
this._processador,
this._placaMae,
this._memoriaRAM,
this._armazenamento,
this._placaDeVideo,
this._fonte,
this._gabinete,
this._sistemaOperacional,
this._cooler,
);
}
}
// Builder que cria pcs com base em um preset, para demonstrar o uso do padrão com interface
class PresetComputadorBuilder implements IComputadorBuilder {
private computador: Computador | null = null;
constructor(preset: "GAMER" | "BASICO" | "SERVIDOR") {
if (preset === "GAMER") {
this.computador = new Computador(
"Intel Core i9-13900K",
"ASUS Z790",
"64GB DDR5",
"2TB SSD NVMe",
"NVIDIA RTX 4090",
"1000W 80 Plus Platinum",
"Full Tower RGB",
"Windows 11 Pro",
"Water Cooler 360mm"
);
} else if (preset === "BASICO") {
this.computador = new Computador(
"Intel Core i3-12100",
"Gigabyte B660M",
"8GB DDR4",
"256GB SSD",
"Integrada",
"400W",
"Mini Tower",
"Windows 10 Home"
);
} else if (preset === "SERVIDOR") {
this.computador = new Computador(
"Intel Xeon E-2336",
"Supermicro X12",
"128GB ECC DDR4",
"4TB SSD Enterprise",
"Integrada",
"850W Redundant",
"Rack 2U",
"Ubuntu Server 22.04",
"Cooler industrial"
);
}
}
private throwError(): never {
throw new Error("Não é possível alterar peças: PC pré-montado");
}
setProcessador(): this { this.throwError(); }
setPlacaMae(): this { this.throwError(); }
setMemoriaRAM(): this { this.throwError(); }
setArmazenamento(): this { this.throwError(); }
setPlacaDeVideo(): this { this.throwError(); }
setFonte(): this { this.throwError(); }
setGabinete(): this { this.throwError(); }
setSistemaOperacional(): this { this.throwError(); }
setCooler(): this { this.throwError(); }
setLeitorDVD(): this { this.throwError(); }
build(): Computador {
if (!this.computador) {
throw new Error("Preset inválido ou não definido.");
}
return this.computador;
}
}
// exemplo do builder de pc personalizável
const builder: IComputadorBuilder = new ComputadorBuilder();
const pc = builder
.setProcessador("Intel Core i7-12700K")
.setPlacaMae("ASUS Z690")
.setMemoriaRAM("32GB DDR5")
.setArmazenamento("1TB SSD NVMe")
.setPlacaDeVideo("NVIDIA RTX 4070")
.setFonte("750W 80 Plus Gold")
.setGabinete("Mid Tower com RGB")
.setSistemaOperacional("Windows 11 Pro")
.build();
console.log("PC criado via interface:", pc);
// exemplo do builder de pc pré montado
const builder2: IComputadorBuilder = new PresetComputadorBuilder("GAMER");
const pcGamer = builder2.build();
console.log("PC Gamer pré-montado:", pcGamer);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment