Which is better - to call empty methods or to use many interfaces

I have several classes that have one base class called Tool. In the form, I have one link to a tool containing one of the lines of the specified classes. When the MouseDown event appears on the form, I call the current method of the ex method. "CurrentTool.MethodWhenMouseDown ()".

Most tools have 3 methods:

MethodWhenMouseDown() MethodWhenMouseUp() MethodWhenMouseMove() 

But one or two classes only have:

 MethodWhenMouseDown() 

Now, which is better:

1. To use all three methods in the Tool, and classes that they do not need, simply call empty methods.

2. To implement ex interfaces. IMouseMoveListener, which will be implemented only by classes that should act when the MouseMove event occurs. Thus, if the MouseMove event occurs, we ask:

 if(CurrentTool is MouseMoveListener) { (CurrentTool as IMouseMoveListener).MethodWhenMouseMove(); } 

Additional Information:
The program is similar to Ms Paint - the tools are Brush, Bucket (one that MethodWhenMouseMove doesn't need), LineTool, etc.
In my PaintForm, I have one reference to the base class abstrac Tool, which stores one of the derived class. What triggers the event is pictureBox.

Have you considered the events that the tools subscribe to? - CodesInChaos

I would say that it would be good practice to have a method in the form that will be called after the appearance of evet and the method calls the siutable CurrentTool method. ex:

 void MouseMoveSubscriber(object sender, MouseEventArgs e) { CurrentTool.MethodWhenMouseMove(e); } 

I assume subscribing and unsubscribing to the CurrentTool method every time CurrentTool has been changed by bad practice?
I also thought that all referee instruments should be in shape, and the event would be signed by each instrument, and there would be no need for unsubscrinig. In my opinion, the biggest drawback is that every tool should check if it is CurrentTool.
What do you think about it? Thanks for the help.

+4
source share
3 answers

Performance is not a problem (when the user clicks, the overhead of invoking an empty function does not need to be unnecessarily), so this really concerns the ease of coding and the clarity of the code / complexity / maintainability.

Therefore, I would put it as simple as possible.

I would implement a base class with empty implementations, as it is clean and simple. To obtain the required results, a minimal code in a derived class is required. It also makes sense. If you do not redefine the upcall button, you essentially say "when I clicked the mouse, I don’t want anything about it").

The next option would be to provide events for the mouse up / down / click, as well as derived classes to subscribe to events, if they so wish. Using events is a standard template, but it does have a flaw that you have to mess up with ugly subscriptions and unsubscribed calls. The advantage of this is that if you make them publicly available, these events can be handled by anyone, not just derived classes.

I would avoid using interfaces and castings - for me it seems like a clumsy approach - all it really does is fragment the "empty functions" approach into several different types instead of a simple set of 3 virtual methods. And instead of just calling the methods and knowing that they will work, you have to do many types of casting and checks in the first place - it just seems messy.

edit Since you added something else to the question, I reread it, and another possibility arises: to create a base class Tool, which provides a virtual MouseDown handler that all derived classes must override. All normal instruments will get in shape.

The optional DragTool class can be obtained as an intermediate class that adds MouseMove and MouseUp handlers, which are necessary for your special pair of drag and drop tools.

i.e.

 ToolBase (abstract MouseDown) | +- ClickTool1 +- ClickTool2 +- DragToolBase (abstract MouseMove + MouseUp) | +- DragTool1 +- DragTool2 

This meant that there would be no empty implementations in any of your tools.

+2
source

Without knowing your scenario, I would go with a combination of interfaces and a base class:
The base class implements all interfaces with an empty virtual method. The base class is a clean convenience construct. If the tool class wants to inherit from the base class, but does not need a method, it does not cancel it.

In code that consumes tools that you will work with interfaces. Like other classes, you can directly implement your interfaces. You get maximum flexibility like this without any sacrifice.

 var mouseMoveListener = CurrentTool as IMouseMoveListener; var mouseDownListener = CurrentTool as IMouseDownListener; // ... if(mouseMoveListener != null) mouseMoveListener.MethodWhenMouseMove(); if(mouseDownListener != null) mouseDownListener.MethodWhenMouseDown(); 

Note: I used as instead of is in combination with as .

+1
source

It depends on the actual case. But in your specific case (user interface events), I believe that a base class with empty handlers (virtual methods) is better than many interfaces. In fact, all your tools inherit from some ToolBase. And the call code will be smaller and simpler without using an interface.

0
source

All Articles