I'm developing a MFC application using MVC pattern. I use the MFC Dialog class as controller and a model class for the model, a view class that holds the model class as a member for the view. The data in the model is organized in a tree structure, something like that shown in http://ift.tt/1VFgtWk.
In the diagram, the capital letter stands for a class name, so the data in the model is a tree of objects, each of which is extended from the same base object and contains many data member, including the reference to the sub-object, like pointer of sub-object or array of sub-objects
But the problem is that when the view display the data in the model on the UI, it need to access the data members of every node object on every level, which requires including headfiles of those node objects so as to retrieve their data members. So the method of Drawing UI in the cpp file looks like this:
#include "B.h"
#include "C.h"
#include "D.h"
#include "E.h"
#include "F.h"
#include "G.h"
void View::Draw(CDC *pDC)
{
B b1=A.b1;
B b2=A.b2;
for (int i=0; i<A.sizeofB;i++)
{
C c=A.arrayofB[i];
int length=c.getlength();
int j=c.sizeofD;
while (j>0)
{
D d=c.arrayofD[j];
CString Name=d.Name;
...
j--;
}
....
}
for (int i=0; i<A.sizeofB2;i++)
{
C c=A.arrayofB2[i];
double start=c.start;
double end=c.end;
int j=c.sizeofD;
while (j>0)
{
D d=c.arrayofD[j];
CString Name=d.Name;
...
j--;
}
....
}
}
This looks very messy and it's obvious that there is a tight coupling between the model and the view. How can I make them highly independent to each other so that to ensure the maintainability or scalability of my application.
Aucun commentaire:
Enregistrer un commentaire