Implementing Proxy Methods

an article added by: Sonja Lande at 05312007


In: Root » » AJAX » Implementing Proxy Methods

French Spanish Portuguese Italian German Japanese Chinese Korean Russian Arabic

Problem You want an effective strategy for implementing proxy methods. Theory The Proxy pattern is defined as an implementation of an interface that acts as a pass-through for the real implementation. Any user of the interface instance will not realize that a particular method call is being rerouted. The full implementation of the Proxy pattern is not the focus of this article; rather, this article focuses on applying the Proxy pattern for a single method. In the alternate way of wiring together two methods, the defined2 method associated with the class instance is wired to first call the new method, and then call the original method. In this form of wiring, you are not implementing the Proxy pattern you are creating a delegate model. The main difference between a proxy and delegate model is that the proxy model is responsible for calling the original implementation, and the delegate model does not have that restriction. Solution Now that you know the difference between the two models and what you want to achieve, let’s examine what the source code should look like, and then convert that source into a general method. Consider the following class declaration.

   Source: /website/ROOT/ajax articles/javascript/proxy.html
   function DefinedClass() {
   }
   DefinedClass.prototype.defined =  function(tstvalue) {
   info("DefinedClass.defined",  "parameter (" + tstvalue + ") arguments count (" +
   arguments.length + ")");
   DefinedClass.prototype.defined.value = 10;
   assertEquals(tstvalue, this.value);
   }
The DefinedClass class is defined, and it has a single method, defined. The method-level Proxy pattern will be applied to defined. The proxy method that is called before the original method defined is as follows.
   Source: /website/ROOT/ajax articles/javascript/proxy.html
   var proxyfunc = function(tstValue, toCall,  args) {
   info("proxyfunction", "parameter  (" + tstValue + ") received arg count (" +
   arguments.length + ") original arg count  (" + args.length + ")");
   toCall.apply(this, args);
   }

Implementing the proxy can be a challenge, because the current method implementation (defined) has to be moved. Per the Proxy pattern implementation, the function referenced by the proxyfunc variable has to be called before the current method implementation. The obvious solution is to assign the defined method to the proxyfunc variable reference, as follows: DefinedClass.prototype.defined = proxyfunc This solution is quirky, because the original method is not referenced by proxyfunc. The original method would seem to have disappeared, and when proxyfunc does attempt to call the original method, an error will result. The original method could follow a convention where the method implementation is referenced by another property:

   DefinedClass.prototype.originalDefined =  DefinedClass.prototype.defined;
   DefinedClass.prototype.defined = proxyfunc

proxyfunc, which is responsible for calling the original implementation using convention, knows that the original method is stored in the originalDefined property. So this seems OK, but in fact there is another problem. What if you have a proxy method of a proxy method? Since the originalDefined method is already defined, do you overwrite and lose the original method to be called? Another solution is to extend the convention to the following:

   DefinedClass.prototype.originalOriginalDefined  =
   DefinedClass.prototype.originalDefined;
   DefinedClass.prototype.originalDefined =  DefinedClass.prototype.defined;
   DefinedClass.prototype.defined = proxyfunc

The downside to this solution is that per the convention, the identifier contains a number of original identifiers. The convention requires that a specific proxy method be placed at a specific location; otherwise, the proxy method does not know whether to call originalDefined or orginalOriginalDefined. Another problem is, what if originalDefined referenced a real method? The solution to the proxy method article is not to create new properties, but to create embedded functions. Embedding functions makes it possible to declare a function that is not accessible externally, and the proxy method can be embedded within a proxy method. The generated JavaScript follows:

   DefinedClass.prototype.defined = function() {
   var origFunc = function (tstvalue) {
   info("DefinedClass.defined",  "parameter (" + tstvalue + ") arguments count (" +
   arguments.length + ")");
   assertEquals(tstvalue, this.value);
   }
   var proxyFunc = function (tstValue, toCall,  args) {
   info("proxyfunction", "parameter  (" + tstValue + ") received arg count (" + 
   arguments.length + ") original arg count  (" + args.length + ")");
   toCall.apply(this, args);
   }
   var args = new Array();
   for (var c1 = 0; c1 < arguments.length; c1  ++) {
   args.push(arguments[c1]);
   }
   args.push(origFunc);
   args.push(arguments);
   proxyFunc.apply(this, args);
   }

 

