Decorator Example (Typescript)

In this example below you will see how to do a Decorator Example (Typescript) with some HTML / CSS and Javascript

Thumbnail
This awesome code was written by Frontender, you can see more from this user in the personal repository.
You can find the original code on Codepen.io
Copyright Frontender ©
  • HTML
  • CSS
  • JavaScript
    <h1>Decorator Example (Typescript)</h1>
<p>There is a Component: Product or its descendant - Jacket. There are several types of prices for Products: basic price,  sales price, price for another area, online price etc. Based on basic price, lets combine different conditions to get prices for particular cases. The hierarchy of classes of the Component (Product) is not affected, instead a number of Decorators have been created to actually "decorate" our basic price allowing variative behavior without creating classes like "ProductChicagoStore", "ProductBostonStore" or "ProductChicagoOnline".</p>
Buy a jacket:
<button id="sender1">Basic Price</button>
<button id="sender2">Price for Chicago</button>
<button id="sender3">Price for Boston</button>
<button id="sender4">Online Price</button>
<button id="sender5">Online Price Boston</button>
<button id="sender6">Online Price Chicago</button>
<button id="reset">Reset</button>
<div id="receiver">
<header>Prices:</header>
</div>

/*Downloaded from https://www.codeseek.co/curveball/decorator-example-typescript-WRyzOJ */
    body {padding:10px;} 
#receiver {
  border:1px solid gray;
  width:25%;
  margin-top:1em;
  padding: 5px;
}
#receiver header, #receiver div {
  border-bottom:1px solid gray;
  padding:5px 0px;
}


/*Downloaded from https://www.codeseek.co/curveball/decorator-example-typescript-WRyzOJ */
    namespace DecoratorArea {
  /* Component */
  abstract class Product {
    constructor(protected price: number) {}
    public abstract getPrice(): number;
  }
  class Jacket extends Product {
    public getPrice(): number {
      return this.price;
    }
  }
  /* Decorators */
  abstract class Decorator {
    protected elem: any = document.getElementById("receiver");
    constructor(protected product: Product | Decorator) {}
    public displayPrice(): void {
      let el: HTMLElement = document.createElement("div");
      el.textContent = Math.round(100 * this.getPrice()) / 100 + "$";
      this.elem.appendChild(el);
    }
    public abstract getPrice(): number;
  }
  class DecoratorBasicPrice extends Decorator {
    public getPrice(): number {
      return this.product.getPrice();
    }
  }
  class DecoratorBostonPrice extends Decorator {
    public getPrice(): number {
      return this.product.getPrice() * 1.07;
    }
  }
  class DecoratorOnlinePrice extends Decorator {
    public getPrice(): number {
      return this.product.getPrice() * 0.97;
    }
  }
  class DecoratorChicagoPrice extends Decorator {
    public getPrice(): number {
      return this.product.getPrice() * 1.05;
    }
  }
  // Run it all, that's Facade
  class Setup {
    private removeMsg(elem: HTMLElement): void {
      let msgs: Array<HTMLElement> = Array.prototype.slice.call(elem.children);
      msgs.forEach((element: HTMLElement, index: number) => {
       if(msgs[index].tagName != 'header'.toUpperCase()) msgs[index].remove();
      })
    }
    public run(): void {
      let btn1: any = document.getElementById("sender1");
      btn1.addEventListener("click", function() {
      let jacket: Decorator = new DecoratorBasicPrice( new Jacket(100) );
      jacket.displayPrice();
      });
      let btn2: any = document.getElementById("sender2");
      btn2.addEventListener("click", function() {
      let jacket: Decorator = new DecoratorChicagoPrice( new Jacket(100) );
      jacket.displayPrice();
      });
      let btn3: any = document.getElementById("sender3");
      btn3.addEventListener("click", function() {
      let jacket: Decorator = new DecoratorBostonPrice( new Jacket(100) );
      jacket.displayPrice();
      });
      let btn4: any = document.getElementById("sender4");
      btn4.addEventListener("click", function() {
      let jacket: Decorator = new DecoratorOnlinePrice( new Jacket(100) );
      jacket.displayPrice();
      });
      let btn5: any = document.getElementById("sender5");
      btn5.addEventListener("click", function() {
      let jacket: Decorator = new DecoratorOnlinePrice( new DecoratorBostonPrice( new DecoratorBasicPrice( new Jacket(100) ) ) );
      jacket.displayPrice();
      });
      let btn6: any = document.getElementById("sender6");
      btn6.addEventListener("click", function() {
      let jacket: Decorator = new DecoratorOnlinePrice( new DecoratorChicagoPrice( new DecoratorBasicPrice( new Jacket(100) ) ) );
      jacket.displayPrice();
      });
      let reset: any = document.getElementById("reset");
      reset.addEventListener("click", () => {
      let receiver: any = document.getElementById("receiver");
      this.removeMsg(receiver);
      });
    }
 }
 let setup = new Setup();
 setup.run();
}

Comments