Saturday, October 24, 2009

Observer Pattern in MFC

As I walk through the different design concepts of the Design Pattern book by GoF, I usually try to find from my past experience about any practical implementation of these patterns. This way I found out one practical implementation of the Observer Pattern in microsoft foundation class (MFC) library which I would like to share with you.

Before explaining the application of Observer Pattern in MFC, let me give you a brief introduction of this pattern. Observer pattern helps you in notifying and updating all the dependent objects, if one object changes state. This pattern has two main objects – Subject and Observer. Multiple number of observers can be attached to a particular Subject. All the observers are notified and in turn gotten updated if the Subject changes its state. This is also known as the publish-subscribe. The subject is the publisher of changes, and the Observers are the subscribers to those changes.

The class diagram will look like the following.

And the sequence diagram of this pattern is like this.

What these two diagrams essentially depict is that in the Observer Pattern, we have a Subject, which can attach one or more Observers through its Attach() function. Whenever it changes its state it Notifies all the attached Observers through its Notify function. The observers in turn synchronize their states with that of the Subject through the GetSubjectState function.

Now let me dissect the MFC's Document-View architecture and explain how the Observer Pattern has been implemented there. In MFC, the Document acts as the Subject and the views attached to it act as the observers. The Document (Subject) has to notify and update the views (observers) whenever it changes its state. This is done through the function UpdateAllViews () of the CDocument class.

Let me show you the actual code of the UpdateAllViews from doccore.cpp under the MFC folder.

CDocument :: UpdateAllViews(CView* pSender, LPARAM lHint, CObject* pHint)
ASSERT(pSender == NULL || !m_viewList.IsEmpty());

POSITION pos = GetFirstViewPosition();
while (pos != NULL)
CView* pView = GetNextView(pos);
if (pView != pSender)
pView->OnUpdate(pSender, lHint, pHint);

As you can see from the code that UpdateAllViews function actually traverses through all the views attached through it and call the view's OnUpdate function. Hence UpdateAllViews acts as the Notify function of the Observer pattern.

Let me show you the actual code of OnUpdate from viewcore.cpp. It goes like this:

CView :: OnUpdate(CView* pSender,LPARAM, CObject*)
ASSERT(pSender != this);

As its is clear from the above code, that the OnUpdate function will actually invalidate the view area, which will force the views to redraw themselves synchronizing their states with the current state of the Document.

This is all about the notification from Document to View. But what about the Attach and Detach function of the Subject. We have similar functions in the Document class which are as follows:

void CDocument :: AddView(CView* pView)
ASSERT(pView->m_pDocument == NULL);
ASSERT(m_viewList.Find(pView, NULL) == NULL);
ASSERT(pView->m_pDocument == NULL);
pView->m_pDocument = this;


void CDocument :: RemoveView(CView* pView)
ASSERT(pView->m_pDocument == this);

pView->m_pDocument = NULL;


As this is clear from the above listings that CDocument :: AddView is similar to the Attach function of the Subject, and RemoveView is like the Detach function as in the Observer Pattern.

Similar to the Observer Pattern, the Document has a list of its attached views through its m_viewList member and each view has a reference to its document through its m_pDocument member.

From the above discussion, it has become clear that the MFC's Document- View architecture is one of the applications of the Observer Pattern.

Saturday, October 10, 2009

WiFi Subsystem in Android

As i was trying to understand the WiFi subsystem of Android i came out with a diagram to depict the flow of events between different functional blocks of it. I would like to share it with you.

It goes like this:

Hope it will help the Android developers' community.

Thursday, October 8, 2009

Tabs in Android - through an example

As i was trying to experiment with the different widgets of Android, i developed this example to show how Tab works in Android.

And the application looks like the following:

And the next tab looks like:

The first thing we need to to do is working on the XML layout.

The XML code of the layout my example is as follows:

(?xml version="1.0" encoding="utf-8"?)
(LinearLayout xmlns:android=""

(TabHost android:id="@+id/TabHost01" android:layout_width="wrap_content" android:layout_height="wrap_content")
(TabWidget android:id="@android:id/tabs" android:layout_width="wrap_content" android:layout_height="wrap_content" /)
(FrameLayout android:id="@android:id/tabcontent" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingTop="65px")
(AnalogClock android:id="@+id/AnalogClock01" android:layout_width="wrap_content" android:layout_height="wrap_content")(/AnalogClock)
(DigitalClock android:text="DigitalClock01" android:id="@+id/DigitalClock01" android:layout_width="wrap_content" android:layout_height="wrap_content")(/DigitalClock)

And the Java code for this example is as follws:

import android.os.Bundle;
import android.widget.TabHost;

public class tabexample extends Activity {
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {

TabHost tabs = (TabHost)findViewById(;


TabHost.TabSpec spec1 = tabs.newTabSpec("tag1");

spec1.setIndicator("Analog Clock");


TabHost.TabSpec spec2 = tabs.newTabSpec("tag2");
spec2.setIndicator("Digital Clock");


Hope this helps the newcomers of Android...