IMPLEMENTATION OF MEDIATOR PATTERN

 

 

As explained Mediator Pattern defines an object that encapsulates how a set of objects interact. It defines how communication between objects can be simplified by using a separate object to keep all objects from having to know about each other. Mediator promotes loose coupling by keeping objects from referring to each other explicitly, and it lets you vary their interaction independently.

 

In this project, an object is defined that encapsulates how a client does a server lookup. The code below(mediator class) explains how lookupBiddingServices( ) method of the Mediator class encapsulates the lookup of server by the client. This method is called by the client to do a server lookup. Clients need to do a lookup service before getting access to the remote interfaces defined in the project. Mediator Pattern is used to perform this operation(lookup). By doing this, server lookup code will not be spread out in the client side code base. The mediator class for this project do an RMI lookup. If any time in future we need the server from an RMI implementation to CORBA then nothing in the client changes except the Mediator class for CORBA lookup.

 

public class Mediator {

 

    public static void setConfig(String[] args) {

      HOST_NAME = "localhost";

      String portNum = "1099";

      if( args != null && args.length > 0 ) {

        HOST_NAME = args[0];

      }

      if( args != null && args.length > 1 ) {

        portNum = args[1];

      }

 

      PORT = Integer.parseInt(portNum);

    }

 

    public static BiddingServices lookupBiddingServices() {

        try {

            String registryURL = "rmi://" +HOST_NAME+ ":" +PORT+"/Bidding";

            //find the remote object and cast it to an interface object

            BiddingServices service = (BiddingServices) Naming.lookup(registryURL);

            //System.out.println("Lookup completed ");

            return service;

        } catch(Exception e) {

            System.out.println("RMI Bidding Server lookup failed with error "+e.getMessage());

            System.exit(1);

        }

        return null;

    }

 

    private static String HOST_NAME;

    private static int PORT;

}