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...
-
3Sounds reasonable to me.Robert Harvey– Robert Harvey04/02/2019 21:56:10Commented Apr 2, 2019 at 21:56
-
2Possible duplicate of Why should I prefer composition over inheritance?Apalala– Apalala04/02/2019 22:37:49Commented Apr 2, 2019 at 22:37
-
1Can you tell more about the HumanVirtualMouse class, please? I am not clear what is "in a human-like way" in your question.Hieu Le– Hieu Le04/03/2019 14:48:17Commented 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.Joao Vitor– Joao Vitor04/03/2019 15:02:55Commented Apr 3, 2019 at 15:02
-
1Do you want both classes to be interchangable? Use one or another indistinctly?Laiv– Laiv04/03/2019 20:44:29Commented Apr 3, 2019 at 20:44
1 Answer 1
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.
-
Agreed, it fails the "is a" test.Robert Harvey– Robert Harvey04/03/2019 20:50:00Commented Apr 3, 2019 at 20:50
Explore related questions
See similar questions with these tags.