jeudi 9 mars 2017

Design pattern name for class that defines the flow between interfaces

I have two independent interfaces that are unaware of each others' existence, then one class to define the flow between them.

public interface IListener
{
    event EventHandler StartSignalReceived;
    void StartListening();
    void ReportProgress(string message);
    void ReportError(Exception exception);
    void ReportComplete();
}

public interface IDevice
{
    Task PerformAsync(IProgress<string> progress);
}

public class MyApplicationFlow
{
    private readonly IListener _listener;
    private readonly IDevice _device;

    public MyApplicationFlow(IListener listener, IDevice device)
    {
        _listener = listener;
        _device = device;
    }

    public void Start()
    {
        _listener.StartSignalReceived += ListenerOnSignalReceived;
        _listener.StartListening();
    }

    private async void ListenerOnSignalReceived(object sender, EventArgs args)
    {
        try
        {
            await _device.PerformAsync(new Progress<string>(DeviceOnProgressChange));

            _listener.ReportComplete();
        }
        catch (Exception ex)
        {
            _listener.ReportError(ex);
        }
    }

    private void DeviceOnProgressChange(string message)
    {
        _listener.ReportProgress(message);
    }
}

There could be several different IDevice and IListener implementations passed into the flow.

Please excuse the poor diagram!

The listeners and devices seem to follow the Adaptor pattern. But what, if anything, does MyApplicationFlow follow?

Is this a known pattern? Does it have a name? Is there another pattern that solves the same problem (i.e. decoupling the two interfaces)?

Aucun commentaire:

Enregistrer un commentaire