title: How to avoid 'instanceof' when implementing factory design pattern?
body: <p>I am attempting to implement my first Factory Design Pattern, and I'm not sure how to avoid using instanceof when adding the factory-made objects to lists. This is what I'm trying to do:</p>
  <pre><code>for (Blueprint bp : blueprints) {
      Vehicle v = VehicleFactory.buildVehicle(bp);
      allVehicles.add(v);
                  
      // Can I accomplish this without using 'instanceof'?
      if (v instanceof Car) {
          cars.add((Car) v);
      } else if (v instanceof Boat) {
          boats.add((Boat) v);
      } else if (v instanceof Plane) {
          planes.add((Plane) v);
      }
  }
  </code></pre>
  <p>From what I've read on Stack Overflow, using 'instanceof' is a code smell. Is there a better way to check the type of vehicle that was created by the factory without using 'instanceof'?</p>
  <p>I welcome any feedback/suggestions on my implementation as I'm not even sure if I'm going about this the right way.</p>
  <p>Full example below:</p>
  <pre><code>import java.util.ArrayList;
  
  class VehicleManager {
      
      public static void main(String[] args) {
          
          ArrayList&lt;Blueprint&gt; blueprints = new ArrayList&lt;Blueprint&gt;();
          ArrayList&lt;Vehicle&gt; allVehicles = new ArrayList&lt;Vehicle&gt;();
          ArrayList&lt;Car&gt; cars = new ArrayList&lt;Car&gt;();
          ArrayList&lt;Boat&gt; boats = new ArrayList&lt;Boat&gt;();
          ArrayList&lt;Plane&gt; planes = new ArrayList&lt;Plane&gt;();
          
          /*
          *  In my application I have to access the blueprints through an API
          *  b/c they have already been created and stored in a data file.
          *  I'm creating them here just for example.
          */
          Blueprint bp0 = new Blueprint(0);
          Blueprint bp1 = new Blueprint(1);
          Blueprint bp2 = new Blueprint(2);
          blueprints.add(bp0);
          blueprints.add(bp1);
          blueprints.add(bp2);
          
          for (Blueprint bp : blueprints) {
              Vehicle v = VehicleFactory.buildVehicle(bp);
              allVehicles.add(v);
              
              // Can I accomplish this without using 'instanceof'?
              if (v instanceof Car) {
                  cars.add((Car) v);
              } else if (v instanceof Boat) {
                  boats.add((Boat) v);
              } else if (v instanceof Plane) {
                  planes.add((Plane) v);
              }
          }
          
          System.out.println(&quot;All Vehicles:&quot;);
          for (Vehicle v : allVehicles) {
              System.out.println(&quot;Vehicle: &quot; + v + &quot;, maxSpeed: &quot; + v.maxSpeed);
          }
          
          System.out.println(&quot;Cars:&quot;);
          for (Car c : cars) {
              System.out.println(&quot;Car: &quot; + c + &quot;, numCylinders: &quot; + c.numCylinders);
          }
          
          System.out.println(&quot;Boats:&quot;);
          for (Boat b : boats) {
              System.out.println(&quot;Boat: &quot; + b + &quot;, numRudders: &quot; + b.numRudders);
          }
          
          System.out.println(&quot;Planes:&quot;);
          for (Plane p : planes) {
              System.out.println(&quot;Plane: &quot; + p + &quot;, numPropellers: &quot; + p.numPropellers);
          }
      }
  }
  
  class Vehicle {
      
      double maxSpeed;
      
      Vehicle(double maxSpeed) {
          this.maxSpeed = maxSpeed;
      }
  }
  
  class Car extends Vehicle {
      
      int numCylinders;
      
      Car(double maxSpeed, int numCylinders) {
          super(maxSpeed);
          this.numCylinders = numCylinders;
      }
  }
  
  class Boat extends Vehicle {
      
      int numRudders;
      
      Boat(double maxSpeed, int numRudders) {
          super(maxSpeed);
          this.numRudders = numRudders;
      }
  }
  
  class Plane extends Vehicle {
      
      int numPropellers;
      
      Plane(double maxSpeed, int numPropellers) {
          super(maxSpeed);
          this.numPropellers = numPropellers;
      }
  }
  
  class VehicleFactory {
      
      public static Vehicle buildVehicle(Blueprint blueprint) {
          
          switch (blueprint.type) {
              
              case 0:
                  return new Car(100.0, 4);
                  
              case 1:
                  return new Boat(65.0, 1);
                  
              case 2:
                  return new Plane(600.0, 2);
                  
              default:
                  return new Vehicle(0.0);
          }
      }
  }
  
  class Blueprint {
      
      int type; // 0 = car; // 1 = boat; // 2 = plane;
      
      Blueprint(int type) {
          this.type = type;
      }
  }
  </code></pre>