Add a dao pattern example using the abstract factory design pattern

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

17
persistence/pom.xml Normal file
View File

@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>fr.gasser</groupId>
<artifactId>java-cookbook</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>patterns</artifactId>
<version>1.0-SNAPSHOT</version>
</project>

View File

@ -0,0 +1,5 @@
package fr.gasser.daoexample;
public class DummyConnection {
}

View File

@ -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<Student> 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<Teacher> 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<Discipline> disciplineDao = df.createDisciplineDao();
for (int i = 0; i < 3; i++) {
Discipline discipline = disciplineDao.find(i);
System.out.println("Discipline #" + discipline.getId() + " - " + discipline.getName());
}
}
}

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;
}
}

View File

@ -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<Teacher> teachers;
private Set<Student> 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);
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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<Discipline> 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<Discipline> getDisciplines() {
return disciplines;
}
public boolean addDiscipline(Discipline discipline) {
return disciplines.add(discipline);
}
public boolean removeDiscipline(Object o) {
return disciplines.remove(o);
}
}

View File

@ -11,6 +11,7 @@
<module>gui</module>
<module>async</module>
<module>lang</module>
<module>persistence</module>
</modules>
<properties>