// example factories

/**
 * Factory to avoid even allocating any RAM for malformed objects.
 *
 * factory to create Integer objects only for 1 to 100
 *
 * @param anInt an integer 1..100
 *
 * @return the corresponding Integer object.
 * If anInt is out of range, it returns null.
 */
public static Integer smallIntegerFactory ( int anInt )
   {
   if ( 1 <= anInt && anInt <= 100 )
      {
      return  Integer.valueOf( anInt );
      }
   else
      {
      return null;
      }
   }

/**
 * Factory to create both Dalmation AND Dog objects.
 * The Dalmatian constructor is private,
 * so you can't call it directly.
 * This method forces users to register every Dalmatian
 * object they produce.
 *
 * @param spots count of spots
 */
public static Dog createDog ( int spots )
   {
   Dog d;
   if ( spots > 0 )
      {
      // The factory CAN call the private constructor,
      // but other classes cannot.
      d = new Dalmatian ( spots );
      }
   else if ( spots == 0 )
      {
      d = new Dog();
      }
   else
      {
      return null;
      }

   // enforce some processing on every newly created object.
   register( d );
   d.getShots();
   kennel.add( d );
   return d;
   }