C++ – Decouple GUI & Program logic

cwindows

I have some windows c++ code (pure winapi c++, no MFC etc) that I would like to build a GUI for. My code does not know about windows GUI, it is completely decoupled from any type of UI.

The question: How could I build a UI around the existing code. Ideally, I am imagining something like this:

GUI <–> Mediator <–> MyExistingCode

For example,

MyExistingCode.showmsg(char* msg) would call Mediator.MessageBox(0, 0, msg, 0);

I'm stuck at how to design the Mediator class (If that is the correct way of going about this). Is the Mediator an Interface?, Abstract Class?, Other? Is an MVC model the best way to go?

Best Answer

The solution that works best for me nowadays is to build my application like a library (lib or dll) and then build my console or GUI application on top of that. On Visual Studio, for example, this works by having (at least) two projects in the same solution. One project is your library and the other is the consumer of that library. You only have to provide a sensible interface, and not care who and how it uses your library. In this way, my console or GUI would use my code as any other regular 3rd party library.

See some opinions on this SO question: What do you think of developing for the command line first?