tags:

views:

212

answers:

3

I wish to present the idea of MVC to a bunch of old C++ spaghetti coders (at my local computer club).

One of them that has alot of influence on the rest of the group seems to finally be getting the idea of encapsulation (largely due in part to this website).

I was hoping that I could also point him in the right direction by showing him Model View Controller, but I need to do it in a way that makes sense to him, as well as it probably needs to be written in C/C++!

I realize that MVC is a very old architectural pattern so it would seem to me that there should be something out there that would do the job.

I'm more of a web developer, so I was wondering if anybody out there who is a good C/C++ coder could tell me what it is that made the MVC light switch turn on in your head.

+1  A: 

Get some spaghetti C++ code (theirs?), refactor it to use MVC, and show them what advantages it has, like easier unit testing, re-use of models, making localized changes to the view with less worry, etc.

Ken
This is a good idea. However, they don't have any form of unit testing in their code. I was trying to learn the Unit testing part of Boost...not done with that yet though...
leeand00
+5  A: 

Don't start off with MVC. Start off with Publish / Subscribe (AKA the "listener" pattern).

Without the listener pattern fully understood, the advantages of MVC will never be understood. Everyone can understand the need to update something else when something changes, but few think about how to do it in a maintainable manner.

Present one option after another, showing each option's weaknesses and strengths: making the variable a global, merging the other portion of code into the variable holder, modifying the holder to directly inform the others, and eventually creating a standard means of registering the intent to listen.

Then show how the full blown listener can really shine. Write a small "model" class and add half a dozen "listeners" and show how you never had to compromise the structure of the original class to add "remote" updates.

Once you get this down, move the idea into to the "model view" paradigm. Throw two or three different views on the same model, and have everyone amazed on how comparatively easy it is to add different views of the same information.

Finally discuss the need to manage views and update data. Note that the input is partially dependent on items which are not in the view or the model (like the keyboard and mouse). Introduce the idea of centralizing the processing where a "controller" needs to coordinate which models to create and maintain in memory, and which views to present to the user.

Once you do that, you'll have a pretty good introduction to MVC.

Edwin Buck
You know what, your right; I remember a time when I suggested that they use the Publish / Subscribe method and they were using a polling method instead (it involved checking a directory to see if it had changed). You can have your program sit there and loop though or wait between loops to check if something has changed in the directory, or you can have a method subscribe to be notified when something changes in the folder.The guy I was explaining this to didn't understand it at the time, I just hope these guys understand the pattern.
leeand00
I even showed him an example in the language that they use to program all the time: http://www.codeproject.com/KB/files/directorychangewatcher.aspxThis demonstrates the Publisher Subscriber pretty well I think.
leeand00
+3  A: 

You might find it easier to sell them on the Document/View or Document/Presenter patterns. MVC was invented on Smalltalk where everything about the different UI elements had to be coded by the developer (as I understand, never used the thing). Thus the controller element was necessary because didn't have things like TextElement::OnChange. Now days, more modern GUI API's use Document/View but Document/Presenter is something I've seen proposed.

You might also consider reading Robert Martin's article on the TaskMaster framework.

You might also consider that any C++ developer who is not familiar with these patterns and already understands their purpose and necessity is either a complete newb or a basket-case best avoided. People like that cause more harm than good and are generally too arrogant to learn anything new or they already would have.

Noah Roberts
Thanks Noah! This is definitely something that I haven't tried.
leeand00
It also appears to contain an Observer Pattern (same thing as Publisher/Subscriber above that Edwin mentioned).
leeand00
@Noah Is the full source of the examples contained in the article, or can it be downloaded from somewhere? (http://www.oma.com/C++Report/TaskMaster/Examples) as mentioned in the article, doesn't appear to be a valid link anymore.
leeand00
I never looked for the examples so I don't know.
Noah Roberts
Umm, don't confuse arrogance with ignorance. Many people happily chug along in ignorance to "better ways of doing things" all the time. That doesn't make them arrogant for doing so.
Chris Kaminski