mardi 3 décembre 2019

Network Manager field design pattern

I have a system which now needs to send and receive commands to the server. I have created sort of service locator class called NetworkManager, which looks as follows

public class NetworkManager : MonoBehaviour
{
    private static NetworkManager _instance;
    public static NetScheduling Scheduling => _instance._netScheduling;
    public static NetCleaning   Cleaning   => _instance._netCleaning;

    private NetScheduling _netScheduling;
    private NetCleaning _netCleaning;

    public NetworkManager()
    {
        _netScheduling = new NetScheduling();
        _netCleaning = new NetCleaning();
    }
}

Referenced classes looks similar to this:

private class NetCleaning
{
    private CleaningClient _client;

    public void StartCleaning()
    {
        var request = new StartRobotKitchenCleaningRequest();
        client.StartRobotKitchenCleaning(request);
    }
]

public class NetScheduling
{
    private ScheduklingClient _client;

    public void GetScheduledTasks()
    {        
        var request = new GetScheduledTasksRequest();
        client.GetScheduledTasks(request);
    }

    public void ScheduleTask(string id)
    {        
        var request = new ScheduleTaskRequest(id);
        client.ScheduleTask(request);
    }
}

NetworkManager will containt about 10 classes like NetCleaning and NetScheduling, all containing methods with differenct names. Btw these are created with gRPC so I am not able to modify those classes.

Problems is that I would need to access methods of these classes from another classes, given any wrapper I can not elliminate the case of checks before performing any request call to those classes - what if server is not reachable at the moment? So I wish I could do the following

public class SomeClass
{
    private void Method()
    {
        ...
        NetworkManager.NetCleaning.StartCleaning();
    }
}

instead of this

public class SomeClass
{
    private void Method()
    {
    ...
    if(NetworkManager.IsServerAvailable)
        NetworkManager.NetCleaning.StartCleaning();
    }
}

So I am looking towards kind of design approach here which will avoid checking of server availability on each request. I don't know but maybe proxy or observer would be useful here, but I can't wrap my head around their implementation here.

Aucun commentaire:

Enregistrer un commentaire