mardi 24 mai 2022

What is the best way to let a child class communicate with the parent class

my question is, what is the best way to let a child class communicate with the parent class. For example: I have a main class simply called Main, and another class SomeClass. Now the Main class creates an instance of SomeClass, once the state of the SomeClass-object changes, the Main class should execute different code, depending on what changed. Yeah I know, that already sounds like the Observer design pattern, but how would I implement it with state changes treated differently? I'm currently writing an Android app with a database to make it more specific. In my project I have the main class, a class to connect, read from/write to the database and a GUI container class. (oversimplified, there are a few more) The main class creates an instance of both the GUI and database class. Now if I press a button A, it should write A-data to the database, if I press button B, it should write B-data to the database. As I think that a gui class shouldn't have direct access to the database, I tried other options, than just accessing the database from the gui-class Currently, I defined a placeholder abstract class with only one method, that I am just overwriting with the functionality. So right now I have to create a one-method-class A for the click of button A and a one-method-class B for the click of button B. It doesn't sound like the best way to me, I mean It's working, but I'd like to improve my code, so if you have any idea, please write your solution. :)

Aucun commentaire:

Enregistrer un commentaire