0

So i have a class called VirtualMouse, it is used to perform mouse actions such as moving and clicking.

public class VirtualMouse
{
    public VirtualMouse()
    {

    }

    public void MoveByOffset(int offsetX, int offsetY)
    {
       // Implementation.
    }

    public void MoveToPosition(Point position)
    {
        // Implementation.
    }

    public void ClickLeftButton()
    {
       // Implementation.
    }

    public void PressLeftButton()
    {
        // Implementation.
    }

    public void ReleaseLeftButton()
    {
        // Implementation.
    }
}

This is to implement basic mouse automation. Now I also wanna have the ability to create a class that is specialized, for example a HumanVirtualMouse which would have the same methods but would move the mouse in a human like way.

Should I inherit from VirtualMouse and call the base class methods in succession to create human patterns(for example I can use MoveByOffset inside a loop) or have an instance of VirtualMouse inside the HumanMouse class?

I guess i should favor inheritance because HumanMouse is-a VirtualMouse and i only need to modify the movement methods, click, press and release remain the same...

Joao Vitor
  • 145
  • 6
  • 3
    Sounds reasonable to me. – Robert Harvey Apr 02 '19 at 21:56
  • 2
    Possible duplicate of [Why should I prefer composition over inheritance?](https://softwareengineering.stackexchange.com/questions/134097/why-should-i-prefer-composition-over-inheritance) – Apalala Apr 02 '19 at 22:37
  • 1
    Can you tell more about the HumanVirtualMouse class, please? I am not clear what is "in a human-like way" in your question. – Hieu Le Apr 03 '19 at 14:48
  • It uses a series of points calculated by another method, these points describe a curve which will make the mouse move in a "Human" fashion, not in an instant robotic way. – Joao Vitor Apr 03 '19 at 15:02
  • 1
    Do you want both classes to be interchangable? Use one or another indistinctly? – Laiv Apr 03 '19 at 20:44

1 Answers1

2

From your description and the comment, it does not sound like you want a derived class HumanVirtualMouse. It seems you want a component which generates certain calls to MoveToPosition (or the other methods) to a virtual mouse object, something like a HumanLikeMouseController. Overriding those "Move" methods does not look necessary, not even very useful to me.

Thus I see no benefit in using inheritance here. In fact, my mental model is one of a mouse on one hand, and someone or something separate controlling it - and the code should reflect that model.

Doc Brown
  • 199,015
  • 33
  • 367
  • 565