0

I want to create an application that maintains a list of Computer objects, checks those objects for availability (using Ping) and if they're available use PSExec to add or remove users to/from the list of administrators.

Currently I can not figure out how to build my classes - which class should be responsible for what (I want to use the common patterns, such as SRP, as good as possible without "overkilling" it.).

When looking at the availability-check, should there be an "extra" class doing the ping request or should this be done by the computer object (instance) itself? If an extra class should be used, how should the computer (and other) object(s) be notified about the changed availability? With a Property?

Tho thoughts regarding this kind of stuff drives me crazy and prevents me from getting any further... I know there is no correct answer as this is obviously a design and opinion question but I'd appreciate it if I could get an experts opinion here - hopefully this brings me back on track.

Th1sD0t
  • 1,089
  • 3
  • 11
  • 37

1 Answers1

1

Hi I have come up with the following according to the description provided. The Computer class is adhering to SRP as it is only concerned with Computer objects. All the operations are delegated to specialized classes. Currently I have added only a class to check availability. We can also add a specialized class for adding removing users.

Open for discussion and refinement.

public class Computer
{
    // properties of the computer class
    public IList<User> Users;

    // IAvailabiity checker
    private readonly IAvailabilityChecker _checker;

    // constructor
    public Computer(IAvailabilityChecker checker)
    {
        this._checker = checker;
    }

    // operations
    public void AddUser()
    {
        if (this._checker.IsAvailable())
        {
            // add user
        }
    }

    public void RemoveUser()
    {
    }
}

public class User
{
}

public interface IAvailabilityChecker
{
    bool IsAvailable();
}

public class AvailabilityChecker
{
    public bool IsAvailable()
    {
        // availability checker logic
        return true;
    }
}
Priyan Perera
  • 560
  • 1
  • 7
  • 20
  • This is a good pattern because it allows you to mock the ping operation. If you didn't do this, you wouldn't be able to unit test all of the logic paths in `Computer` without actually bringing one of your computers down. – John Wu Mar 10 '18 at 01:54