Advertisement
Guest User

Untitled

a guest
Apr 24th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * FACTORY METHOD 2
  3.  */
  4.  
  5. abstract class PrepaymentCreator {
  6.     public abstract prepaymentFactory(): IPrepayment;
  7.  
  8.     public addToDb(): string {
  9.         const prepayment = this.prepaymentFactory();
  10.  
  11.         return `ABSTRACT FACTORY: The prepayment for  ${prepayment.getDate()} with amount ${prepayment.getAmount()} has been saved to database.`;
  12.     }
  13.  
  14.     public getDate(): string {
  15.         const prepayment = this.prepaymentFactory();
  16.  
  17.         const month = prepayment.getDate().split('.')[0];
  18.         const year = prepayment.getDate().split('.')[1];
  19.  
  20.         return `ABSTACT FACTORY: Month is ${month}, year is ${year}`;
  21.     }
  22. }
  23.  
  24. interface IPrepayment {
  25.     getAmount(): number;
  26.     getDate(): string;
  27. }
  28.  
  29. class ConcretePrepayament extends PrepaymentCreator {
  30.     constructor(
  31.         amount: number,
  32.         date: string
  33.     ) {
  34.         super();
  35.         this.amount = amount;
  36.         this.date = date;
  37.     }
  38.  
  39.     private amount: number;
  40.     private date: string;
  41.  
  42.     public prepaymentFactory(): IPrepayment {
  43.         return new Prepayment(this.amount, this.date);
  44.     }
  45. }
  46.  
  47. class Prepayment implements IPrepayment {
  48.     constructor(
  49.         amount: number,
  50.         date: string
  51.     ) {
  52.         this.amount = amount;
  53.         this.date = date;
  54.     }
  55.  
  56.     private amount: number;
  57.     private date: string;
  58.  
  59.     public getAmount(): number {
  60.         return this.amount;
  61.     }
  62.  
  63.     public getDate(): string {
  64.         return this.date;
  65.     }
  66. }
  67.  
  68.  
  69. /**
  70.  * CLIENT CODE
  71.  */
  72.  
  73. const prepayment = new ConcretePrepayament(10000, '04.2019');
  74.  
  75. console.log(prepayment.addToDb());
  76. console.log(prepayment.getDate());
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement