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