IMPLEMENTATION OF SINGLETON PATTERN

 

 

As explained Singleton Pattern ensure a class only has one instance, and provide a global point of access to it.

 

In this project, as bidding server is accessed by multiple clients at the same time. In order maintain consistency of data, there is a need to implement singleton pattern.  The code below explains how this can be accomplished. The BiddingServer defines an Interface named BiddingServices which extends the java.rmi.Remote to provide access to the remote methods for a client. The implementation of the interface is BiddingServicesImpl which handles all the client requests.  The BiddingServicesImpl uses a class named BidsManager. BidsManager is implemented using the Singleton Pattern because only a single instance of the BidsManager object should ever exist on the server to manage the bids. By following the Singleton Pattern we can guarantee that only a single instance of the object will ever exist for a given JVM.

 

 

public interface BiddingServices extends Remote {

 

  public void placeNurseTimeForBid(Nurse nurse) throws RemoteException;

  public boolean isPatientNameUnique(String patienName) throws RemoteException;

  public void bidOnNurseTime(BidDetails details) throws RemoteException;

  public List getBidableNurseTimes() throws RemoteException;

  public List getAllNurseTimes() throws RemoteException;

  public Collection getAllBidDetails() throws RemoteException;

}

 

 

public class BiddingServicesImpl extends UnicastRemoteObject implements BiddingServices {

 

    public BiddingServicesImpl() throws RemoteException {

        super();

    }

 

    public void placeNurseTimeForBid(Nurse nurse) throws RemoteException {

        BidsManager.getInstance().placeNurseTimeForBid(nurse);

    }

 

    public boolean isPatientNameUnique(String patientName) throws RemoteException {

        return BidsManager.getInstance().isPatientNameUnique(patientName);

    }

 

    public void bidOnNurseTime(BidDetails details) throws RemoteException {

        BidsManager.getInstance().bidOnNurseTime(details);

    }

 

    public List getBidableNurseTimes() throws RemoteException {

        return new ArrayList(BidsManager.getInstance().getBidableNurseTimes());

    }

 

    public List getAllNurseTimes() throws RemoteException {

        return new ArrayList(BidsManager.getInstance().getAllNurseTimes());

    }

 

    public Collection getAllBidDetails() throws RemoteException {

      return BidsManager.getInstance().getAllBidDetails();

    }

}