Refactoring

This commit is contained in:
Thibaud Gasser 2019-01-03 00:07:26 +01:00
parent 4cde30a6e0
commit a30e86016c
18 changed files with 101 additions and 84 deletions

View File

@ -1,16 +1,16 @@
package fr.gasser.daoexample.dao; package fr.gasser.daoexample.dao;
import fr.gasser.daoexample.model.Entity; import fr.gasser.daoexample.model.Entity;
import fr.gasser.daoexample.sql.Connection; import fr.gasser.daoexample.dao.connection.ConnectionWrapper;
import java.util.List; import java.util.List;
public abstract class Dao<T extends Entity> { public abstract class Dao<T extends Entity> {
protected Connection connection; protected final ConnectionWrapper connectionWrapper;
public Dao(Connection connection) { protected Dao(ConnectionWrapper connectionWrapper) {
this.connection = connection; this.connectionWrapper = connectionWrapper;
} }
public abstract boolean create(T obj); public abstract boolean create(T obj);

View File

@ -1,5 +1,8 @@
package fr.gasser.daoexample.dao; package fr.gasser.daoexample.dao;
import fr.gasser.daoexample.dao.dummy.DaoFactory;
import fr.gasser.daoexample.dao.dummy.FactoryType;
import fr.gasser.daoexample.dao.sqlite.SqliteDaoFactory;
import fr.gasser.daoexample.model.Discipline; import fr.gasser.daoexample.model.Discipline;
import fr.gasser.daoexample.model.Student; import fr.gasser.daoexample.model.Student;
import fr.gasser.daoexample.model.Teacher; import fr.gasser.daoexample.model.Teacher;

View File

@ -1,26 +0,0 @@
package fr.gasser.daoexample.dao;
import fr.gasser.daoexample.model.Discipline;
import fr.gasser.daoexample.model.Student;
import fr.gasser.daoexample.model.Teacher;
import fr.gasser.daoexample.sql.Connection;
import fr.gasser.daoexample.sql.DummyConnection;
public class DaoFactory implements DaoAbstractFactory {
private static Connection connection = new DummyConnection();
@Override
public Dao<Student> createStudentDao() {
return new StudentDao(DaoFactory.connection);
}
@Override
public Dao<Teacher> createTeacherDao() {
return new TeacherDao(DaoFactory.connection);
}
@Override
public Dao<Discipline> createDisciplineDao() {
return new DisciplineDao(DaoFactory.connection);
}
}

View File

@ -1,18 +1,22 @@
package fr.gasser.daoexample.dao; package fr.gasser.daoexample.dao;
import fr.gasser.daoexample.model.Discipline;
import fr.gasser.daoexample.model.Student;
import fr.gasser.daoexample.model.Teacher;
public class XmlDaoFactory implements DaoAbstractFactory { public class XmlDaoFactory implements DaoAbstractFactory {
@Override @Override
public StudentDao createStudentDao() { public Dao<Student> createStudentDao() {
return null; return null;
} }
@Override @Override
public TeacherDao createTeacherDao() { public Dao<Teacher> createTeacherDao() {
return null; return null;
} }
@Override @Override
public DisciplineDao createDisciplineDao() { public Dao<Discipline> createDisciplineDao() {
return null; return null;
} }
} }

View File

@ -1,8 +1,8 @@
package fr.gasser.daoexample.sql; package fr.gasser.daoexample.dao.connection;
import java.sql.SQLException; import java.sql.SQLException;
public interface Connection { public interface ConnectionWrapper {
void connect() throws SQLException; void connect() throws SQLException;
void close() throws SQLException; void close() throws SQLException;

View File

@ -1,6 +1,6 @@
package fr.gasser.daoexample.sql; package fr.gasser.daoexample.dao.connection;
public class DummyConnection implements Connection { public class DummyConnectionWrapper implements ConnectionWrapper {
@Override @Override
public void connect() { public void connect() {

View File

@ -1,8 +1,8 @@
package fr.gasser.daoexample.sql; package fr.gasser.daoexample.dao.connection;
import java.sql.SQLException; import java.sql.SQLException;
public class SQLiteConnectionException extends SQLException { class SQLiteConnectionException extends SQLException {
public SQLiteConnectionException(String message) { public SQLiteConnectionException(String message) {
super(message); super(message);
} }

View File

@ -1,4 +1,4 @@
package fr.gasser.daoexample.sql; package fr.gasser.daoexample.dao.connection;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -6,17 +6,17 @@ import org.slf4j.LoggerFactory;
import java.sql.DriverManager; import java.sql.DriverManager;
import java.sql.SQLException; import java.sql.SQLException;
public class SQLiteConnection implements Connection { public class SQLiteConnectionWrapper implements ConnectionWrapper {
private static final Logger LOGGER = LoggerFactory.getLogger(SQLiteConnection.class); private static final Logger LOGGER = LoggerFactory.getLogger(SQLiteConnectionWrapper.class);
private String dbpath; private final String dbpath;
private java.sql.Connection connection = null; private java.sql.Connection connection = null;
public SQLiteConnection(String dBPath) { private SQLiteConnectionWrapper(String dBPath) {
dbpath = dBPath; dbpath = dBPath;
} }
public SQLiteConnection() { public SQLiteConnectionWrapper() {
this(":memory:"); this(":memory:");
} }

View File

@ -0,0 +1,28 @@
package fr.gasser.daoexample.dao.dummy;
import fr.gasser.daoexample.dao.Dao;
import fr.gasser.daoexample.dao.DaoAbstractFactory;
import fr.gasser.daoexample.model.Discipline;
import fr.gasser.daoexample.model.Student;
import fr.gasser.daoexample.model.Teacher;
import fr.gasser.daoexample.dao.connection.ConnectionWrapper;
import fr.gasser.daoexample.dao.connection.DummyConnectionWrapper;
public class DaoFactory implements DaoAbstractFactory {
private static final ConnectionWrapper CONNECTION_WRAPPER = new DummyConnectionWrapper();
@Override
public Dao<Student> createStudentDao() {
return new StudentDao(DaoFactory.CONNECTION_WRAPPER);
}
@Override
public Dao<Teacher> createTeacherDao() {
return new TeacherDao(DaoFactory.CONNECTION_WRAPPER);
}
@Override
public Dao<Discipline> createDisciplineDao() {
return new DisciplineDao(DaoFactory.CONNECTION_WRAPPER);
}
}

View File

@ -1,7 +1,8 @@
package fr.gasser.daoexample.dao; package fr.gasser.daoexample.dao.dummy;
import fr.gasser.daoexample.dao.Dao;
import fr.gasser.daoexample.model.Discipline; import fr.gasser.daoexample.model.Discipline;
import fr.gasser.daoexample.sql.Connection; import fr.gasser.daoexample.dao.connection.ConnectionWrapper;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
@ -16,8 +17,8 @@ public class DisciplineDao extends Dao<Discipline> {
new Discipline(2, "Qsdf") new Discipline(2, "Qsdf")
)); ));
public DisciplineDao(Connection connection) { public DisciplineDao(ConnectionWrapper connectionWrapper) {
super(connection); super(connectionWrapper);
} }
@Override @Override

View File

@ -1,4 +1,4 @@
package fr.gasser.daoexample.dao; package fr.gasser.daoexample.dao.dummy;
public enum FactoryType { public enum FactoryType {
DaoFactory, DaoFactory,

View File

@ -1,7 +1,8 @@
package fr.gasser.daoexample.dao; package fr.gasser.daoexample.dao.dummy;
import fr.gasser.daoexample.dao.Dao;
import fr.gasser.daoexample.model.Student; import fr.gasser.daoexample.model.Student;
import fr.gasser.daoexample.sql.Connection; import fr.gasser.daoexample.dao.connection.ConnectionWrapper;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
@ -16,8 +17,8 @@ public class StudentDao extends Dao<Student> {
new Student(2, "Qsdf", "Jklm")) new Student(2, "Qsdf", "Jklm"))
); );
StudentDao(Connection connection) { StudentDao(ConnectionWrapper connectionWrapper) {
super(connection); super(connectionWrapper);
} }
@Override @Override

View File

@ -1,7 +1,8 @@
package fr.gasser.daoexample.dao; package fr.gasser.daoexample.dao.dummy;
import fr.gasser.daoexample.dao.Dao;
import fr.gasser.daoexample.model.Teacher; import fr.gasser.daoexample.model.Teacher;
import fr.gasser.daoexample.sql.Connection; import fr.gasser.daoexample.dao.connection.ConnectionWrapper;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
@ -16,8 +17,8 @@ public class TeacherDao extends Dao<Teacher> {
new Teacher(2, "Sit", "Amet")) new Teacher(2, "Sit", "Amet"))
); );
TeacherDao(Connection connection) { TeacherDao(ConnectionWrapper connectionWrapper) {
super(connection); super(connectionWrapper);
} }
@Override @Override

View File

@ -1,14 +1,16 @@
package fr.gasser.daoexample.dao; package fr.gasser.daoexample.dao.sqlite;
import fr.gasser.daoexample.dao.Dao;
import fr.gasser.daoexample.dao.DaoAbstractFactory;
import fr.gasser.daoexample.model.Discipline; import fr.gasser.daoexample.model.Discipline;
import fr.gasser.daoexample.model.Student; import fr.gasser.daoexample.model.Student;
import fr.gasser.daoexample.model.Teacher; import fr.gasser.daoexample.model.Teacher;
import fr.gasser.daoexample.sql.Connection; import fr.gasser.daoexample.dao.connection.ConnectionWrapper;
import fr.gasser.daoexample.sql.SQLiteConnection; import fr.gasser.daoexample.dao.connection.SQLiteConnectionWrapper;
public class SqliteDaoFactory implements DaoAbstractFactory { public class SqliteDaoFactory implements DaoAbstractFactory {
private static Connection conn = new SQLiteConnection(); private static final ConnectionWrapper conn = new SQLiteConnectionWrapper();
@Override @Override
public Dao<Student> createStudentDao() { public Dao<Student> createStudentDao() {

View File

@ -1,7 +1,10 @@
package fr.gasser.daoexample.dao; package fr.gasser.daoexample.dao.sqlite;
import fr.gasser.daoexample.dao.Dao;
import fr.gasser.daoexample.dao.DaoAbstractFactory;
import fr.gasser.daoexample.dao.dummy.FactoryType;
import fr.gasser.daoexample.model.Student; import fr.gasser.daoexample.model.Student;
import fr.gasser.daoexample.sql.Connection; import fr.gasser.daoexample.dao.connection.ConnectionWrapper;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -16,18 +19,18 @@ public class StudentSQLiteDao extends Dao<Student> {
private static final Logger LOGGER = LoggerFactory.getLogger(StudentSQLiteDao.class); private static final Logger LOGGER = LoggerFactory.getLogger(StudentSQLiteDao.class);
public StudentSQLiteDao(Connection connection) { StudentSQLiteDao(ConnectionWrapper connectionWrapper) {
super(connection); super(connectionWrapper);
try { try {
connection.connect(); connectionWrapper.connect();
dropAndCreateTable(connection); dropAndCreateTable(connectionWrapper);
} catch (SQLException e) { } catch (SQLException e) {
LOGGER.error("Unable to connect to the database.", e); LOGGER.error("Unable to connect to the database.", e);
} }
} }
private void dropAndCreateTable(Connection connection) throws SQLException { private void dropAndCreateTable(ConnectionWrapper connectionWrapper) throws SQLException {
try (Statement st = connection.getConnection().createStatement()) { try (Statement st = connectionWrapper.getConnection().createStatement()) {
st.setQueryTimeout(30); // set timeout to 30 sec. st.setQueryTimeout(30); // set timeout to 30 sec.
st.executeUpdate("DROP TABLE IF EXISTS students"); st.executeUpdate("DROP TABLE IF EXISTS students");
// In SQLite, table rows normally have a 64-bit signed integer ROWID // In SQLite, table rows normally have a 64-bit signed integer ROWID
@ -41,7 +44,7 @@ public class StudentSQLiteDao extends Dao<Student> {
@Override @Override
public boolean create(Student obj) { public boolean create(Student obj) {
final String sql = "INSERT INTO students (name, surname) VALUES (?, ?)"; final String sql = "INSERT INTO students (name, surname) VALUES (?, ?)";
try (PreparedStatement st = connection.getConnection().prepareStatement(sql)) { try (PreparedStatement st = connectionWrapper.getConnection().prepareStatement(sql)) {
st.setObject(1, obj.getName()); st.setObject(1, obj.getName());
st.setObject(2, obj.getSurname()); st.setObject(2, obj.getSurname());
final int ret = st.executeUpdate(); final int ret = st.executeUpdate();
@ -55,7 +58,7 @@ public class StudentSQLiteDao extends Dao<Student> {
@Override @Override
public Student find(int id) { public Student find(int id) {
try (PreparedStatement st = connection.getConnection().prepareStatement("SELECT rowid,* FROM students WHERE rowid = ?")) { try (PreparedStatement st = connectionWrapper.getConnection().prepareStatement("SELECT rowid,* FROM students WHERE rowid = ?")) {
st.setObject(1, id); st.setObject(1, id);
final ResultSet rs = st.executeQuery(); final ResultSet rs = st.executeQuery();
final String name = rs.getString("name"); final String name = rs.getString("name");
@ -70,7 +73,7 @@ public class StudentSQLiteDao extends Dao<Student> {
@Override @Override
public List<Student> findAll() { public List<Student> findAll() {
List<Student> results = new ArrayList<>(); List<Student> results = new ArrayList<>();
try (Statement st = connection.getConnection().createStatement()) { try (Statement st = connectionWrapper.getConnection().createStatement()) {
ResultSet rs = st.executeQuery("select rowid,* from students"); ResultSet rs = st.executeQuery("select rowid,* from students");
while (rs.next()) { while (rs.next()) {
final int id = rs.getInt("rowid"); final int id = rs.getInt("rowid");
@ -86,7 +89,7 @@ public class StudentSQLiteDao extends Dao<Student> {
@Override @Override
public boolean update(Student obj) { public boolean update(Student obj) {
try (PreparedStatement st = connection.getConnection().prepareStatement("UPDATE students SET name = ?, surname = ? WHERE rowid = ?")) { try (PreparedStatement st = connectionWrapper.getConnection().prepareStatement("UPDATE students SET name = ?, surname = ? WHERE rowid = ?")) {
st.setString(1, obj.getName()); st.setString(1, obj.getName());
st.setString(2, obj.getSurname()); st.setString(2, obj.getSurname());
st.setInt(3, obj.getId()); st.setInt(3, obj.getId());
@ -101,7 +104,7 @@ public class StudentSQLiteDao extends Dao<Student> {
@Override @Override
public boolean delete(Student obj) { public boolean delete(Student obj) {
String deleteSQL = "DELETE FROM students WHERE rowid = ?"; String deleteSQL = "DELETE FROM students WHERE rowid = ?";
try (PreparedStatement st = connection.getConnection().prepareStatement(deleteSQL)) { try (PreparedStatement st = connectionWrapper.getConnection().prepareStatement(deleteSQL)) {
st.setInt(1, obj.getId()); st.setInt(1, obj.getId());
st.executeUpdate(); st.executeUpdate();
return true; return true;
@ -114,7 +117,7 @@ public class StudentSQLiteDao extends Dao<Student> {
/** /**
* Quick testing of CRUD operations * Quick testing of CRUD operations
* @param args * TODO: write some unit tests
*/ */
public static void main(String[] args) { public static void main(String[] args) {
DaoAbstractFactory df = DaoAbstractFactory.createFactory(FactoryType.SqliteDaoFactory); DaoAbstractFactory df = DaoAbstractFactory.createFactory(FactoryType.SqliteDaoFactory);

View File

@ -32,15 +32,15 @@ public class Discipline extends Entity {
return teachers.add(teacher); return teachers.add(teacher);
} }
public boolean removeTeacher(Object o) { public boolean removeTeacher(Teacher t) {
return teachers.remove(o); return teachers.remove(t);
} }
public boolean addStudent(Student student) { public boolean addStudent(Student student) {
return students.add(student); return students.add(student);
} }
public boolean removeStudent(Object o) { public boolean removeStudent(Student s) {
return students.remove(o); return students.remove(s);
} }
} }

View File

@ -2,9 +2,9 @@ package fr.gasser.daoexample.model;
public class Entity { public class Entity {
protected int id; int id;
public Entity(int id) { Entity(int id) {
this.id = id; this.id = id;
} }

View File

@ -44,8 +44,8 @@ public class Teacher extends Entity {
return disciplines.add(discipline); return disciplines.add(discipline);
} }
public boolean removeDiscipline(Object o) { public boolean removeDiscipline(Discipline discipline) {
return disciplines.remove(o); return disciplines.remove(discipline);
} }
} }