Source of UniversalMazeFactory.java


  1: 
  2: package maze; 
  3: 
  4: import java.util.*;
  5: import java.io.*;
  6: import java.awt.*;
  7: import javax.swing.*;
  8: 
  9: public class UniversalMazeFactory extends MazeFactory {
 10: 
 11:   public static MazeFactory getInstance() { 
 12:     if (theInstance == null) { 
 13:       if (usePrototype) { 
 14:         MazeFactory factory = null;
 15:         switch (theme) { 
 16:         case HARRY_PORTER_THEME: 
 17:           factory = new maze.harry.HarryPotterMazeFactory(); 
 18:           break; 
 19:         case SNOW_WHITE_THEME: 
 20:           factory = new maze.snow.SnowWhiteMazeFactory(); 
 21:           break; 
 22:         }
 23:         if (factory == null) { 
 24:           factory = new MazeFactory(); 
 25:         }
 26:         theInstance = new MazePrototypeFactory(factory.makeMaze(),
 27:                                                factory.makeWall(),
 28:                                                factory.makeRoom(0),
 29:                                                factory.makeDoor(null, null));
 30:       } else { 
 31:         switch (theme) { 
 32:         case HARRY_PORTER_THEME: 
 33:           theInstance = new maze.harry.HarryPotterMazeFactory();
 34:           break; 
 35:         case SNOW_WHITE_THEME: 
 36:           theInstance = new maze.snow.SnowWhiteMazeFactory();
 37:           break; 
 38:         default:
 39:           theInstance = new MazeFactory();
 40:           break; 
 41:         }
 42:       }
 43:     }
 44:     return theInstance; 
 45:   }
 46: 
 47:   protected UniversalMazeFactory() {}
 48: 
 49:   private static MazeFactory theInstance = null; 
 50: 
 51:   private static final int SIMPLE_THEME = 0; 
 52:   private static final int HARRY_PORTER_THEME = 1; 
 53:   private static final int SNOW_WHITE_THEME = 2; 
 54:   private static boolean usePrototype = true; 
 55:   private static int theme = SIMPLE_THEME; 
 56: 
 57:   static {
 58:     Properties configProperties = new Properties();
 59:     try {
 60:       configProperties.load(new FileInputStream("maze.properties")); 
 61:     } catch (IOException e) {}    
 62:     String value;
 63:     value = System.getProperty("maze.theme"); 
 64:     if (value == null) { 
 65:       value = configProperties.getProperty("maze.theme"); 
 66:     }
 67:     if (value != null) { 
 68:       if ("Harry".equals(value)) { 
 69:         theme = HARRY_PORTER_THEME; 
 70:       } else if ("Snow".equals(value)) {
 71:         theme = SNOW_WHITE_THEME;
 72:       }
 73:     }
 74:     
 75:     value = System.getProperty("maze.prototype"); 
 76:     if (value == null) { 
 77:       value = configProperties.getProperty("maze.prototype"); 
 78:     }
 79:     if (value != null) { 
 80:       usePrototype = Boolean.getBoolean(value);
 81:     }
 82:   }
 83: 
 84: 
 85:   public static void main(String[] args) { 
 86:     MazeFactory factory = UniversalMazeFactory.getInstance();
 87:     Maze maze = MazeGameAbstractFactory.createMaze(factory); 
 88:     maze.setCurrentRoom(1); 
 89:     maze.showFrame("Maze -- Universal Factory");
 90:   }
 91: 
 92: }