Saturday, October 8, 2011

What is a state machine? How state machines are designed?

The link below has answers to these questions with examples. This is the best you can find in internet.

http://accu.org/index.php/journals/252

Tuesday, June 16, 2009

What flexibility is, Why it is needed and How it is achieved?

Recently, I have read some interesting articles about flexibility. Below are some links to those articles.

OO in One Sentence - Keep It DRY, Shy, and Tell the Other Guy
This article explains the importance of flexibility and some simple ways to achieve it.

A Conversation with Erich Gamma
This article is about flexibility and reuse.

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.






Tuesday, February 17, 2009

UI Widget Development with MVC (Model-View-Controller) pattern

MVC is one of the most widely used design pattern in widget architectures.
Before MVC (also called as "3 objects" pattern), user interface widgets were designed either using "1 object" or "2 objects" pattern.
The diagram below explains the types of objects involved in these patterns.

Disadvantages of "1 Object" and "2 Objects" pattern:

  • Source code becomes flooded with algorithms, data structures etc.,

  • Tough to understand and maintain.

  • Makes the code less flexible to changes and difficult to reuse because of tight coupling.

All the above issues can be solved by using "3 objects" pattern (MVC).

Model-View-Controller (MVC)

  • It is an architectural pattern. Mainly used in GUI architectures.

  • Helps to decouple presentation, functionality and input handling.

It consists of three types of objects.
  1. Model: Functionality or data
  2. View: Screen presentation of model
  3. Controller: Defines how view reacts to user input


Most Common challenges faced while designing these objects:
This section explains the most common challenges faced while designing View, Controller and Model objects.
Let's take an example of simple menu widget.

VIEW:

Supporting dynamic properties is one of the main challenges faced while designing View.
For Example,


Creating different View objects for the above, makes the menu more complex. It also bloats the code and makes it hard to change.
This design problem can be solved by using Decorator pattern.

Decorator design pattern:
  • Create scrollbar, title, border, etc., as decorators and design the View in such a way that decorators can be added or removed at runtime.

  • This design pattern allows new behavior to be added to an existing class menu view class dynamically. In other words, using this functionality, menu can be extended at runtime.

CONTROLLER:

Supporting Multiple Algorithms is one of the main challenges faced while designing Controller.
Let's take, for example, user pressed '2' key



Different algorithms are needed to support these features and it has to be designed in such a way that View can easily select an algorithm at runtime.
This relationship between controller and view can be represented by using strategy design pattern.

Strategy design pattern:
  • Encapsulates each algorithm, and makes them selectable at runtime.

  • Using this pattern, Algorithms can be replaced either statically or dynamically.

  • Decouples an algorithm from its host.

MODEL:

Model notifies View, when Controller changes the Data. This has to be achieved without coupling Model and View.

For Example,


These menus need different functionality, so coupling Model and View directly makes it less flexible to changes and difficult to reuse.
This design problem can be solved by using Observer design pattern.

Observer design pattern:
  • Represents relationship between Model and View.

  • It helps to decouple Model from the View.

  • Notifies associated Views when the Model changes.


For more information about MVC pattern.