Friday, March 27, 2009

How Model-View-Controller objects are created and composed?

Any widget that supports multiple look and multiple feel has to deal with multiple Model, View and Controller objects. To maintain flexibility it is important to make these objects not depend on how other objects are created.

Creational design patterns solve these issues. These patterns reduce design complexity and increases flexibility. Here's the sentence from a famous design patterns book about creational design patterns:

"Creational design patterns give a lot of flexibility in What gets created, Who creates it, How it gets created, and when"

Let's see how and where MVC uses these patterns.
Specifying default controller class for a view is one place where MVC uses these patterns .It uses a design pattern called Factory Method to achieve this.

Factory Method Pattern:

This pattern encapsulates Controller creation and moves this knowledge out of View framework classes. View framework let's View subclasses to decide which controller class to instantiate because it only knows when to create a controller, not what kind of controller to create.

The main purpose of the Factory method is to create objects.

  • Controller Base: Controller's abstract class

  • Controller 1 and Controller 2: Controller's Concrete class

  • View Base: View's abstract class

  • View 1 and View 2: View's Concrete class

  • Controller Creator: This class has a factory method that creates Controller 1 or Controller 2 and returns a pointer to Controller Base. View Base uses this class and lets the derived class View 1 and View 2 to decide which Controller to instantiate.


Creational pattern, for more information about other Creational design patterns.

Friday, March 13, 2009

Nested Views support in MVC


Nested view is a view that is based on other views. In other words, it is a view which contains multiple views (or) widgets.

For example,


Nested views are used to create complex user interfaces. There are so many mechanisms to handle nested views, but the most common one is "Treat individual views and nested views in the same way" .This reduces design complexity and increases flexibility. This mechanism can be achieved by using Composite design pattern.


Composite design pattern:


  • Composite allows a group of objects to be treated in the same way as a single instance of an object.

  • Composite lets clients treat individual objects and compositions uniformly.


Both Views and Nested Views are derived from the same View base class. Nested View classes will have some extra functionality to support multiple Views or widgets.

The VIEW framework that handles and maintains Views is programmed using View base class interfaces, so no changes in logic are needed to support different types of Views.

Let's take, For example, how Draw () Interface of Views looks like.





Composite design pattern – Wikipedia, for more information about composite pattern.