Generify Dao hierarchy

This commit is contained in:
Thibaud Gasser 2019-01-02 23:57:25 +01:00
parent 4cc19191c0
commit 0d9692992b
8 changed files with 41 additions and 24 deletions

View File

@ -17,7 +17,7 @@ public abstract class Dao<T extends Entity> {
public abstract T find(int id); public abstract T find(int id);
public abstract List<T> findAll(T obj); public abstract List<T> findAll();
public abstract boolean update(T obj); public abstract boolean update(T obj);

View File

@ -1,5 +1,9 @@
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 interface DaoAbstractFactory { public interface DaoAbstractFactory {
static DaoAbstractFactory createFactory(FactoryType type) { static DaoAbstractFactory createFactory(FactoryType type) {
@ -18,10 +22,10 @@ public interface DaoAbstractFactory {
return createFactory(FactoryType.DaoFactory); return createFactory(FactoryType.DaoFactory);
} }
StudentDao createStudentDao(); Dao<Student> createStudentDao();
TeacherDao createTeacherDao(); Dao<Teacher> createTeacherDao();
DisciplineDao createDisciplineDao(); Dao<Discipline> createDisciplineDao();
} }

View File

@ -1,5 +1,8 @@
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;
import fr.gasser.daoexample.sql.Connection; import fr.gasser.daoexample.sql.Connection;
import fr.gasser.daoexample.sql.DummyConnection; import fr.gasser.daoexample.sql.DummyConnection;
@ -7,17 +10,17 @@ public class DaoFactory implements DaoAbstractFactory {
private static Connection connection = new DummyConnection(); private static Connection connection = new DummyConnection();
@Override @Override
public StudentDao createStudentDao() { public Dao<Student> createStudentDao() {
return new StudentDao(DaoFactory.connection); return new StudentDao(DaoFactory.connection);
} }
@Override @Override
public TeacherDao createTeacherDao() { public Dao<Teacher> createTeacherDao() {
return new TeacherDao(DaoFactory.connection); return new TeacherDao(DaoFactory.connection);
} }
@Override @Override
public DisciplineDao createDisciplineDao() { public Dao<Discipline> createDisciplineDao() {
return new DisciplineDao(DaoFactory.connection); return new DisciplineDao(DaoFactory.connection);
} }
} }

View File

@ -31,7 +31,7 @@ public class DisciplineDao extends Dao<Discipline> {
} }
@Override @Override
public List<Discipline> findAll(Discipline obj) { public List<Discipline> findAll() {
return DISCIPLINES; return DISCIPLINES;
} }

View File

@ -32,7 +32,7 @@ public class StudentDao extends Dao<Student> {
} }
@Override @Override
public List<Student> findAll(Student obj) { public List<Student> findAll() {
return students; return students;
} }

View File

@ -31,7 +31,7 @@ public class TeacherDao extends Dao<Teacher> {
} }
@Override @Override
public List<Teacher> findAll(Teacher obj) { public List<Teacher> findAll() {
return teachers; return teachers;
} }

View File

@ -2,37 +2,45 @@ package fr.gasser.daoexample.model;
public class Student extends Entity { public class Student extends Entity {
private String nom; private String name;
private String prenom; private String surname;
public Student() { public Student() {
this(0, "", ""); this(0, "", "");
} }
public Student(int id, String nom, String prenom) { public Student(int id, String name, String surname) {
super(id); super(id);
this.nom = nom; this.name = name;
this.prenom = prenom; this.surname = surname;
} }
public String getName() { public String getName() {
return nom; return name;
} }
public void setNom(String nom) { public void setName(String name) {
this.nom = nom; this.name = name;
} }
public String getSurname() { public String getSurname() {
return prenom; return surname;
} }
public void setPrenom(String prenom) { public void setSurname(String surname) {
this.prenom = prenom; this.surname = surname;
} }
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", surname='" + surname + '\'' +
", id=" + id +
'}';
}
} }

View File

@ -1,9 +1,11 @@
package fr.gasser.daoexample.sql; package fr.gasser.daoexample.sql;
public interface Connection { import java.sql.SQLException;
void connect();
void close(); public interface Connection {
void connect() throws SQLException;
void close() throws SQLException;
java.sql.Connection getConnection(); java.sql.Connection getConnection();
} }