mercredi 20 septembre 2023

Design Pattern for handling the many components of a locomotive

In C#, I'm creating a locomotive management system. My issue is that I have various components, such as a bogie carrying Axles, Axles holding multiple Wheels and Breaks, and all of these parts degrade on a timer. I'd like a system in which the wheels can update the basic components holding them, such as the bogie (chassis), and I'd like the components (Axle, Wheel) to be more independent rather than being stored into lists. Is there a design pattern that can meet these requirements? My code is included below.

public abstract class LocomotiveComponent 
{
    public string Name;
    public string Description;
    public List<LocomotiveComponent> Components;
    public float Health;

    protected LocomotiveComponent(string name) 
    {
        Name = name;
        Health = 100f;
    }

    public abstract void Update();

}

public abstract class Bogie : LocomotiveComponent
{
    public Bogie(string name) : base(name)
    { }
}

public abstract class Axle : LocomotiveComponent
{
    public Axle(string name) : base(name)
    {}
}

public class Axle4Wheeled : Axle
{
    public Axle4Wheeled(string name) : base(name)
    {
        Components = new(4);
    }

    public override void Update()
    {
    }
}

public class Axle6Wheeled : Axle
{
    public Axle6Wheeled(string name) : base(name)
    {   
        Components = new (6); 
    }

    public override void Update()
    {
    }
}

public class Wheel : LocomotiveComponent
{
    public Wheel(string name) : base(name) 
    {}

    public override void Update()
    {
        Health--;
        if(Health < 10f)
        {
            //update bogie of low health
        }
    }
}

I tried the composite pattern for various components holding others.

Aucun commentaire:

Enregistrer un commentaire