I am making a chat application. My problem with updating lies here :
I have the Core class that stores an instance of Component.
When a new message is received the Core will call a method of Component and it will update the screen.
When a new message has to be send(typed in Component pane) how the Component will notify the Core ?
It doesn’t make sense to me ( I might be wrong) that the Component will have an instance of Core in order to send updates to it ( same thing would happen with Observer pattern).
It might be a straightforward solution but I can’t figure it out right now.. Any suggestions are welcome. Its something like a two-way observer pattern that I need.
Basically, an observer pattern is always nice to use if you have a data source and a receiver and you want to ensure a loose coupling between them.
If your Core class stores directly an Instance of your component, you shouldnt need an extra observer pattern between them, since the Core class can directly call methods on your component, right?
So, you’ll need any connection / indirect association between your Core and your Component for sending messages, and there you should use Observer. You can pass yourself as an Observer to your Component when you create an instance of it.
But actually, I think its a major design problem in your architecture, you should use MVC for such applications:
MVC
In that case, you’ll have to split up Core in a Controller (handling UI interactions, i.e. sending / receiving messages) and a Model (storing messages and meta data, maybe application logic). Your Component would be the View class.