Try pseudo persistence for mock ups

It's possible to build Data Access Objects which mimic persistance, but whose data lasts only until the application shuts down. One might call this "pseudo-persistance", and such objects "pseudo-DAOs".

These stub classes are easy to construct, and can be useful for quick mock ups. They usually require less effort than providing true persistance. By following the style shown below, for example, a pseudo-DAO could be constructed in 10 minutes.

This example pseudo-DAO implements pseudo-persistence for Person objects. The intent is to create a pseudo-DAO whose methods will match the final, true DAO. Thus, the same class can evolve from a mock up into a final implementation, without affecting the caller in any way. From the caller's point of view, there should be little or no difference between the pseudo-DAO and the real DAO, including the checked exceptions thrown by its methods.

The underlying technique is simply to store objects in a static member of the pseudo-DAO. In this case, a Map is used.

import java.util.LinkedHashMap;
import java.util.Map;

/**
 Provides "pseudo-persistance", by storing objects as static data attached to this
 class. Such data is lost when the application is shut down, but is retained
 as long as the application is running.

 <P>The important thing is to match the desired style of the true DAO 
 into which this pseudo-DAO may evolve.

 <P>Note that <code>DataAccessException</code> (or similar - a checked exception) 
 can and should be declared in the throws clause, even when the underlying 
 implementation for this pseudo-DAO never throws such an exception. Again, 
 the idea is to provide a stub which, from the point of view of the caller, 
 is as realistic as possible.
*/
public final class HumanDao {

  /**
   Add <code>human</code> to the datastore, returning an indicator of
   success or failure.
  
   If <code>human</code> already exists in the datastore, then return false.
  */
  public boolean add(Human human) throws DataAccessException {
    boolean result = false;
    synchronized(humans) {
      if (! humans.containsKey(human.getName())) {
        humans.put(human.getName(), human);
        result = true;
        log("Humans after add: " + humans);
      }
    }
    return result;
  }

  /**
   Fetch a human from the datastore, using <code>name</code> as a unique
   identifier.
  
   If no <code>Herson</code> is found, return null.
  */
  public Human fetch(String name) throws DataAccessException {
    Human result = null;
    synchronized(humans){
      if(humans.containsKey(name)){
        result = humans.get(name);
      }
      log("Humans during fetch: " + humans);
    }
    return result;
  }

  /**
   Update the data associated with <code>human</code>, returning an
   indicator of success or failure.
  
   If <code>humann</code> is not found in the datastore, then return false.
  */
  public boolean change(Human human) throws DataAccessException {
    boolean result = false;
    synchronized(humans){
      if (humans.containsKey(human.getName())) {
        humans.put(human.getName(), human); //replaces old entry
        result = true;
        log("Humans after change: " + humans);
      }
    }
    return result;
  }

  /**
   Delete the <code>Herson</code> identified uniquely by <code>name</code>,
   returning an indicator of success or failure.
  
   If <code>name</code> is not found in the datastore, then return false.
  */
  public boolean delete(String name) throws DataAccessException {
    boolean result = false;
    synchronized(humans){
      if (humans.containsKey(name)) {
        humans.remove(name);
        result = true;
        log("Humans after delete: " + humans);
      }
    }
    return result;
  }

  // PRIVATE 

  /**
   Stores Human objects in a static field, attached to this class, which will
   remain available as long as this class remains loaded.
  */
  private static Map<String, Human> humans = new LinkedHashMap<>();

  private void log(String aText){
    System.out.println(aText);
  }
} 
 

See Also :
Data access objects