[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 652
  • Last Modified:

Design Pattern, observer and observable, transulation from Java to C#

I was experimenting design pattern using Head First Design Pattern which is primary for java.
So far I managed to adopt the code into C# until on page 67

import java.util.Observable
import java.util.Oberver

I was looking for library based on above for the C# and found nothing relevent. It not clear if this actually exist for C#, I thing it don't.

Is there alternative method or solution that work for C#.

Thanks
0
riscy
Asked:
riscy
  • 3
1 Solution
 
PoeticAudioCommented:
You can use delegates / events in C#, that's really close to how the observer pattern works.

The object that raises the event is like the subject, and all of the objects that are tied into the event handler are like observers.

Do you need a code example?
0
 
PoeticAudioCommented:
Here is a cute little example, just so you can see the similarities

//----------------------------- This is the code for the "subject" -------------------------------
public class Subject
{
    public delegate void OnCountedToTenEventHandler();
    public event OnCountedToTenEventHandler OnCountedToTen;

    public Subject()
    {
    }

    public void CountToTen()
    {
        for(int i = 1; i <= 10; i++)
        {
            if(i == 10)
            {
               //raise the event when we reached 10.
                OnCountedToTen();
            }
        }
    }
}
//----------------------------- End code for the "subject" -------------------------------




public class Observer1
{
    //Pass in a subject, and tie an event handler in. We could have made the delegate in the subject static... that's just not as fun =)
    public Observer1(Subject subject)
    {
        subject.OnCountedToTen +=new WindowsApplication1.Subject.OnCountedToTenEventHandler(subject_OnCountedToTen);
    }

    //here is what we want to happen when the subject hits 10 on the CountToTen method (ie: this is the event handler)
    private void subject_OnCountedToTen()
    {
        System.Windows.Forms.MessageBox.Show("I'm Observer1 and I got the subjects message!");
    }
}


//Here is another observer. We could have got technical and made an observer base class that these 2 observers derrived from,
//but I don't want to stray too far away from the point of events

public class Observer2
{
    public Observer2(Subject subject)
    {
        subject.OnCountedToTen +=new WindowsApplication1.Subject.OnCountedToTenEventHandler(subject_OnCountedToTen);
    }

    private void subject_OnCountedToTen()
    {
        System.Windows.Forms.MessageBox.Show("Check it, I'm observer2 and I got the message!");
    }
}



//Now lets get the ball rolling in a button click or something
Subject subject = new Subject();
Observer1 observer1 = new Observer1(subject);  //pass in the subject
Observer2 observer2 = new Observer2(subject);  //pass in the same subject to observer2

subject.CountToTen(); //here we go!!
0
 
PoeticAudioCommented:
Just so you know, you can pass any object back to the event handlers, for example, lets say you wanted to alter it a bit...


public class Subject
{
    public delegate void OnCountedEventHandler(int NumCountedTo);
    public event OnCountedEventHandler OnCounted;

    public Subject()
    {
    }

    public void CountToTen()
    {
        for(int i = 1; i <= 10; i++)
        {
            OnCounted(i);  //pass the number that the subject has counted to thus far.
        }
    }
}

There is other crap too, like when you see events in windows applications you often see an object called sender and another object that is a System.EventArgs. You can create your own EventArgs class and pass back info through that as well. That is beyond the scope of my point though.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now