dimanche 23 juin 2019

Design Pattern to use when you want apply some common functionality to some specific methods?

I am trying to figure out a design pattern to use(if any exists) to a situation where I would be re-doing some functionality across a bunch of classes. Below is a (simplified) overview of the problem I am facing:

I have some Java code to CREATE, UPDATE, DELETE Student objects, Professor object, & Staff objects. And every time such object is either created, deleted, or updated, I want to extract some information about the affected object(such as name, age, id) and notify an external service. So something like:

class StudentDAO {
   public Student createStudent(Student studentToCreate) {
       jdbcTemplate.update(INSERT_SQL, .....);
       //===> extract some info of the student
       //let external service know a student was created....
   }
   public Student deleteStudent(Student studentToDelete) {
       jdbcTemplate.update(DELETE_SQL, .....);
       //===> extract some info of the student
       //let external service know a student was deleted....
   }
   //same thing for update
}

class ProfessortDAO {
   public Professor createProfessor(Professor professorToCreate) {
       jdbcTemplate.update(INSERT_SQL, .....);
       //===> extract some info of the professor
       //let external service know a Professor was created....
   }
   public Student deleteProfessor(Professor professorToDelete) {
       jdbcTemplate.update(DELETE_SQL, .....);
       //===> extract some info of the professor
       //let external service know a professor was deleted....
   }
   //same thing for update
}

//repeat for Staff

The example is bit contrived but assume that Student, Professor, Staff share no inheritance hierarchy. Is there a way to achieve this functionality without copying and pasting the logic for extracting the info and sending it in all the DAO classes for CREATE, DELETE, UPDATE methods ?

Aucun commentaire:

Enregistrer un commentaire