From f1f6c778700944e5b91790ae98ec78ff0a34b684 Mon Sep 17 00:00:00 2001 From: Thibaud Date: Wed, 2 Jan 2019 22:15:59 +0100 Subject: [PATCH] Add a dao pattern example using the abstract factory design pattern --- persistence/pom.xml | 17 ++++++ .../fr/gasser/daoexample/DummyConnection.java | 5 ++ .../java/fr/gasser/daoexample/FirstTest.java | 34 ++++++++++++ .../java/fr/gasser/daoexample/dao/Dao.java | 25 +++++++++ .../daoexample/dao/DaoAbstractFactory.java | 25 +++++++++ .../fr/gasser/daoexample/dao/DaoFactory.java | 22 ++++++++ .../gasser/daoexample/dao/DisciplineDao.java | 53 ++++++++++++++++++ .../fr/gasser/daoexample/dao/FactoryType.java | 6 +++ .../fr/gasser/daoexample/dao/StudentDao.java | 54 +++++++++++++++++++ .../fr/gasser/daoexample/dao/TeacherDao.java | 54 +++++++++++++++++++ .../gasser/daoexample/dao/XmlDaoFactory.java | 18 +++++++ .../gasser/daoexample/model/Discipline.java | 46 ++++++++++++++++ .../fr/gasser/daoexample/model/Entity.java | 18 +++++++ .../fr/gasser/daoexample/model/Student.java | 38 +++++++++++++ .../fr/gasser/daoexample/model/Teacher.java | 51 ++++++++++++++++++ pom.xml | 1 + 16 files changed, 467 insertions(+) create mode 100644 persistence/pom.xml create mode 100644 persistence/src/main/java/fr/gasser/daoexample/DummyConnection.java create mode 100644 persistence/src/main/java/fr/gasser/daoexample/FirstTest.java create mode 100644 persistence/src/main/java/fr/gasser/daoexample/dao/Dao.java create mode 100644 persistence/src/main/java/fr/gasser/daoexample/dao/DaoAbstractFactory.java create mode 100644 persistence/src/main/java/fr/gasser/daoexample/dao/DaoFactory.java create mode 100644 persistence/src/main/java/fr/gasser/daoexample/dao/DisciplineDao.java create mode 100644 persistence/src/main/java/fr/gasser/daoexample/dao/FactoryType.java create mode 100644 persistence/src/main/java/fr/gasser/daoexample/dao/StudentDao.java create mode 100644 persistence/src/main/java/fr/gasser/daoexample/dao/TeacherDao.java create mode 100644 persistence/src/main/java/fr/gasser/daoexample/dao/XmlDaoFactory.java create mode 100644 persistence/src/main/java/fr/gasser/daoexample/model/Discipline.java create mode 100644 persistence/src/main/java/fr/gasser/daoexample/model/Entity.java create mode 100644 persistence/src/main/java/fr/gasser/daoexample/model/Student.java create mode 100644 persistence/src/main/java/fr/gasser/daoexample/model/Teacher.java diff --git a/persistence/pom.xml b/persistence/pom.xml new file mode 100644 index 0000000..3e6933e --- /dev/null +++ b/persistence/pom.xml @@ -0,0 +1,17 @@ + + + 4.0.0 + + + fr.gasser + java-cookbook + 1.0-SNAPSHOT + + + patterns + 1.0-SNAPSHOT + + + \ No newline at end of file diff --git a/persistence/src/main/java/fr/gasser/daoexample/DummyConnection.java b/persistence/src/main/java/fr/gasser/daoexample/DummyConnection.java new file mode 100644 index 0000000..3f72644 --- /dev/null +++ b/persistence/src/main/java/fr/gasser/daoexample/DummyConnection.java @@ -0,0 +1,5 @@ +package fr.gasser.daoexample; + +public class DummyConnection { + +} diff --git a/persistence/src/main/java/fr/gasser/daoexample/FirstTest.java b/persistence/src/main/java/fr/gasser/daoexample/FirstTest.java new file mode 100644 index 0000000..4c2a57c --- /dev/null +++ b/persistence/src/main/java/fr/gasser/daoexample/FirstTest.java @@ -0,0 +1,34 @@ +package fr.gasser.daoexample; + +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; + +public class FirstTest { + public static void main(String[] args) { + DaoAbstractFactory df = DaoAbstractFactory.createFactory(); + Dao studentDao = df.createStudentDao(); + for (int i = 0; i < 3; i++) { + Student student = studentDao.find(i); + System.out.println("Student #" + student.getId() + " - " + student.getName() + " " + student.getSurname()); + } + + System.out.println("\n********************************\n"); + Dao teacherDao = df.createTeacherDao(); + for (int i = 0; i < 3; i++) { + Teacher teacher = teacherDao.find(i); + System.out.println("Teacher #" + teacher.getId() + " - " + teacher.getName() + " " + teacher.getSurname()); + } + + System.out.println("\n********************************\n"); + Dao disciplineDao = df.createDisciplineDao(); + for (int i = 0; i < 3; i++) { + Discipline discipline = disciplineDao.find(i); + System.out.println("Discipline #" + discipline.getId() + " - " + discipline.getName()); + } + + } + +} diff --git a/persistence/src/main/java/fr/gasser/daoexample/dao/Dao.java b/persistence/src/main/java/fr/gasser/daoexample/dao/Dao.java new file mode 100644 index 0000000..5e262f5 --- /dev/null +++ b/persistence/src/main/java/fr/gasser/daoexample/dao/Dao.java @@ -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 { + + 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 findAll(T obj); + + public abstract boolean update(T obj); + + public abstract boolean delete(T obj); +} diff --git a/persistence/src/main/java/fr/gasser/daoexample/dao/DaoAbstractFactory.java b/persistence/src/main/java/fr/gasser/daoexample/dao/DaoAbstractFactory.java new file mode 100644 index 0000000..ed8759e --- /dev/null +++ b/persistence/src/main/java/fr/gasser/daoexample/dao/DaoAbstractFactory.java @@ -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(); + +} diff --git a/persistence/src/main/java/fr/gasser/daoexample/dao/DaoFactory.java b/persistence/src/main/java/fr/gasser/daoexample/dao/DaoFactory.java new file mode 100644 index 0000000..c258b5d --- /dev/null +++ b/persistence/src/main/java/fr/gasser/daoexample/dao/DaoFactory.java @@ -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); + } +} diff --git a/persistence/src/main/java/fr/gasser/daoexample/dao/DisciplineDao.java b/persistence/src/main/java/fr/gasser/daoexample/dao/DisciplineDao.java new file mode 100644 index 0000000..fc52a23 --- /dev/null +++ b/persistence/src/main/java/fr/gasser/daoexample/dao/DisciplineDao.java @@ -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 { + + // Emulate discipline table + private final static List 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 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()); + } +} diff --git a/persistence/src/main/java/fr/gasser/daoexample/dao/FactoryType.java b/persistence/src/main/java/fr/gasser/daoexample/dao/FactoryType.java new file mode 100644 index 0000000..cc27066 --- /dev/null +++ b/persistence/src/main/java/fr/gasser/daoexample/dao/FactoryType.java @@ -0,0 +1,6 @@ +package fr.gasser.daoexample.dao; + +public enum FactoryType { + DaoFactory, + XmlDaoFactory +} diff --git a/persistence/src/main/java/fr/gasser/daoexample/dao/StudentDao.java b/persistence/src/main/java/fr/gasser/daoexample/dao/StudentDao.java new file mode 100644 index 0000000..3df5427 --- /dev/null +++ b/persistence/src/main/java/fr/gasser/daoexample/dao/StudentDao.java @@ -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 { + + // Emulate student table + private final static List 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 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()); + } +} diff --git a/persistence/src/main/java/fr/gasser/daoexample/dao/TeacherDao.java b/persistence/src/main/java/fr/gasser/daoexample/dao/TeacherDao.java new file mode 100644 index 0000000..c10b644 --- /dev/null +++ b/persistence/src/main/java/fr/gasser/daoexample/dao/TeacherDao.java @@ -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 { + + // Emulate teacher table + private final static List 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 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()); + } + +} diff --git a/persistence/src/main/java/fr/gasser/daoexample/dao/XmlDaoFactory.java b/persistence/src/main/java/fr/gasser/daoexample/dao/XmlDaoFactory.java new file mode 100644 index 0000000..247d6a7 --- /dev/null +++ b/persistence/src/main/java/fr/gasser/daoexample/dao/XmlDaoFactory.java @@ -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; + } +} diff --git a/persistence/src/main/java/fr/gasser/daoexample/model/Discipline.java b/persistence/src/main/java/fr/gasser/daoexample/model/Discipline.java new file mode 100644 index 0000000..07eb89f --- /dev/null +++ b/persistence/src/main/java/fr/gasser/daoexample/model/Discipline.java @@ -0,0 +1,46 @@ +package fr.gasser.daoexample.model; + +import java.util.HashSet; +import java.util.Set; + +public class Discipline extends Entity { + + private String name; + private Set teachers; + private Set students; + + public Discipline(int id, String name) { + super(id); + this.name = name; + this.teachers = new HashSet<>(); + this.students = new HashSet<>(); + } + + public Discipline(int id) { + super(id); + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public boolean addTeacher(Teacher teacher) { + return teachers.add(teacher); + } + + public boolean removeTeacher(Object o) { + return teachers.remove(o); + } + + public boolean addStudent(Student student) { + return students.add(student); + } + + public boolean removeStudent(Object o) { + return students.remove(o); + } +} diff --git a/persistence/src/main/java/fr/gasser/daoexample/model/Entity.java b/persistence/src/main/java/fr/gasser/daoexample/model/Entity.java new file mode 100644 index 0000000..0a18a26 --- /dev/null +++ b/persistence/src/main/java/fr/gasser/daoexample/model/Entity.java @@ -0,0 +1,18 @@ +package fr.gasser.daoexample.model; + +public class Entity { + + protected int id; + + public Entity(int id) { + this.id = id; + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } +} diff --git a/persistence/src/main/java/fr/gasser/daoexample/model/Student.java b/persistence/src/main/java/fr/gasser/daoexample/model/Student.java new file mode 100644 index 0000000..3d2472e --- /dev/null +++ b/persistence/src/main/java/fr/gasser/daoexample/model/Student.java @@ -0,0 +1,38 @@ +package fr.gasser.daoexample.model; + +public class Student extends Entity { + + private String nom; + private String prenom; + + public Student() { + this(0, "", ""); + } + + public Student(int id, String nom, String prenom) { + super(id); + this.nom = nom; + this.prenom = prenom; + } + + + public String getName() { + return nom; + } + + + public void setNom(String nom) { + this.nom = nom; + } + + + public String getSurname() { + return prenom; + } + + + public void setPrenom(String prenom) { + this.prenom = prenom; + } + +} \ No newline at end of file diff --git a/persistence/src/main/java/fr/gasser/daoexample/model/Teacher.java b/persistence/src/main/java/fr/gasser/daoexample/model/Teacher.java new file mode 100644 index 0000000..04f7f9f --- /dev/null +++ b/persistence/src/main/java/fr/gasser/daoexample/model/Teacher.java @@ -0,0 +1,51 @@ +package fr.gasser.daoexample.model; + +import java.util.HashSet; +import java.util.Set; + +public class Teacher extends Entity { + + private String name; + private String surname; + private Set disciplines; + + public Teacher(int id, String name, String surname) { + super(id); + this.name = name; + this.surname = surname; + this.disciplines = new HashSet<>(); + } + + public Teacher(int id) { + super(id); + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getSurname() { + return surname; + } + + public void setSurname(String surname) { + this.surname = surname; + } + + public Set getDisciplines() { + return disciplines; + } + + public boolean addDiscipline(Discipline discipline) { + return disciplines.add(discipline); + } + + public boolean removeDiscipline(Object o) { + return disciplines.remove(o); + } + +} diff --git a/pom.xml b/pom.xml index 80892d1..888381b 100644 --- a/pom.xml +++ b/pom.xml @@ -11,6 +11,7 @@ gui async lang + persistence