Click here to Skip to main content
15,888,527 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I've read about Model-View-Controller pattern, and I plan to use it for my WTL-based project. For example, I would like to write a calculator application.

First of all, I write CCalculator class that performs actual calculations. This is the model in terms of MVC.

Then I create a dialog and write CCalculatorDlg class (derived from WTL's CDialogImpl<> template class) to handle dialog events. What is this in terms of MVC? View or view-controller? If this is just a view, than should I write another class, say CCalculatorController, as an intermediary between CCalculatorDlg and CCalculator? How to apply MVC pattern to WTL correctly?
Posted
Updated 15-Jul-11 17:47pm
v2

1 solution

The answer to the questions "The question is how to apply MVC pattern correctly? What is the view and what is controller?" is a part of MVC. Keep reading. It need some experience in analysis and architecture.

The Model is the calculation model represented in data: operands and result of calculation. I think it should not contain any strings representing numeric data, should be numeric-only. In a more or less advanced calculator if should be something like expression tree.

The View should be the window with controls.

The events of the window should be implemented in Controller. The idea is: the events should not be translated in the UI-related processing. All events should be consumed by Controller to update the state of the model. Then the whole updated model is used by Controller to map it on the states of the View.

Now, what is the functionality of the calculator? I would say in this is a calculator with buttons (1, 2, 3, +, -, etc.) it would be a waste of time. Who needs it? Just to mimic standard historical calculator. The real thing should work with text input.

I would understand that you need it just for exercise. Otherwise, making a Windows application for a calculator would be a waste of time. Why? Because a practical calculator is something like mine:

http://www.sakryukov.org/freeware/calculator/[^].

—SA
 
Share this answer
 
Comments
OlegKrivtsov 15-Jul-11 6:07am    
Thanks for your explanation, but it still unclear for me.

"The events of the window should be implemented in Controller"
Should I write another class, say CCalculatorController? But if I write it, this is waste of time, because it is easier to handle events in CCalculatorDlg class with built-in message map.
Sergey Alexandrovich Kryukov 16-Jul-11 0:25am    
Wrong. It's not a waste of time if you do it right. Also, Controller is not a class -- this is a concept. Got the idea?
Is this the reason you voted 2, I wonder?
--SA
OlegKrivtsov 16-Jul-11 2:26am    
I voted 2, because you didn't answer my question. If this is not a waste of time, than how to do this correctly? If you have ever worked with WTL, you should know that all window messages are handled inside of CCalculatorDlg class, not in another class. To handle messages in CCalculatorController class I should do a lot of work to redirect messages to that class, and I must know what's the advantage of doing this? I don't see for now..
Sergey Alexandrovich Kryukov 18-Jul-11 23:49pm    
You should have ask the question properly instead of down-voting and demanding without understanding, especially with your apparent lack of judgement and experience. You're on your own, no more help from me.
Good luck.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900