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

              
                <!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Observer Pattern</title>
</head>
<body>
  <h1>Observer Pattern Execution Script</h1>
</body>
</html>
              
            
!

CSS

              
                
              
            
!

JS

              
                /**
 * Observer Pattern - Subject Interface
 */
interface INewsFeed {
    add(aSubscriber: ISubscriber): void;
    remove(aSubscriber: ISubscriber): void;
    notify(): void;
}

/**
 * Observer Pattern - Observer Interface
 */
interface ISubscriber {
    id: number;
    update(): void;
}

/**
 * Observer Pattern - ConcreteSubject Class
 */
class NewsFeed implements INewsFeed {

    private _subscribers: Array<ISubscriber> = [];
    private _feed: Array<Article> = [];

    get feed(): Array<Article> {
        return this._feed;
    }

    add(aSubscriber: ISubscriber){
        this._subscribers.push(aSubscriber);
        log(`[NewsFeed] - add new subscriber with id = ${aSubscriber.id}`);
    }

    remove(aSubscriber: ISubscriber){
        this._subscribers.splice(this._subscribers.indexOf(aSubscriber), 1);
        log(`[NewsFeed] - remove a subscriber with id = ${aSubscriber.id}`);
    }

    notify(){
        this._subscribers.map((thisSubscriber) => {
            thisSubscriber.update();
        })
        log(`[NewsFeed] - notified ${this._subscribers.length} subscribers`);
    }

    addArticle(anArticle: Article){
        this._feed.push(anArticle);
        this.notify();
    }
}

/**
 * Observer Pattern - ConcreteObserver Class Type 1
 */
class EmailSubscriber implements ISubscriber{
    id: number;
    private _newsFeed: NewsFeed;

    constructor(aNewsFeed: NewsFeed, anId: number){
        this._newsFeed = aNewsFeed;
        this.id = anId;
    }

    update(){
        log(`[EmailSubscriber] with id = ${this.id} notified.`);
        this._newsFeed.feed.map((thisArticle) => {
            log(`[EmailSubscriber] id = ${this.id} prints article title = ${thisArticle.title}`);
        });
    }
}

/**
 * Observer Pattern - ConcreteObserver Class Type 2
 */
class RssSubscriber implements ISubscriber{
    id: number;
    private _newsFeed: NewsFeed;

    constructor(aNewsFeed: NewsFeed, anId: number){
        this._newsFeed = aNewsFeed;
        this.id = anId;
    }

    update(){
        log(`[RssSubscriber] with id = ${this.id} notified.`);
        this._newsFeed.feed.map((thisArticle) => {
            log(`[RssSubscriber] id = ${this.id} prints article title = ${thisArticle.title}`);
        });
    }
}

/**
 * An class created for this example (not a part of the 
 * official observer pattern persay)
 */
class Article {
    private _title: string;

    constructor(aTitle: string){
        this._title = aTitle;
    }

    get title(): string {
        return this._title;
    }
}

/**
 * A simple function to handle displaying the results of 
 * this example on both the browser and in the console
 */
function log(message){

    if(typeof document !== 'undefined'){
        if(message == " "){
            var br = document.createElement("br");
            document.body.appendChild(br);
        }else{
            var p = document.createElement("p");
            p.innerHTML = message;
            document.body.appendChild(p);
        }
    }else{

        console.log(message);
    }
}

/* Execution Script */
let aNewsFeed = new NewsFeed();
let emailSub = new EmailSubscriber(aNewsFeed, 1);
let rssSub = new RssSubscriber(aNewsFeed, 2);
let emailSub2 = new EmailSubscriber(aNewsFeed, 3);
let rssSub2 = new RssSubscriber(aNewsFeed, 4);


log("***** START *****");
log(" ")
log("***** ADD 2 SUBSCRIBERS ");

aNewsFeed.add(emailSub);
aNewsFeed.add(rssSub);

log(" ")
log("****  ADD FIRST ARTICLE ******");
aNewsFeed.addArticle(new Article("First Article"));

log(" ")
log("***** ADD 2 MORE SUBSCRIBERS *****");
aNewsFeed.add(emailSub2);
aNewsFeed.add(rssSub2);

log(" ")
log("***** ADD SECOND ARTICLE *****");
aNewsFeed.addArticle(new Article("Second Article"));

log(" ")
log("***** REMOVE 3 SUBSCRIBERS *****");
aNewsFeed.remove(rssSub);
aNewsFeed.remove(emailSub2);
aNewsFeed.remove(rssSub2);

log(" ")
log("***** ADD THIRD ARTICLE *****");
aNewsFeed.addArticle(new Article("Third Article"));
              
            
!
999px

Console