mardi 14 juillet 2020

Placeholder of class that can be filled out later?

Suppose I have a collection which I initialize with a buffer parameter that represents the number of elements that the collection should contain at all times. Upon construction, I would like to fill it with placeholders until 'real' values or instances are added. When a real value is added, I want the oldest placeholder value to update its values to match that of the 'added' value instead of physically adding new data to the collection.

To do this, I thought I could write an interface IPlaceholder that roughly does something like this:

public interface IPlaceholder<T>
{
    public bool Placeholder { get; set; }
    public void UpdateValues(/* type specific constructor parameters? */);
    // public void UpdateValues(object[] params) is a last resort
}

For example, I could use a buffer for data which I receive from a sensor to keep track of how the sensor data changed over the last 10 minutes:

public class SensorData : IPlaceholder<SensorData>
{
    public decimal? Sensor1 { get; set; } = null;
    public decimal? Sensor2 { get; set; } = null;
    public DateTime ReceiveTime { get; set; }
    public bool Placeholder { get; set; }

    public SensorData()
    {
        Placeholder = true;
        ReceiveTime = DateTime.Now;
    }
    public SensorData(DateTime t)
    {
        Placeholder = true;
        ReceiveTime = t;
    }
    public SensorData(decimal s1, decimal s2, DateTime t)
    {
        Sensor1 = s1;
        Sensor2 = s2;
        ReceiveTime = t;
        Placeholder = false;
    }
    
    // Use any of the possible constructor signatures as class?
    public void UpdateValues(decimal s1, decimal s2, DateTime t)
    {
        Sensor1 = s1;
        Sensor2 = s2;
        ReceiveTime = t;
        Placeholder = false;
    }
}

Or I could have a collection of competitors in a race of who I know they must finish, therefore the buffer represents the order in which the racers have finished.

public class Racer : IPlaceholder<Racer>
{
    public string Name { get; set; } = null;
    public string RacerCar { get; set; } = null;
    public bool Placeholder { get; set; }

    public Racer()
    {
        Placeholder = true;
    }
    public Racer(string name, string racerCar)
    {
        Name = name;
        RacerCar = racerCar;
        PlaceHolder = false;
    }

    // Use any of the possible constructor signatures as class?
    public void UpdateValues(string name, string racerCar)
    {
        Name = name;
        RacerCar = racerCar;
    }
}

The idea is that I can initialize the collection given a number of placeholders that get filled in once the actual data starts flowing in:

var racerCount = 6;
var racers = new CustomCollection<Racer>(racerCount); // Initializes 6 placeholder Racer objects

// Actual racers:
var john = new Racer("John Lennon", "Lucy");
var paul = new Racer("Paul McCartney", "Michelle");
var ringo = new Racer("Ringo Starr", "Octopus");
var george = new Racer("George Harrison", "Blue Jay");
var brian = new Racer("Brian Epstein", "Seltaeb");
var yoko = new Racer("Yoko Ono", "Samantha");

racers.Add(john);
/* John Finishes first 
=> calls racers.Where(x => x.Placeholder).First()
.UpdateValues(john.Name, john.RacerCar)
or something
*/
etc.

For the sensor example, I would probably also add the functionality to add new data to it, in which case the oldest data gets removed and all the existing data gets shifted back for the latest sensor data.

Is there a way to implement an interface to accommodate the type-specific construction parameters to update a general placeholder?





Aucun commentaire:

Enregistrer un commentaire