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 List<T> findAll(T obj);
public abstract List<T> findAll();
public abstract boolean update(T obj);

View File

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

View File

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

View File

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

View File

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

View File

@ -2,37 +2,45 @@ package fr.gasser.daoexample.model;
public class Student extends Entity {
private String nom;
private String prenom;
private String name;
private String surname;
public Student() {
this(0, "", "");
}
public Student(int id, String nom, String prenom) {
public Student(int id, String name, String surname) {
super(id);
this.nom = nom;
this.prenom = prenom;
this.name = name;
this.surname = surname;
}
public String getName() {
return nom;
return name;
}
public void setNom(String nom) {
this.nom = nom;
public void setName(String name) {
this.name = name;
}
public String getSurname() {
return prenom;
return surname;
}
public void setPrenom(String prenom) {
this.prenom = prenom;
public void setSurname(String surname) {
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;
public interface Connection {
void connect();
import java.sql.SQLException;
void close();
public interface Connection {
void connect() throws SQLException;
void close() throws SQLException;
java.sql.Connection getConnection();
}