vendredi 16 avril 2021

Encapsulation of modules

I have a concrete idea of a structure but I can't identify any pattern to this. So I guess that I do some thing that I should avoid and do in a different way.

My application will control multiple devices (all of the same type) which have multiple communication interfaces and multiple sensors (it's a simplified example to demonstrate the concept!).

Now, below you can find the example code. Now let's only focus on the "Device" class. This is some kind of a man in the middle that doesn't provide any own functionality but only implements other classes.

This sounds for me like a "Facade". But the difference is that a facade implemenets other classes as private instances and provide functions, where in my example instead I declare the implemented instances as public to let the user access them directly.

Achievement: The (in my real case high) number of services provided by the "Device" get splitted into specific topics (here e.g. "CommunicationServices" and "MeasurementServices"). This should help the user to gain a better orientation over the code.

So, is there an pattern (which I simply can't identify) that represents this implementation below? Or would that still be called a "Facade"?

class Application()
{
    List<IDevice> _listOfDevices = new List<IDevice>;

    readonly Device.Factory _deviceFactory;

    Application(Device.Factory df)
    {
        _deviceFactory = df;
    }

    void DoSomething()
    {
        // e.g. instantiate 2 devices
        _listOfDevices.Add(_deviceFactory);
        _listOfDevices.Add(_deviceFactory);
        
        foreach(IDevice device in _listOfDevices)
        {
            int temperature = device.MeasurementServices.TemperatureSensor.ReadTemperature();
            device.CommunicationServices.Wifi.SendMessage(temperature);
        
            //...  and so on
        }
    }
}

public class Device : IDevice
{
    public delegate Device Factory();

    public ICommunicationServices CommunicationServices { get; }
    public IMeasurementServices MeasurementServices { get; }
    
    public Device (ICommunicationServices comServices, IMeasurementServices measurementServices)
    {
        CommunicationServices = comServices;
        MeasurementServices = measurementServices;
    }   
}

public class CommunicationServices : ICommunicationServices
{
    IBluetooth Bluetooth { get; }
    IWifi Wifi { get; }
    ISerial Serial { get; }
    // ... more interfaces

    public CommunicationServices(IBluetooth bt, IWifi wf, ISerial sr)
    {
        Bluetooth = bt;
        Wifi = wf;
        Serial = sr;
    }
}

public class MeasurementServices : IMeasurementServices
{
    ITemperatureSensor TemperatureSensor { get; }
    IHumiditySensor HumiditySensor { get; }
    // ... more sensors
    
    public MeasurementServices (ITemperatureSensor ts, IHumiditySensor hs)
    {
        TemperatureSensor = ts;
        HumiditySensor = hs;
    }
}

Aucun commentaire:

Enregistrer un commentaire