Advertisement
Guest User

Untitled

a guest
Apr 20th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.14 KB | None | 0 0
  1. abstract class PlantCreator {
  2.  
  3. public abstract getInstance(): IPlant;
  4.  
  5. public getName(): string {
  6. const plant = this.getInstance();
  7.  
  8. return plant.name;
  9. }
  10.  
  11. public getType(): string {
  12. const plant = this.getInstance();
  13.  
  14. return plant.type;
  15. }
  16. }
  17.  
  18. interface IPlant {
  19. name?: string;
  20. sort?: string;
  21. type?: string;
  22. }
  23.  
  24. class CannabisFactory extends PlantCreator {
  25. constructor(
  26. name: string, sort: string
  27. ) {
  28. super();
  29. this.name = name;
  30. this.sort = sort;
  31. }
  32.  
  33. private type: string = 'Cannabis';
  34. private name: string;
  35. private sort: string;
  36.  
  37. public getInstance(): IPlant {
  38. return new Cannabis(
  39. this.name, this.sort
  40. );
  41. }
  42. }
  43.  
  44. class Cannabis implements IPlant {
  45. constructor(
  46. name: string,
  47. sort: string
  48. ) { }
  49.  
  50. sort: string = this.sort;
  51.  
  52. public getSort(): string {
  53. return this.sort;
  54. }
  55. }
  56.  
  57. function getNewGanja(ganja: PlantCreator): void {
  58. console.log(ganja.getName());
  59. }
  60.  
  61. getNewGanja(new CannabisFactory('First', 'WW'));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement