Skip to content

Instantly share code, notes, and snippets.

@mangar
Created October 17, 2015 23:31
Show Gist options
  • Select an option

  • Save mangar/8504bfb6c4fe5633dfcf to your computer and use it in GitHub Desktop.

Select an option

Save mangar/8504bfb6c4fe5633dfcf to your computer and use it in GitHub Desktop.
AbstractDAO
import java.util.List;
import java.util.Map;
public interface DAO {
public Boolean save(Map<?, ?> param);
public List<?> getAll();
}
public class DAOFactory {
static DAOFactory instance;
static final String DB_TYPE_ORACLE = "oracle";
static final String DB_TYPE_MYSQL = "mysql";
static final String DB_TYPE = DB_TYPE_MYSQL;
private DAOFactory() {
super();
}
public static DAOFactory newInstance() {
if (DAOFactory.instance == null) {
DAOFactory.instance = new DAOFactory();
}
return DAOFactory.instance;
}
public DAO getDao() {
if (DAOFactory.DB_TYPE.equalsIgnoreCase(DB_TYPE_MYSQL)) {
return new DAOMySQL();
} else {
return new DAOOracle();
}
}
}
import java.util.List;
import java.util.Map;
public class DAOMySQL implements DAO {
@Override
public Boolean save(Map<?, ?> param) {
return null;
}
@Override
public List<?> getAll() {
return null;
}
}
import java.util.List;
import java.util.Map;
public class DAOOracle implements DAO {
@Override
public Boolean save(Map<?, ?> param) {
return null;
}
@Override
public List<?> getAll() {
return null;
}
}
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public abstract class DAOUsage {
public static void main(String args[]) {
List<?> allRecords = DAOFactory.newInstance().getDao().getAll();
Map<String,String> record = new HashMap<String, String>();
record.put("id", "1");
record.put("name", "User Name");
record.put("email", "Email");
DAOFactory.newInstance().getDao().save(record);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment