views:

55

answers:

4

For example, in an IDE application, say for C#, there are 2 views ClassView and TextView.

In ClassView we need to display the Class information in a hierarchical manner, where as in TextView the code for that Class is shown.

The ClassView needs to query for classes, methods, properties, fields, etc. whereas the Text View queries lines in the code.

Both the views are editable. Change in one view should be reflected in the other view too.

So Class View requires one model and Text View requires another but the underlying data is the same.

Is there a design pattern to solve such a problem?

Thanks in advance.

A: 

Model View Controller :)

The mistake in your question is that your data is actually mapped on your model.

You can have 2 views (classview and textview) and they both works with one single common model. Controller can update one view when another one changes the model.

Roman
A: 

You tagged it yourself with MVC... The underlying data is the model, the Class View and Text View act as views/controllers. The model sends out events to its views, to make sure that changes in one view are reflected in the other.

Thomas
A: 

There's nothing in MVC architecture to prevent writing multiple model hierarchies which interact with the same underlying data store.

You would just have Controllers/Views which interact with Model A, and different Controllers/Views which interact with Model B.

David Montgomery
Thanks for the quick response. So when model A has changed (via View A), who tells view B to update itself. I mean is the following correct.View A tells Ctrl'er A tells Model A to change some data. Model A tells Common model to change data. Common model tells all registered observers (in this case Model B) to update the view. Model B informs the Ctrl'er B informs View B to update.
Ajay
@Ajay: Maybe I'm missing something in your question, but a Model is nothing more than a logical representation of your underlying business data as it currently exists, usually in a database somewhere. If the underlying data has changed, then the it follows the model class instances representing that data will also be changed. The details of how to implement this would depend on the nature of your data store, whether Model A knows anything about Model B, is this happening in one process or many processes, which language, platform, etc.
David Montgomery
The data is a simple file. All models are in the same process. Language: C#, Platform:.NET.What I wanted to know is in a scenario where there are multiple models for the same data, whether the models can tell the other models about the change in data. Does the MVC pattern allow this or is there any other pattern which does a better job.Also can the MVC pattern be hierarchical. i.e. One model being a model to another model. E.g.Data->Model A->Model 1->View 1Data->Model A->Model 2->View 2
Ajay
A: 

Certainly an MVC model can be hierarchical. If your data is all contained in a single file, maybe you don't really need multiple models for your application? How about:

    namespace MyNamespace
    {
    public class CodeFile
    {
        /// <summary>
        /// A list of contained classes for the Class view
        /// </summary>
        public List<CodeClass> Classes { get; set; }
        public CodeFile()
        {
            Classes = new List<CodeClass>();
        }
        public string ToString()
        {
            // send output to Text view
        }
    }

    public class CodeClass
    {
        public string ClassName {get; set;}
        public List<CodeProperty> Properties {get; set;}
        public List<CodeMethod> Methods {get;set;}
        public CodeClass(string className)
        {
            ClassName = className;
            Properties = new List<CodeProperty>();
            Methods = new List<CodeMethod>();
        }
    }

    public class CodeMethod
    {
        public string MethodName { get; set; }
    }

    public class CodeProperty
    {
        public string PropertyName
    }


}
David Montgomery