Section 15.3. Building Faades on the Server


15.3. Building Fa §ades on the Server

As your application grows, so does the number of classes and components that it uses. In many situations, to complete an operation, you will have to affect multiple components at the same time. On these occasions, the fa §ade pattern comes to the rescue. Design Patterns (Addison Wesley) defines fa §ade as a way to "provide a unified interface to a set of interfaces in a subsystem. Fa §ade defines a higher level interface that makes the subsystem easier to use." In other words, it provides a unified frontend to a set of functionality that would normally be sparse. Here's a very simple example.

On the client side, we have a Chat component and a Notes component. We also have a Clear All button. When this button is clicked, the following call is made to the server side:

 nc.call("clearAll", null); 

The server-side script is something like this:

 Client.prototype.clearAll = function ( ) {   var chat_so = SharedObject.get("chat", true);   chat_so.setProperty("history", "");   var notes_so = SharedObject.get("notes", true);   notes_so.setProperty("note", ""); }; 

In a more realistic example, the function would call the appropriate method on each component it wants to clear, like this:

 Client.prototype.clearAll = function (  ) {   application.components.chat.clearHistory(  );   application.components.note.clear(  ); }; 

The effect is the same: one single call on the server-side, clearAll( ) , triggers seemingly unrelated onSync( ) calls on each client (one for the chat and one for the notes).

The important aspect of this pattern is the trickle-down effect of making a single call.

For a much more complex example of the fa §ade pattern, look at how the server-side part of each communication component is instantiated lazily (i.e., only when the first client-to-server call is made on it; the code is in your scriptlib/facade.asc file; see Chapter 14). Whenever a client-to-server call is first made on an uninstantiated component (say, with a call to FCChat.chat_mc.connect( ) ), it gets trapped by a __ resolve( ) method, which creates an instance of a class ( gFramework.components.FCChat , of type FCFactory ), which in turn creates an instance of another class ( gFramework.components.FCChat.instances.chat_mc , of type FCFacade ) which finally instantiates the component class and calls the method on it.

Once again, a simple client-to-server call triggered a blur of activity on the server side, with code touching a bunch of different classes behind the scenes.

So any time you find yourself modifying multiple shared objects or making calls to multiple parts of your application to achieve a single goal, wrapping the operation in a fa §ade function on the server side is often a good idea.




Programming Flash Communication Server
Programming Flash Communication Server
ISBN: 0596005040
EAN: 2147483647
Year: 2003
Pages: 203

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net