vendredi 25 décembre 2020

How to make objects interact each other in this example with the proper way?

I have an awkward question, a question from an amateur at designing reliable and flexible softwares. Let's say that i have an interface to specify Movable behaviour, named by IMovable as:

interface IMovable
{
    void MoveToDirection(string direction);
}

And a concrete class that movable two directions and implements the interface IMovable:

class TwoDirectionMovable : IMovable
{
    IMovement Forward;
    IMovement Backward;
    public void MoveToDirection(string direction)
    {
        if (direction == "Forward")
            Forward.PerformMovement();
        else if (direction == "Backward")
            Backward.PerformMovement();
        else
            throw new Exception("Can't move that direction!");
    }

    public TwoDirectionMovable(IMovement forward, IMovement backward)
    {
        this.Forward = forward;
        this.Backward = backward;
    }
}

As you can see, movements are specified by IMovement objects. IMovement interface and its abstract class, Movement Base, are like this:

interface IMovement
{
    bool Reached { get; set; }
    void PerformMovement();
}

abstract class MovementBase : IMovement
{
    public bool Reached { get; set; }

    public void PerformMovement()
    {
        if (!Reached)
        {
            PerformMovementImp();
        }
    }

    protected abstract void PerformMovementImp();
}

Assume there is a FastSlowMovement sub class:

class FastSlowMovement : MovementBase
{
    IMovement FastMovement;
    IMovement SlowMovement;
    public bool SlowDown { get; set; }

    protected override void PerformMovementImp()
    {
        if (SlowDown)
            SlowMovement.PerformMovement();
        else
            FastMovement.PerformMovement();
    }

    public FastSlowMovement(IMovement fastMovement, IMovement slowMovement)
    {
        this.FastMovement = fastMovement;
        this.SlowMovement = slowMovement;
    }
}

Somewhere in program, the SlowDown property becomes True while performing fast movement and object performs slow movement. Going back to TwoDirectionMovable, lets say that i instantiate object like this:

IMovable movable = new TwoDirectionMovable(new FastSlowMovement(movement1, movement2), new FastSlowMovement(movement3, movement4));

In this scenario, my movable object can go forward or backward and both movement is performed fast and slow. But the thing is, while moving forward direction, backward direction's SlowDown property must be false and same goes for moving backward direction, it sets forward direction's SlowDown property as false. And in future, TwoDirectionMovable may have another type of movement that doesn't have SlowDown property or may have one with SlowDown and one without it.

I thought that i can fire an event when one of those objects perform movement, subscribers can get their job done. But i couldn't figure where to do so. And i don't want to create objects for each possibility, for example: a composite object or a state machine with one FastSlowMovement and one with another type of movement etc.

Should i make objects subscribe events at the creational side of software? I mean, i will get that objects from a builder or factory. But i'm not sure if i have the knowledge to do so in the proper way.

I know it is a long, amateur-ish and an awkward question. Maybe the whole implementation is wrong, maybe i have to re-design with the proper way. I don't know, i am not sure. But i would be very appreciated if you'd answer.

Thanks to everyone, have a nice day/night.

Aucun commentaire:

Enregistrer un commentaire