Proxy design pattern in Javascript

For any useful advice you want to share, on any subject under the sun.

Moderator: Thought Police

Locked
heavy-rotation
Thought Police
Thought Police
Posts: 1205
Joined: Thu Mar 14, 2002 12:11 pm
Location: Macclesfield
Contact:

Proxy design pattern in Javascript

Post by heavy-rotation »

I've been wrestling with this one for a while, finally got it to work as I wanted the other day. It may be totally useless for anyone here, but I thought I'd post it here anyway.

First of all, I had to extend the function prototype so I could modify the class' prototype before actually using it. I could have done this in the constructor, but that would mean it runs every time you create a new instance of the class. Hardly ideal.

Code: Select all

Function.prototype.makeProxyFor = function (instanceClass)
{
	for (var i in instanceClass.prototype)
	{
		eval ("this.prototype['"+i+"'] = function () { return this.instance['"+i+"'].apply (this.instance, arguments); } ")
	}
}
Thats the main bit of it really. Then all you need is your proxy class and the it acts as a proxy for. Example:

Code: Select all

function Proxy ()
{
	this.instance = new Something ();
}

function Something ()
{
}

Something.prototype.hello = function ()
{
	alert ('Hello from the proxied class!')
}

Proxy.makeProxyFor (Something)

test = new Proxy ()
test.hello ()
Note: unless you fiddle with the makeProxyFor method, you have to make you're proxied class instance name as 'instance'.

Thats not exactly the best example, as I could have done that by making Proxy inherit from Something, but you get the idea.

A better real world example would be the menu system I'm working on currently - I have a MenuStructure class that only controls and keeps track of where items are in relation to itself within the menu (using recursion to make it an n-level menu), and then I have a proxy that is used to display the items on the page and control all the visual elements of it basically. It may seem a bit of a long way of doing things, but you'd be surprised at the number of different variations of menu I'm asked to do, this allows me to keep the same model for the structure but change the way it appears or behaves on-page.
Kajun
World Controller
World Controller
Posts: 10412
Joined: Tue Jan 22, 2002 12:00 am
Location: Hear.
Contact:

Post by Kajun »

I see.
Kajun is awaiting approval.
heavy-rotation
Thought Police
Thought Police
Posts: 1205
Joined: Thu Mar 14, 2002 12:11 pm
Location: Macclesfield
Contact:

Post by heavy-rotation »

:P

When I get some suitable menu stuff done I'll post with a better example.

Anyway: http://wiki.cs.uiuc.edu/PatternStories/ProxyPattern
shtum
Delta (Bokanovsky)
Delta (Bokanovsky)
Posts: 167
Joined: Tue Sep 03, 2002 5:36 pm

Post by shtum »

heavy-rotation wrote:using recursion to make it an n-level menu
its like a sexual thing with you isn't it
heavy-rotation
Thought Police
Thought Police
Posts: 1205
Joined: Thu Mar 14, 2002 12:11 pm
Location: Macclesfield
Contact:

Post by heavy-rotation »

:lol: :P
Kajun
World Controller
World Controller
Posts: 10412
Joined: Tue Jan 22, 2002 12:00 am
Location: Hear.
Contact:

Post by Kajun »

I've tried hard, but I still have no concept of what's going on here, or there. It's as though I missed a year at school. <sniff>
Kajun is awaiting approval.
heavy-rotation
Thought Police
Thought Police
Posts: 1205
Joined: Thu Mar 14, 2002 12:11 pm
Location: Macclesfield
Contact:

Post by heavy-rotation »

Done any OO Javascript before?

If not... another Hints & Tips thread coming right up :)
Kajun
World Controller
World Controller
Posts: 10412
Joined: Tue Jan 22, 2002 12:00 am
Location: Hear.
Contact:

Post by Kajun »

Done very little Javascript as it's largely against my design criteria. But that doesn't mean I can't learn :)
Kajun is awaiting approval.
Locked