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.

No comments:

Post a Comment