4

Supposed I've Facade class that has two subsystem classes. Each subsystem has different event.

The FacadeClass is

public class FacadeClass
{
    private SubsystemClass1 _s1;
    private SubsystemClass2 _s2;

    public FacadeClass() 
    {
        _s1 = new SubsystemClass1();
        _s2 = new SubsystemClass2();
    }
}

and SubsystemClass1

public class SubsystemClass1
{
    public event EventHandler FetchComplete;

    public void OnFetchComplete(EventArgs e)
    {
        if (FetchComplete != null)
        {
            FetchComplete(this, e);
        }
    }
}

then SubsystemClass2

public class SubsystemClass2
{
    public event EventHandler SendComplete;

    public void OnSendComplete(EventArgs e)
    {
        if (SendComplete != null)
        {
            SendComplete(this, e);
        }
    }
}

Supposed I've another class that used facade class and I want to attach event from SubsystemClass1 and SubsystemClass2. The question is how to attach the event without redifine it in facade class and also without using subsystem class (if any)?

Example for redifine it in facade class

public class FacadeClass
{
    private SubsystemClass1 _s1;
    private SubsystemClass2 _s2;

    public FacadeClass() 
    {
        _s1 = new SubsystemClass1();
        _s2 = new SubsystemClass2();
    }

    // Redifine the event
    public event EventHandler FetchComplete
    {
        add { _s1.FetchComplete += value; }
        remove { _s1.FetchComplete -= value; }
    }

    public event EventHandler SendComplete
    {
        add { _s2.SendComplete += value; }
        remove { _s2.SendComplete -= value; }
    }
}

Example for using subsystem class, make them both public

public class FacadeClass
{
    // Make both class to public
    public SubsystemClass1 _s1;
    public SubsystemClass2 _s2;

    public FacadeClass() 
    {
        _s1 = new SubsystemClass1();
        _s2 = new SubsystemClass2();
    }
}

public class AnotherClass
{
    FacadeClass _fd;

    public AnotherClass() 
    {
        _fd = new FacadeClass();
        // Little bit ugly
        _fd._s1.FetchComplete += new EventHandler(_s1_FetchComplete);
    }

    void _s1_FetchComplete(object sender, EventArgs e)
    {
        // Do Something Here
    }
}

Thanks in advance,

regards Brian ...

Hensembryan
  • 1,067
  • 3
  • 14
  • 31

1 Answers1

1

Your solution to wrap the event seems pretty good to me. What is the issue with this? In some way you are going to have to either specify the SubSystemClass or add some sort of wrapper on FacadeClass. Because the SubSystemClasses are private it is not possible to get to the events unless you add some code to FacadeClass, which you have done. The only other option I can think of is reflection which might be a workable solution.

Edit: I don't know if this is any different to the case of wanting to expose a property of s1 or s2. If you wanted to do that you would either need to make s1/s2 public, provide a wrapper property or use reflection.

MikeKulls
  • 2,979
  • 2
  • 25
  • 30
  • I do that for information hiding. And if there is more than one class that use subsystem class, I must redifine it and again. – Hensembryan Aug 03 '12 at 02:30