我怎样才能避免这个 if 语句

发布于 2024-12-03 20:39:03 字数 1249 浏览 1 评论 0原文

我有一个枚举

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

    public  boolean isCar()
{
    ...
}

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

我有一个 2 DAO CarDAOPushBikeDAO 正在实现一个 BaseDao

我有一个 JSF 托管 bean 这样的东西

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
    }
}

我试图摆脱这个如果可能通过使用泛型或任何适当的面向对象技术来声明 可能类似于

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

如果 vehicle.getcode() 返回字符串值 Car 我确实有一个模型类 Car。

只是大声思考(真的抓住了吸管:))。

这是 我的

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 statement 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

一绘本一梦想 2024-12-10 20:39:03

向枚举添加一个方法,调用 new 并返回正确的 dao。

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

ゃ人海孤独症 2024-12-10 20:39:03

让每个枚举常量定义它们各自的 DAO 类:

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();
}

这样,您可以使用:

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

看一下 Factory方法模式策略模式(如果您想知道) 更多的。枚举是我使用后者的首选方式。

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.

小女人ら 2024-12-10 20:39:03

我会使用工厂方法,如下所示:

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();
    }
}

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();
    }
}
反差帅 2024-12-10 20:39:03

除非您对 DAO 对象有更多用途,否则可以缩短此代码:

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

有两种选择,我将继续使用 if 语句。如果您确实有很多车辆变体,则可以使用由枚举值作为键的哈希表并存储 DAO 类:

Map<Vehicle, Class> DAOClass = new HashMap<...>();
...
DAOClass.get(vehicle).getConstructor().newInstance().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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文