Question

I am looking for an appropriate design pattern for the following:

I have the following system structure:

MainApplication
    SubSystem1
    SubSystem2
    SubSystem3

Where the MainApplication initializes each subsystem,

    SubSystem1 s1;
    SubSystem2 s2;
    SubSystem3 s3;

    public MainApplication()
    {
        s1 = new SubSystem1();
        s2 = new SubSystem2();
        s3 = new SubSystem3();
    }

and each subsystem should be able to communicate with one another.

Within each subsystem how can I call a method from another subsystem? For example in s1

    public SubSystem1()
    {
        s2.Method1();
        s3.Method2();
    }

Would a Facade Design Pattern work here? If so, how would it be implemented? If not which design pattern should be used for this scenario?

Was it helpful?

Solution

This pretty much depends on the kind of communication between the subsystems.

If it is abstract, i.e. the subsystems do not actually have to know about each other, a publish-subscribe based messaging mechanism may be appropriate. See https://en.wikipedia.org/wiki/Publish/subscribe for an introduction, but i think the concept should be fairly straight-forward.

If, on the other hand, the subsystems really have to know each other in a concrete way, why are they subsystems in the first place? Making this kind of partitioning indicates that there is indeed a separation of concerns, so finding an abstract interface should not be that hard. If it is, maybe you should reconsider your subsystem responsibilities.

OTHER TIPS

I never could remember design pattern names. Why can't you let each subsystem know the other subsystems?

s1.SetSubsystem2(s2);
s1.SetSubsystem3(s3); 
...

If you want to be more future-change-resilient, describe each subsystem's interface in an interface, and make sure SetSubsystemX takes that interface, and not the concrete class.

EDIT: An example of an interface.

Let's say your first subsystem knows how to send an email, and the second subsystem knows how to print a file. You should declare two interfaces:

interface IEmailSubsystem
{
    void SendEmail(string content);
}

interface IPrintSubsystem
{
    void PrintFile(string path);
}

Then you can define your two Subsystem objects:

class Subsystem1: IEmailSubsystem ...
class Subsystem2: IPrintSubsystem ...

If you ever going to end up needing more than the 3 subsystems, you should have a global subsystem registry, but don't worry about it just yet.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top