Observer Pattern Sample Walkthrough
___________________________________

in this sample we have a simple form SubjectForm that acts as a subject. and we have also two forms that
act as observers ObserverForm1 and ObserverForm2 the observers will be interested in observing the change of
the BackColor of the SubjectForm . 

in the SubjectForm we have a comboBox with the values Red,Green,Blue you should choose a color from the list
and click the Button ChangeColor to change the BackColor of the SubjectForm to the corresponding Color selected
in the comboBox.

*********************************************************************************************************************
Public Class SubjectForm                                                                                            *
    Inherits System.Windows.Forms.Form                                                                              *
														    *
#Region " Windows Form Designer generated code "								    *
.														    *
.														    *
.														    *			
#End Region													    *
														    *			
    Public o1 As New ObserverForm1										    *
    Public o2 As New ObserverForm2										    *
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click     *
        Dim curColor As String = ComboBox1.SelectedItem                                                             *
        Select Case curColor                                                                                        *
            Case "Red"                                                                                              *
                Me.BackColor = Color.Red									    *
            Case "Green"                                                                                            *
                Me.BackColor = Color.Green									    *
            Case "Blue"												    *
                Me.BackColor = Color.Blue									    *
        End Select												    *
    End Sub													    *		
														    *
    Private Sub SubjectForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load    *
        Me.o1.Location = New Point(Me.Location.X + Me.Width, Me.Location.Y + Me.Height)                             *
        Me.o1.Show()                                                                                                *
        Me.o2.Location = New Point(Me.Location.X - Me.Width, Me.Location.Y + Me.Height)                             *
        Me.o2.Show()                                                                                                *
    End Sub                                                                                                         *
End Class                           										    *			
*********************************************************************************************************************

to start coding the pattern let's first write the subject observer protocols. 

***********************************************
Public Interface Subject		      *
    Sub UpdateObservers()		      *
    Sub AddObserver(ByVal ob As Observer)     *
End Interface				      *
					      *
Public Interface Observer		      *
    Sub UpdateObserver(ByVal note As Object)  *
End Interface				      *		
***********************************************

obviously the SubjectForm must implement the Subject interface and both ObserverForm1 and ObserverForm2
must implement the Observer interface. 

in AOP we separate the Functionality of the Observer Pattern from the classes participating in this pattern,
we will encapsulate this functionality in an aspect named SubjectObserverAspect . 

Choose Project->Add New Item then choose Aspect from the items in the list
(you should see this item after you install the the AopDotNetAddIn),name this Aspect SubjectObserverAspect . 

a class SubjectObserverAspect derived from AspectLib.Aspect will be added to your project also
an xml file aspectDescriptor.xml will be added


Coding the Aspect
_________________

as we said this aspect will contain the functionality of both the observer and the subject. 

starting with the subject functionality add the following code to the SubjectObserverAspect aspect 

********************************************************************
#Region "Subject Functionality"					   *
		       						   *								
    Public Observers As New ArrayList				   *	
								   *
    Public Sub AddObserver(ByVal ob As Observer)		   *
        Observers.Add(ob)					   *
    End Sub							   *
								   *
    Public Sub UpdateObservers(ByVal subj As SubjectForm)	   *	
        Dim ob As Observer					   *
        For Each ob In Observers				   *
            ob.Update(subj.BackColor)				   *
        Next							   *
    End Sub							   *
    								   *	
    Public Sub initObservers(ByVal f As SubjectForm)		   *
        f.AddObserver(f.o1)					   *
        f.AddObserver(f.o2)					   *
    End Sub							   *
								   *
    Public Sub afterEvent(ByVal subj As Subject)		   *
        subj.UpdateObservers()					   *
    End Sub							   *
#End Region							   *
********************************************************************	

a subject should have a list of all its observers so we have the ArrayList Observers, the implementation of
the function AddObserver would add the supplied observer to the Observers Array.

the UpdateObservers function will loop over all the observers in the Observers array and call the Update
method of each one passing it the background color, note that we added an argument of type SubjectForm (subj)
to the Update function, this argument enables us to access the context information we need to know about
the Subject (like the background color).

the initObservers sub is where we register o1 and o2 as observers of the SubjectForm. 

the afterEvent sub is used to start updatig the observers.

and here is the implementation of the observer functionality. 
**************************************************************************
#Region "Observer Functionality"					 *
									 *		
    Public Sub UpdateObserver(ByVal obs As Form, ByVal note As Object)   *
        Dim c As Color = CType(note, Color)				 *
        obs.BackColor = c						 *
    End Sub								 *
									 *
#End Region								 *
**************************************************************************

this implementation will be applied to both ObserverForm1 and ObserverForm2 which derive from Form
so we added an argument of type Form (obs) 

Writing the Weave Instructions (aspectDescriptor.xml)
_____________________________________________________

the weaving the aspect code into the object code (the classes) is determined by the aspectDescriptor.xml file
 
*************************************************************************************************************
<?xml version="1.0" encoding="utf-8"?>									    *		
<aspects>												    *	
    <aspect name="SubjectObserverAspect" code="SubjectObserverAspect.vb" weave="true">			    *	
        <advices>											    *
            <advice type="after">									    *
                <method name="initObservers" />								    *	
                <pointcut>										    *	
                    <execution>										    *
                        <method name="SubjectForm_Load" class="SubjectForm" access="*" return="*"/>	    *
                    </execution>									    *
                </pointcut>										    *
            </advice>											    *
            <advice type="after">									    *
                <method name="afterEvent" />								    *
                <pointcut>									 	    *
                    <execution>										    *		
                        <method name="Button1_Click" class="SubjectForm" access="*" return="*" />	    *
                    </execution>									    *
                </pointcut>										    *	
            </advice>											    *		
        </advices>											    *
        <introductions>											    *			
            <member name="Observers" introduceto="SubjectForm" />					    *
            <interface name="Subject" introduceto="SubjectForm" />					    *
            <interface name="Observer" introduceto="Observer*" />					    *
        </introductions>										    *	
    </aspect>												    *	
</aspects>												    *	
*************************************************************************************************************

the instructions are defined for each aspect inside the <aspect> node.

you can introduce new member and/or methods to your classes by specifying them in the <introductions> node.
in our example we specified that:
- we will add the member Observers to the SubjectForm Class.
- the SubjectForm class will implement the Subject interface.
- both ObserverForm1 and ObserverForm2 (Observer*) will implement the Observer interface. 

the <advice> node represent pieces of an aspect implementation to be executed at a pointcut,
where the pointcut defines specific points in a program's execution

the first advice specifies that the initObserves method will be executed after the SubjectForm_Load method ( which has any
access modifier , any return type and is in the SubjectForm class) 

the second advice specifies that the afterEvent method will be executed after the Button1_Click method
( which has any access modifier , any return type and is in the SubjectForm class) 


