Last active
November 21, 2019 13:58
-
-
Save MMortari/c16574ad0baa3740e079a773bfd4a081 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
class Main { | |
produto: any; | |
constructor(tipoProduto: EnumProduto) { | |
this.produto = new ProdutoFactory(tipoProduto); | |
console.log(this.produto instanceof ProdutoMaracuja); // false | |
console.log(this.produto instanceof ProdutoBanana); // true | |
} | |
getProdutoNome(): string { | |
return this.produto.nomeProduto(); | |
} | |
} | |
class ProdutoFactory { | |
constructor(produto: EnumProduto) { | |
if(produto == EnumProduto.MARACUJA) { | |
return new ProdutoMaracuja; | |
} else if(produto == EnumProduto.BANANA) { | |
return new ProdutoBanana; | |
} | |
} | |
} | |
interface Produto { | |
nomeProduto(): string; | |
} | |
enum EnumProduto { | |
MARACUJA, | |
BANANA | |
} | |
class ProdutoMaracuja implements Produto { | |
nomeProduto(): string{ | |
return "maracuja"; | |
} | |
} | |
class ProdutoBanana implements Produto { | |
nomeProduto(): string{ | |
return "banana"; | |
} | |
} | |
const teste = new Main(EnumProduto.BANANA); | |
console.log(teste.getProdutoNome()); // banana |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment