How to implement the Observer pattern in practice with database?

4

I'm studying some design patterns, and right now I'm learning about the observer pattern . I've read books, I've seen some lessons on Youtube, I've done the examples and everything.

But now I'd like to implement in practice inserting data into the database, to use in a project I'm currently working on.

My situation: I have a base with 3 tables ( Setor , Pessoa , PessoaSetor ). It's a "many-to-many" relationship. Every sector can have more than one person, this information is in the PersonSetor table.

What I need: Every time a person registers in a sector, he or she sends a notification to those responsible for the sector. In this case, the notification can be a separate table.

    
asked by anonymous 10.03.2016 / 21:26

1 answer

3

The question does not give too many details, it's too generic, so I can not give you such a precise answer, but basically the observer pattern is applied to the database using the triggers feature. >

Every major relational database has the command CREATE TRIGGER where you define an action to be executed when any changes to the database occur. Exactly what determines this pattern. One of these actions is to call some routine, an executable and in some database it is possible to only send a notification to the connected client (s). It depends on the bank to know if it will be simpler and more appropriate to do something like this. See the specific documentation for your database.

It may seem like a simple and obvious solution to use. But using triggers is full of tricks and even experienced developers can fall into traps. I'm passing the technique, but not saying it's the best way to handle it. As always, everything depends on the case. Like any design pattern, it can be abused.

    
11.03.2016 / 12:53