I am writing a client server application. I wanted to fully separate the server logic, from the view. The first thing I wanted to to, is to make a sort of a message log.
The server itself should not know if the messages will be shown at a GUI, or on the console.
What I was thinking, would be to have a handler method, that would be called every time a new message was posted. So a GUI app would have it’s own method to maybe add to a listView, while the console would have a simple printf.
Is there a better way to do this?
You’re on the right track and more or less describing the Adapter Design Pattern, which is defined by wikipedia as:
Adapter design pattern is used when you want two different classes
with incompatible interfaces to work together. Interfaces may be
incompatible but the inner functionality should suit the need. The
Adapter pattern allows otherwise incompatible classes to work together
by converting the interface of one class into an interface expected by
the clients.
In your case, you would probably have a GUI adapter and a Console adapter, both which would consume handle the server messages as appropriate for their user interface and you would instantiate at run-time whichever adapter is most appropriate.
2