title: How can I use the Factory pattern to refactor my Java code?
body: <p>I would like to use the Factory pattern to make a new method addShip this determines in running time which type of Ship to initialize. </p>
  
  <pre><code>    while(!g.allShipsPlaced())
      {
          NumberGenerator gen = new NumberGenerator();
          int x = gen.rand(10);
          int y = gen.rand(10);
          int o = gen.rand(1);
  
          x = gen.rand(10);
          y = gen.rand(10);
          o = gen.rand(2);        
          System.out.println("vertical sub x = " + x + "\n");
          System.out.println("vertical sub y = " + y + "\n");
          g.addSub(x,y, o);
  
          x = gen.rand(10);
          y = gen.rand(10);
          o = gen.rand(2);        
          System.out.println("vertical battle x = " + x + "\n");
          System.out.println("vertical battle y = " + y + "\n");
          g.addBattle(x,y, o);    
  
          x = gen.rand(10);
          y = gen.rand(10);
          o = gen.rand(2);                
          System.out.println("vertical air x = " + x + "\n");
          System.out.println("vertical air y = " + y + "\n");
          g.addAir(x,y, o);
  
          x = gen.rand(10);
          y = gen.rand(10);
          o = gen.rand(2);                
          System.out.println("vertical mine x = " + x + "\n");
          System.out.println("vertical mine y = " + y + "\n");
          g.addMine(x,y, o);
  
          x = gen.rand(10);
          y = gen.rand(10);
          o = gen.rand(2);
          System.out.println("horizontal dest x = " + x + "\n");
          System.out.println("horizontal dest y = " + y + "\n");
          g.addDest(x,y, o);
  
      }
  
      System.out.println("agent grid");
      System.out.println(g.toString());
  
      return g;
  }
  </code></pre>
  
  <p>For example,</p>
  
  <pre><code>    x = gen.rand(10); 
      y = gen.rand(10);
      o = gen.rand(2);        
      System.out.println("vertical sub x = " + x + "\n");
      System.out.println("vertical sub y = " + y + "\n");
      g.addShip(x, y, o, "Submarine");
      /* x,y are the coordinates to define where to place the ship and o
      defines how to place the ship (horizontal or vertical) */
  </code></pre>
  
  <p>From the String type you can see which type of boat should be initialized, this would mean that you make a call to the addSubmarine method </p>
  
  <p>Here is a link to the original code <a href="https://github.com/michaelokarimia/battleships/tree/master/Battleships" rel="nofollow">https://github.com/michaelokarimia/battleships/tree/master/Battleships</a>. You can find this code in the Agent class. Any help is appreciated.</p>