New to patterns

Discussions

Web tier: servlets, JSP, Web frameworks: New to patterns

  1. New to patterns (2 messages)

    Hi,

    I am completely new to design patterns and J2EE so any help would be useful.

    I have a basic web page with an embedded flash movie which communicates with a single servlet.

    The servlet is basically a big if...else statement on the request params from the flash. Each if...else calling an appropriate method, doing some work and returning something to the flash movie.

    I have been asked by a customer if the server side servlet would/could be extendable, I want to redesign the whole approach to use a design pattern (is this the right thing to do?), but I don't know really where to start or even if this is the right thing to do. Any help ideas?

    I have started reading J2EE Design Patterns by William Crawford, Jonathan Kaplan (is this a good book?) and am thinking of something along the MVC lines with all the work done in a JavaBean, the customer will want to be able to add to the if...else in the controller servlet, preferably by some sort of extension without changing my code, how would something like this work? Is there a better pattern for what I'm trying to do?

    Any help will be much appreciated.

    Thanks in advance.

    Phil Maskell

    Threaded Messages (2)

  2. New to patterns[ Go to top ]

    There are lots of things that you can do, but here are some general tips.

    You need to split your one monolithic servlet into lots of smaller components. The best way to do this is to associate each component with a separate URL. This makes each component independent of all the others. If the flash application want to perform a different function, it invokes the URL of that component, and passes it the correct parameters.

    Your server side components can be implemented in several ways:

    1) Each component a separate servlet (mapped to the URL).

    2) One "Front Controller" servlet mapped to all URLs, which then loads the correct component (probably a simple Java object) into memory.

    This would be the best approach, but it means that you would also need to rewrite the Flash so that it invokes various server side components based on the URL.

    If rewriting the Flash is not practical, you can go with a variation on (2). Write a Front Controller servlet that:

    a) Determines which component to load based on the request parameters.

    b) Loads that component, and passes it the parameters.

    This is nowhere near as elegant, but may be a better approach if you cannot control the Flash code yourself.

    A final point on how to implement your Front Controller and your components:

    I) Define a configuration file that maps URLs (or parameter sets) to Java classes that implement a specific interface like:

    public interface Component {
      public void execute(Map parameters);
    }

    II) In the Front Controller servlet, read the config file, and compare it to the incoming URL (or parameter set).

    III) Load the component into memory using Class.forName(), put your parameters into the Map and invoke the execute() method.

    Check our Martin Fowler's "Patterns of Enterprise Architecture" book for an easy-to-follow discussion of Front Controller servlets.
  3. Flash, Flash Remoting[ Go to top ]

    I assume that you want to keep the Flash movie as your user interface.

    You should consider Flash Remoting

    1) http://www.onjava.com/pub/a/onjava/2003/02/26/flash_remoting.html

    2) http://www.openamf.org/ (open source Java Flash Remoting)

    3) Design Patterns for Flash Remoting
       http://www.macromedia.com/devnet/mx/flashremoting/articles/facades.html