Two variables, origFunc and proxyFunc, reference functions. The origFunc variable represents the original implementation of the defined method. The proxyFunc variable represents the proxy method that is called before the original method. Both functions are embedded in a function that is responsible for calling the proxy method.

Embedding the functions within a function does not require the proxy method to know what the original function is called. For each instance of embedding, there is a proxy method and the original method. The convention is that the method that embeds the two methods keeps track of who calls whom and what parameters are passed around.

The role of the parent method is to manage the references of the embedded methods, manage the this instance, and manage the parameters. The parent method manages the parameters by considering the arguments as an Array type. The JavaScript-defined arguments object is an array of arguments. The parent method will create a new set of arguments so that the proxy method knows which function to call and has the ability to modify the arguments based on the original object instance. The purpose of creating an array of arguments that contains the arguments relates to the declaration of the proxy method, which will always have as its last parameters the method to call and arguments. Embedding a function within a function has a problem: how do you assign the this reference in the embedded function? JavaScript solves this problem by providing the apply method, which is associated with a function object.

legal disclaimer

Our website is not responsible for the information contained by this article. Web-articles is a free articles resource.
Suggestion: If you need fresh, daily updated content for your website, feel free to use our service. Click here for more information.

related articles

1. Understanding the Definition and Philosophy of REST
Understanding the Definition and Philosophy of REST REST is a controversial topic among Web service enthusiasts, because it’s considered to stand for the opposite of what Web services and SOA are trying to achieve. The problem with this thinking is that REST is not in contradiction with the abstract definition of SOA and Web services. REST is in contradiction with technologies such as SOAP, WSDL, and WS-* specifications. The following offers a quick definition of REST:...

2. The Easiest Way to Get Started with Ajax and REST
The Easiest Way to Get Started with Ajax and REST Problem You want to know the best way to get started with writing Ajax and REST. Solution When developing an Ajax and REST application, you must decide on the tools and frameworks you’ll use. The choice is simple: Use whatever you’re using today, and write some Ajax applications. You don’t need to change the tools you’re using today. Whether you’re using ASP.NET, JavaServer Pages (JSP), PHP, Ruby, or Python, you...

3. Testing a Dynamic Contract with Ajax
Coding the Contract Using Test-Driven Development Techniques Coding the contract using agile and test-driven development techniques requires writing a number of tests and implementing aMock URL layer. Problem You want to code the contract using these development techniques. Solution To demonstrate, let’s define a use case, implement the use case as a contract, write a test case(s) to implement the contract, implement the contract in the Mock URL, and finally...

4. Testing the Client Side Logic
Problem You want to effectively test your application’s client-side logic. Theory Testing GUI code tends not to be a productive task because of the complications that arise. The main complication is how to test the correctness of a user interface. Imagine a situation where clicking a button causes a table to be filled with data. Now imagine that when a check box is checked and the button is clicked again, a different table is filled with content. The fact that clicking the same button results in two ...

5. Understanding JavaScript and Types
Understanding JavaScript and Types Problem You want to work around the fact that JavaScript does not have types declared for its variables. Theory JavaScript code does not have any variables with a declared type. The lack of typed variables is apparent when you declare functions. That said, not having typed variable declarations does not mean JavaScript has no types or no type safety. Let’s start out with the simple declaration of a function, as illustrated by the following ex...

6. Coding Using Conventions and Not Configurations
Coding Using Conventions and Not Configurations Problem You want to make your JavaScript constructs more efficient by applying the Rails “convention over configuration” principle to them. Theory You may already be familiar with the programming platform Ruby on Rails, which is used to build Web applications. The focus of this recipe is not Ruby on Rails, but one aspect of Ruby on Rails namely, convention over configuration (see http://en.wikipedia.org/wiki/ Ruby_on_Rails for m...

7. Advantage of parameterless functions in JavaScript
Using Parameterless Functions Problem You want to take advantage of parameterless functions in JavaScript. Theory JavaScript functions for the most part have parameters. You may think that the previous sentence states the obvious after all, without parameters, what data could be passed to a function? JavaScript has the ability to declare functions that have no parameters, even though the caller of the function has passed parameters to the function. For example, let’s look at...