开发者

How can I avoid this if statement

开发者 https://www.devze.com 2023-04-03 03:53 出处:网络
I have an enum public enum Vehicle { CAR(\"CAR\", \"Car\"), PUSHBIKE(\"PUSHBIKE\", \"PuschBike\"); publicboolean isCar()

I have an enum

public enum Vehicle {
    CAR("CAR", "Car"), PUSHBIKE("PUSHBIKE", "PuschBike");

    public  boolean isCar()
{
    ...
}

    public  boolean isPushBike()
{
    ....
}
}

I have a 2 DAO CarDAO and PushBikeDAO which is are implementing a BaseDao

I have a JSF managed bean somthing like this

public class JsfManagedBean {

    private Vehicle vehicle;

    private BaseDAO baseDao;

    public void Search()
    {
           //I need to get rid of this if statement
        if (vehicle.isCar())
        {
            baseDao = new CarDao;
            baseDao.search();
        }
        else if(vehicle.isPushBike())
        {
            baseDao = new PushBike;
            baseDao.search();
        }
      //Please Note each type of search is very different call to an other party's Jar
    }
}

I am trying to get rid of this if statem开发者_开发问答ent possibly by using generics or any proper OO technique may be something like

baseDao = new baseDaoImpl<getClass(vehicle.getcode())> 

where if vehicle.getcode() returns String value Car I do have a model class Car.

Just loud thinking (clinching the straws really :)).

This is an offshot of this question of mine


Add a method to the enum that calls new and returns the right dao.


Let each of the enum constants define their respective DAO classes:

public enum Vehicle {
  CAR("CAR", "Car"){
    public BaseDAO getNewDAO(){
      return new CarDAO();
    }
  },
  PUSHBIKE("PUSHBIKE", "PuschBike"){
    public BaseDAO getNewDAO() {
      return new PushBikeDAO();
    }
  };

  Vehicle(String a, String b){/* ... */}
  //this forces every enum constant to implement a getNewDAO() method:
  abstract BaseDAO getNewDAO();
}

This way, you can use:

public void Search() {
  baseDao = vehicle.getNewDAO();
  baseDao.search();
}

Take a look at the Factory method pattern and the Strategy pattern if you'd like to know more. Enums are my preferred way to use the latter.


I would use a factory method, like so:

public class JsfManagedBean {

    private static final Map<Vehicle,BaseDAO> daos;
    static {
        Map<Vehicle,BaseDAO> tmp = new HashMap<Vehicle,BaseDAO>();
        tmp.put(Vehicle.CAR,new CarDAO());
        tmp.put(Vehicle.BIKE,new BikeDAO());
        daos = Collections.unmodifiableMap(tmp);
    }
    public static getDao(Vehicle v) {
        return daos.get(v);
    }

    private Vehicle vehicle;

    private BaseDAO baseDao;

    public void Search()
    {
            baseDao = getDao(vehicle);
            baseDao.search();
    }
}


Unless you have more uses for DAO objects, you could make this code shorter:

if (vehicle.isCar()) new CarDao().search();
else if(vehicle.isPushBike()) new PushbikeDao().search();

With two alternatives, I'd stay with the if statement. If you had really many variants of vehicles, you could use a hash table keyed by the enum values and storing the DAO classes:

Map<Vehicle, Class> DAOClass = new HashMap<...>();
...
DAOClass.get(vehicle).getConstructor().newInstance().search();

Reflection is not that slow not to use here.

0

精彩评论

暂无评论...
验证码 换一张
取 消