How to correctly implement class interaction?

How to correctly implement class interaction?

There is a pure class for working with the database, you need to make a class for working with some data (all sorts of checks, etc.) that you need to get from the database. So how is it correct from the point of view of architecture to describe a new class? As a descendant of a class for working with the base, this is somehow strange. Can pass a pointer to an instance of the database class to the constructor?


Answer 1, authority 100%

If “in terms of architecture” , then look at the “Decorator” design pattern. There is also AOP, with frequent “working with some data (all sorts of checks, etc.)” it may make sense to cut it down.
In general, they usually don’t mess around with data from the database: they get it and show it;). It is assumed that all data processing took place before the data was entered into the database and there (in the database) they (data) are in the form that the application is immediately ready to “give” to the client. Therefore, look carefully at the architecture of your application (maybe somewhere you are wrong ;)).
And a class that works with the database is really better to design as a singleton, and as the load increases, be ready for pooling or load distribution.


Answer 2

In such cases, they make it from a class with a database “singletone“, and the processing of some data is done in another class, in which they use access to the database through “singleton“.