JS Object-oriented observer pattern

Source: Internet
Author: User

Simulate some column dependencies that a target might have
function Observerlist () {
This.observerlist = [];
};

Add an Observer
OBSERVERLIST.PROTOTYPE.ADD = function (obj) {
return This.observerList.push (obj);
};

Clear all observers
ObserverList.prototype.Empty = function () {
This.observerlist = [];
};

Get the number of all observers
ObserverList.prototype.Count = function () {
return this.observerList.length;
};

//Get a specified observer
ObserverList.prototype.Get = function (index) {
if (Index >-1 && Index < THIS.OBSE Rverlist.length) {
return this.observerlist[index];
}
};
//INSERT an observer
ObserverList.prototype.Insert = function (obj, index) {
var pointer =-1;
if (index = = = 0) {
This.observerList.unshift (obj);
pointer = index;
} else if (index = = this.observerList.length) {
This.observerList.push (obj);
pointer = index;
}
return pointer;
};
//Starting from a location to find a specific observer and returning the index value of the Observer
ObserverList.prototype.IndexOf = function (obj, startIndex) {
var i = StartIndex , pointer =-1;
while (I < this.observerList.length) {
if (this.observerlist[i] = = obj) {
pointer = i;
} i++;
}
return pointer;
};

Remove the observer for a particular index (if present)
ObserverList.prototype.RemoveIndexAt = function (index) {
if (index = = 0) {
This.observerList.shift ();
} else if (index = = this.observerlist.length-1) {
This.observerList.pop ();
}
};

Extension objects
function extend (obj, extension) {
Console.log (obj);
For (var k in obj) {
Console.log (k);
EXTENSION[K] = Obj[k]
}
};

Simulates a target object and simulates the ability to implement adding, deleting, and notifying observers on the observer list
function Subject () {
This.observers = new Observerlist ();
};

Target ADD an Observer
Subject.prototype.AddObserver = function (Observer) {
THIS.OBSERVERS.ADD (Observer);
};

Target object removes an observer
Subject.prototype.RemoveObserver = function (Observer) {
THIS.OBSERVERS.REMOVEINDEXAT (THIS.OBSERVERS.INDEXOF (Observer, 0));
};

Subject.prototype.RemoveAllObserver = function () {
This.observers = [];
}

Simulation Implementation notification function
Subject.prototype.Notify = function (context) {
var observercount = This.observers.Count ();
for (var i = 0; i < Observercount; i++) {
This.observers.Get (i). Update (context);
}
};
Create an observer by simulating
function Observer () {
This. Update = function () {
};
}

var controlcheckbox = document.getElementById ("Maincheckbox");
var removerobserver = document.getElementById ("Removerallobserver");
var addbtn = document.getElementById ("Addnewobserver");
var container = document.getElementById ("Observercontainer");
Extend (new Subject (), Controlcheckbox);

controlcheckbox["onclick"] = Function ("controlcheckbox.notify (controlcheckbox.checked)");

addbtn["onclick"] = Addnewobserver;
function Addnewobserver () {
var check = document.createelement ("input");
Check.type = "checkbox";
Extend (new Observer (), check);
Check. Update = function (value) {
this.checked = value;
};
Controlcheckbox.addobserver (check);
Container.appendchild (check);
}

JS Object-oriented observer pattern

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.