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...

asked Apr 2, 2019 at 21:49
5
  • 3
    Sounds reasonable to me. Commented Apr 2, 2019 at 21:56
  • 2
    Possible duplicate of Why should I prefer composition over inheritance? Commented Apr 2, 2019 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. Commented Apr 3, 2019 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. Commented Apr 3, 2019 at 15:02
  • 1
    Do you want both classes to be interchangable? Use one or another indistinctly? Commented Apr 3, 2019 at 20:44

1 Answer 1

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.

answered Apr 3, 2019 at 20:42
1
  • Agreed, it fails the "is a" test. Commented Apr 3, 2019 at 20:50

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.