Singleton Pattern:
The
AuctionServer defines an Interface named AuctionServices which extends the
java.rmi.Remote to provide the remote methods for a client. The implementation
of the interface is AuctionServicesImpl which
handles all the client requests. The
AuctionServicesImpl 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.
The code
is as follows:
public interface AuctionServices extends Remote {
public void placeItemForBid(Item item) throws
RemoteException;
public boolean isItemNameUnique(String itemName) throws RemoteException;
public void bidOnItem(BidDetails details)
throws RemoteException;
public List getBidableItems() throws RemoteException;
public List getAllItems() throws RemoteException;
public Collection getAllBidDetails() throws RemoteException;
public void
lock(Item item) throws RemoteException;
public void unLock(Item item) throws RemoteException;
}
public class AuctionServicesImpl extends UnicastRemoteObject
implements AuctionServices {
public
AuctionServicesImpl() throws RemoteException {
super();
}
public void placeItemForBid(Item item) throws
RemoteException {
BidsManager.getInstance().placeAnItem(item);
}
public boolean isItemNameUnique(String itemName) throws RemoteException
{
return BidsManager.getInstance().isItemNameUnique(itemName);
}
public void bidOnItem(BidDetails details)
throws RemoteException {
BidsManager.getInstance().bidOnItem(details);
}
public List getBidableItems() throws RemoteException
{
return new ArrayList(BidsManager.getInstance().getBidableItems());
}
public List getAllItems() throws RemoteException
{
return new ArrayList(BidsManager.getInstance().getItems());
}
public Collection getAllBidDetails() throws RemoteException
{
return BidsManager.getInstance().getAllBidDetails();
}
public void
lock(Item item) throws RemoteException
{
BidsManager.getInstance().lock(item);
}
public void unLock(Item item) throws RemoteException {
BidsManager.getInstance().clearLock(item);
}
}