Last active
April 7, 2022 21:04
-
-
Save wesleysaraujo/490643bd314949e5d77f2be679bb24e7 to your computer and use it in GitHub Desktop.
Exemplo de classe Carro no typescript. Esse é um bom exemplo que atende perfeitamente ao princípio da responsabilidade única, pois cada ação está segregado na sua classe especializada.
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 default class Carro | |
{ | |
private _ano: number | |
private _velocidade: number = 0 | |
private _modelo: string | |
private _marca: string | |
private _cor: string | |
constructor(ano: number, modelo: string, marca: string, cor: string){ | |
this._ano = ano | |
this._modelo = modelo | |
this._marca = marca | |
this._cor = cor | |
} | |
public acelerar(): void{ | |
this._velocidade += 10 | |
} | |
public frear(): void{ | |
if (this._velocidade > 10) { | |
this._velocidade -= 10 | |
} else { | |
this._velocidade = 0 | |
} | |
} | |
public buzinar(): string{ | |
return 'Beeeeeee' | |
} | |
public getDetalheCarro(): string { | |
return `${this.marca} ${this.modelo} ${this.cor} ano ${this.ano}` | |
} | |
get ano(): number{ | |
return this._ano | |
} | |
get modelo(): string{ | |
return this._modelo | |
} | |
get marca(): string{ | |
return this._marca | |
} | |
get velocidade(): number{ | |
return this._velocidade | |
} | |
get cor(): string{ | |
return this._cor | |
} | |
set cor(cor: string){ | |
this._cor = cor | |
} | |
} | |
// Uso da classe Carro - O objeto carro é um carro de cor preta, modelo Gol, marca Fiat e ano 2018 | |
// e esse carro ele acelera, freia e buzina. | |
// Ao contrário do exemplo ruim, trocar o pneu, trocar o oleo seria uma responsabilidade do objeto Mecanico, abastecer do objeto Motorista, e lavar do LavaJato. | |
/* | |
let carro: Carro = new Carro(2016, 'New Fiesta', 'Ford', 'Branco') | |
console.log('Comprei o seguinte carro:') | |
console.log(`Modelo: ${carro.modelo} - Marca: ${carro.marca} - Cor: ${carro.cor} - Ano: ${carro.ano}`) | |
console.log(`Velocidade: ${carro.velocidade}`) | |
console.log('Acelerando...') | |
carro.acelerar() | |
console.log(`Velocidade: ${carro.velocidade}`) | |
carro.acelerar() | |
console.log(`Velocidade: ${carro.velocidade}`) | |
console.log('Freando...') | |
carro.frear() | |
console.log(`Velocidade: ${carro.velocidade}`) | |
carro.frear() | |
console.log(`Velocidade: ${carro.velocidade}`) | |
*/ |
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 Carro from './Carro' | |
interface ServicosLavaJatoInterface { | |
get carro(): Carro | |
lavar(): string | |
polir(): string | |
} | |
export default class LavaJato implements ServicosLavaJatoInterface { | |
private _carro: Carro | |
constructor(carro: Carro) | |
{ | |
this._carro = carro | |
} | |
get carro(): Carro { | |
return this._carro | |
} | |
public lavar(): string { | |
return `Lavando o ${this.carro.getDetalheCarro()}` | |
} | |
public polir(): string { | |
return `Polindo o ${this.carro.getDetalheCarro()}` | |
} | |
} |
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 Carro from './Carro' | |
interface ServicosMecanicosInterface { | |
get carro(): Carro | |
trocarPneu(): string | |
trocarOleo(): string | |
} | |
export default class Mecanico implements ServicosMecanicosInterface { | |
private _carro: Carro | |
constructor(carro: Carro) | |
{ | |
this._carro = carro | |
} | |
get carro(): Carro { | |
return this._carro | |
} | |
public trocarPneu(): string { | |
return `Trocando pneu do ${this.carro.getDetalheCarro()}` | |
} | |
public trocarOleo(): string { | |
return `Trocando oleo do ${this.carro.getDetalheCarro()}` | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment