Add a dao pattern example using the abstract factory design pattern

This commit is contained in:
2019-01-02 22:15:59 +01:00
parent 07a463cbfe
commit f1f6c77870
16 changed files with 467 additions and 0 deletions

View File

@ -0,0 +1,25 @@
package fr.gasser.daoexample.dao;
import fr.gasser.daoexample.DummyConnection;
import fr.gasser.daoexample.model.Entity;
import java.util.List;
public abstract class Dao<T extends Entity> {
protected DummyConnection connection;
public Dao(DummyConnection connection) {
this.connection = connection;
}
public abstract boolean create(T obj);
public abstract T find(int id);
public abstract List<T> findAll(T obj);
public abstract boolean update(T obj);
public abstract boolean delete(T obj);
}

View File

@ -0,0 +1,25 @@
package fr.gasser.daoexample.dao;
public interface DaoAbstractFactory {
static DaoAbstractFactory createFactory(FactoryType type) {
switch (type) {
case XmlDaoFactory:
return new XmlDaoFactory();
default:
case DaoFactory:
return new DaoFactory();
}
}
static DaoAbstractFactory createFactory() {
return createFactory(FactoryType.DaoFactory);
}
StudentDao createStudentDao();
TeacherDao createTeacherDao();
DisciplineDao createDisciplineDao();
}

View File

@ -0,0 +1,22 @@
package fr.gasser.daoexample.dao;
import fr.gasser.daoexample.DummyConnection;
public class DaoFactory implements DaoAbstractFactory {
private static DummyConnection connection = new DummyConnection();
@Override
public StudentDao createStudentDao() {
return new StudentDao(DaoFactory.connection);
}
@Override
public TeacherDao createTeacherDao() {
return new TeacherDao(DaoFactory.connection);
}
@Override
public DisciplineDao createDisciplineDao() {
return new DisciplineDao(DaoFactory.connection);
}
}

View File

@ -0,0 +1,53 @@
package fr.gasser.daoexample.dao;
import fr.gasser.daoexample.DummyConnection;
import fr.gasser.daoexample.model.Discipline;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class DisciplineDao extends Dao<Discipline> {
// Emulate discipline table
private final static List<Discipline> DISCIPLINES = new ArrayList<>(Arrays.asList(
new Discipline(0, "Azerty"),
new Discipline(1, "Foo"),
new Discipline(2, "Qsdf")
));
public DisciplineDao(DummyConnection connection) {
super(connection);
}
@Override
public boolean create(Discipline obj) {
return false;
}
@Override
public Discipline find(int id) {
return DISCIPLINES.get(id);
}
@Override
public List<Discipline> findAll(Discipline obj) {
return DISCIPLINES;
}
@Override
public boolean update(Discipline obj) {
if (!containsId(obj.getId())) return false;
DISCIPLINES.set(obj.getId(), obj);
return true;
}
@Override
public boolean delete(Discipline obj) {
return DISCIPLINES.remove(obj);
}
private boolean containsId(int id) {
return DISCIPLINES.stream().anyMatch(d -> id == d.getId());
}
}

View File

@ -0,0 +1,6 @@
package fr.gasser.daoexample.dao;
public enum FactoryType {
DaoFactory,
XmlDaoFactory
}

View File

@ -0,0 +1,54 @@
package fr.gasser.daoexample.dao;
import fr.gasser.daoexample.DummyConnection;
import fr.gasser.daoexample.model.Student;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class StudentDao extends Dao<Student> {
// Emulate student table
private final static List<Student> students = new ArrayList<>(Arrays.asList(
new Student(0, "Azerty", "Uiop"),
new Student(1, "Foo", "Bar"),
new Student(2, "Qsdf", "Jklm"))
);
StudentDao(DummyConnection connection) {
super(connection);
}
@Override
public boolean create(Student obj) {
students.add(obj);
return true;
}
@Override
public Student find(int id) {
return students.get(id);
}
@Override
public List<Student> findAll(Student obj) {
return students;
}
@Override
public boolean update(Student obj) {
if (!containsId(obj.getId())) return false;
students.set(obj.getId(), obj);
return true;
}
@Override
public boolean delete(Student obj) {
return students.remove(obj);
}
private boolean containsId(int id) {
return students.stream().anyMatch(student -> id == student.getId());
}
}

View File

@ -0,0 +1,54 @@
package fr.gasser.daoexample.dao;
import fr.gasser.daoexample.DummyConnection;
import fr.gasser.daoexample.model.Teacher;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class TeacherDao extends Dao<Teacher> {
// Emulate teacher table
private final static List<Teacher> teachers = new ArrayList<>(Arrays.asList(
new Teacher(0, "Uiop", "Jklm"),
new Teacher(1, "Lorem", "Ipsum"),
new Teacher(2, "Sit", "Amet"))
);
TeacherDao(DummyConnection connection) {
super(connection);
}
@Override
public boolean create(Teacher obj) {
return teachers.add(obj);
}
@Override
public Teacher find(int id) {
return teachers.get(id);
}
@Override
public List<Teacher> findAll(Teacher obj) {
return teachers;
}
@Override
public boolean update(Teacher obj) {
if (!containsId(obj.getId())) return false;
teachers.set(obj.getId(), obj);
return true;
}
@Override
public boolean delete(Teacher obj) {
return teachers.remove(obj);
}
private boolean containsId(int id) {
return teachers.stream().anyMatch(t -> id == t.getId());
}
}

View File

@ -0,0 +1,18 @@
package fr.gasser.daoexample.dao;
public class XmlDaoFactory implements DaoAbstractFactory {
@Override
public StudentDao createStudentDao() {
return null;
}
@Override
public TeacherDao createTeacherDao() {
return null;
}
@Override
public DisciplineDao createDisciplineDao() {
return null;
}
}