java和sqlite的复合实体dao性能

0x6upsns  于 2021-07-06  发布在  Java
关注(0)|答案(0)|浏览(279)

首先,对不起我的英语。
我对道和实体有个问题。我开发一个应用程序已经很长时间了。开始时,我使用以下方法:

class Entity {
  int id;
  String name;
}

class Composed {
  int id;
  // others prop's
  Entity e;
}

class OtherComposed {
  int id
  // others prop's
  Composed c;
}

DAOEntity {
  private Connection c;
  DAOEntity {
     c = new FactoryDB.getConnection();
  }

  // cruds methods
  public List<Entity> findAll() {
    String findAll = "LONG QUERY";
    List<Entity> entities = new ArrayList();
    // dao's implements
    // make objects
    Entity e = new Entity(id, name);
    entities.add(e);
    return entities;
  }

  public Entity find(int id) {
    String find = "LONG QUERY";
    // dao's implements
    // make objects
    Entity e = new Entity(id, name);
    return e;
  }
}

DAOComposed {
  private Connection c;
  DAOComposed {
     c = new FactoryDB.getConnection();

  }

  // cruds methods
  public List<Composed> findAll() {
    String findAll = "LONG QUERY" + "INNER JOIN";
    List<Entity> composited = new ArrayList();
    // dao's implements
    // make objects
    Entity e = new Entity(id, name);
    Composed com = new Composed(id, e);
    composited.add();
    return composited;
  }

  public Composed find(int id) {
    String find = "LONG QUERY" + "INNER JOIN";
    // dao's implements
    // make objects
    Entity e = new Entity(id, name);
    Composed com = new Composed(id, e);
    return com;
  }

}

DAOOtherComposed {
  private Connection c;
  DAOOtherComposed {
     c = new FactoryDB.getConnection();
  }

  // cruds methods
  public List<Composed> findAll() {
    String findAll = "MONSTER QUERY" + "LONG QUERY" + "INNER JOIN";
    List<Entity> otherComposited = new ArrayList();
    // dao's implements
    // make objects
    Entity e = new Entity(id, name);
    Composed com = new Composed(id, e);
    OtherComposed oc = new OtherComposed(id, com);
    otherComposited.add(oc);
    return otherComposited;
  }

  public OtherComposed find(int id) {
    String find = "MONSTER QUERY" + "LONG QUERY" + "INNER JOIN";
    // dao's implements
    // make objects
    Entity e = new Entity(id, name);
    Composed com = new Composed(id, e);
    OtherComposed oc = new OtherComposed(id, c);
    return oc;
  }

}

好。这种方法在性能上是好的,但在维护和调试上却很糟糕。我读了一些关于lambdas和其他东西的书,我将dao的逻辑更改为以下方法:

DAOEntity {
    private Connection c;
    DAOEntity {
     c = new FactoryDB.getConnection();
  }

  // cruds methods
  public List<Entity> findAll() {
    String findAll = "LONG QUERY";
    List<Entity> entities = new ArrayList();
    // dao's implements
    // make objects
    Entity e = new Entity(id, name);
    entities.add(e);
    return entities;
  }

  public Entity find(int id) {
    Entity e = findAll.stream.filter...;
    return e;
  }
}

DAOComposed {
  private Connection c;
  DAOComposed {
     c = new FactoryDB.getConnection();
     daoe = new DAOEntity();
  }

  // cruds methods
  public List<Composed> findAll() {
    String findAll = "SHORT QUERY" + "INNER JOIN";
    List<Entity> composited = new ArrayList();
    // dao's implements
    // make objects
    Entity e = daoe.find(id);
    Composed com = new Composed(id, e);
    composited.add();
    return composited;
  }

  public Composed find(int id) {
    Composed com = findAll.stream.filter...;
    return com;
  }

}

DAOOtherComposed {
  private Connection c;
  private DAOComposed daoc;
  DAOOtherComposed {
     c = new FactoryDB.getConnection();
     daoc = new DAOComposed();
  }

  // cruds methods
  public List<Composed> findAll() {
    String findAll = "LONG QUERY" + "SHORT QUERY" + "INNER JOIN";
    List<Entity> othercomposited = new ArrayList();
    // dao's implements
    // make objects
    Composed com = daoc.find(id);
    OtherComposed oc = new OtherComposed(id, com);
    othercomposited.add();
    return composited;
  }

  public OtherComposed find(int id) {
    OtherComposed oc = findAll.stream.filter...;        
    return oc;
  }

}

这种方法对我来说更简单更实用。但是有一个很大的问题:对于每一个新dao(dao dao=newdao),响应时间增加1秒。在本例中,实体有一秒,由两秒组成,其他由3秒组成(甚至更多)。这种方法不好,还是设计得不好?有更好的维护和调试方法吗?第一种方法速度很快,但多次复制/粘贴代码;第二种方法比较抽象,但存在此问题。
我的数据库是sqlite。再次为我的英语道歉。提前谢谢。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题