Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <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>
              
            
!

CSS

              
                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;
}
              
            
!

JS

              
                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();
}
              
            
!
999px

Console