In Java, is there a performance gain when using interfaces for complex models?

The name is hardly understandable, but I'm not sure how to summarize this differently. Any changes for clarification are welcome.

I was told and it is recommended that you use interfaces to improve performance even in a case that does not particularly require the regular role of an “interface”. In this case, the objects are large models (in the meaning of MVC), with many methods and fields.

The “good use” that I recommended was to create an interface with its unique implementation. Of course, there will be no other class implementing this interface. I was told that it is better to do this because it "provides less" (or something close) to other classes that will use methods from this class, since these objects refer to an object from its interface (all public methods from the implementation, reproducible in the interface).

This seems rather strange to me, as it seems to me that using C ++ is for me (with header files). There I see the essence, but in Java?

Does it make sense to create an interface for such a unique implementation? I would really appreciate some clarification on this topic, so I could justify the lack of such behavior, as well as the difficulties encountered when duplicating all ads.


Edit: Thanks to everyone for the answers, it was really helpful and instructive (most of them, not just “accepted”).

There are clearly no advantages in performance, and now I have more features that it may have (depending on the situation), in addition to the usual OO-function of the interface.

+4
source share
10 answers

Using interfaces has little to do with performance (with the possible exception of the productivity of the development team, that is, the speed of development). This is more about controlling dependencies and separating unrelated parts of a program.

If you depend directly on a particular C class in your code, this code is more complex than unit test, among others. If you instead depend on the interface, then snap-ins are enough to create modular implementations in unit tests.

Of course, you may not need to pull all the methods of your class into the parent interface. In fact, you may not need one parent interface . Analyze the use of this class and (especially with a large class such as yours), you will find two or even more different groups of methods used by different clients (for example, one group of clients requests only the state of the object, and the other updates it). This allows you to create two or more separate interfaces, which are much simpler and cleaner.

This analysis may even lead to the conclusion that your class is trying to do too many things (instead of having sole responsibility ) and you better extract some of your content into a separate class! In other words, as soon as you start thinking about interfaces and programming, you begin to see design at a different level, and this can lead to better design decisions.

All this said, if after all the above analysis you still do not see the interface for your model class, write about it. Repeat it, say, in six months. Then , if you still feel that he did not pay for himself, just discard him . Interfaces - like any other element of your program - should always have a clear purpose and reason for existence (and better than "my colleague told me to create it"). They are not a panacea. If you use them smarter, you will improve your code. If you use them stupidly, you make your code even worse. The fact that you posted this question here means that you want to learn how to use them correctly, which is good :-)

+12
source

First of all, interfaces do not have any performance benefit. In any case, the use of polymorphic code and dynamic dispatching (for example, C ++ virtual functions) carries a cost.

Think of interfaces not as adding expressive power, but as something that makes it difficult for you to write. Interfaces are important, IMHO, for three reasons:

1) They can help you write code with better encapsulation, data hiding, stability, etc.

2) They can help you separate behavior and implementation by thinking about what you are trying to imagine, rather than how to implement it. The inability to add state does a lot so you cannot accidentally add it.

3) They allow you to expand things in the future and separate your code from independent units. Each component knows only what it needs to know about other services.

You can do some of this with abstract classes that have no state, but interfaces are specifically designed for this and carry the benefits of multiple subtyping. In C ++, such abstract classes are often used to mimic interfaces.

Now, if you can write a huge system with an ideal level of encapsulation directly with classes, this is great. But with classes, it is often tempting to prematurely make a fortune and create connections.

Should you always write interfaces? Of course not. Always be afraid of the "Always" rules :) This is always a balance between wasteful work and useless complexity and advantages. With time and experience you get the instinct for this.

In addition, the header files are essentially interface files. Usually they determine acceptable behavior, but not state. If you learn C and then Java (some schools do), you will learn about ADT in C and then about ADT in Java using interfaces.

+4
source

In fact, using interfaces instead of classes allows you three things:

  • Cancel the contract from its implementation: the contract is the interface (this is what you declare what you will do), and the class is obviously the embodiment
  • Provide easy administrative implementation. Typical examples of this involve replacing the storage engine with another without changing the user code (e.g., pushing MySQL, e.g., Project Voldemort).
  • As an advantage of 2 (and this time easily extensible for model elements) interfaces allow you to perform several tests using a white box: using a mocking structure (JMock, EasyMock, ...), you will be implementing an interface whose behavior may be suspicious, so called a "layout" that returns exactly what you expect from your test. This will allow your test not only to test one component, but also to ensure that the error comes from this component, and not from its internal contractors (the interface that you give it).
+3
source

In short -

  • You document the API for this module explicitly in the interface object. It will become more understandable for your users and, perhaps, will make you think about what is happening in this interface.
  • you can provide multiple interfaces on the same object and limit consumers to what they can do (for example, read-only, read-write)
  • mocking frameworks use interfaces, and mocking classes are a very powerful testing method.
+3
source

This reason for using interfaces seems very strange to me. I don’t see how there might be some kind of performance advantage, and if it were, it would probably be completely unimportant.

However, there are several reasons to use interfaces, even if one implementation currently exists. It may be easier to distribute later or provide alternative implementations. (Although there is a refactoring of the extract interface in any suitable IDE.) It can be a lot easier to check: if you want to test a class that uses other classes only through interfaces, you can easily provide an object layout for other classes.

+2
source

Here are 2 links that explain why you want to do this:

http://www.artima.com/lejava/articles/designprinciples.html http://pragmaticjava.blogspot.com/2008/08/program-to-interface-not-implementation.html

This has nothing to do with the performance of the execution (which is more related to the performance of the programmer (you)), since you disconnect class users from its implementation - among other things, to facilitate testing and maintainability.

Of course, there will be no other class implementing this interface.

Maybe not this year, but maybe next year.

+1
source

Interfaces are incredibly overestimated in Java, partly because of the design of the language, and partly because people think that creating a lot of interfaces is a good design.

Abstract classes will outperform interfaces if Java allows multiple inheritance. Scala, the new programming language for the JVM, has the concept of traits, which are essentially abstract classes with MI capabilities.

The advantage of an abstract class is that you can add behavior to an abstract class while violating existing classes that extend it. Thus, an abstract class is also better than an extension mechanism (plugin) and then interfaces.

Unfortunately, JMock and his brothers encourage many interfaces for testing BDD, but this is not necessarily the case because there are several libraries for mocking specific objects.

+1
source

This question has targeted C # , but I think it applies as well. The highest voted answer says that defining an interface for each class is just code noise .

0
source

There is a relatively common developmental dogma that there should be an interface for almost everything. As far as I can tell, this is based either on the burden-cult mentality in relation to “program to interface, not implementation” (which, of course, does not require you to put code with interfaces around) or to do test development with older fake ones frameworks that could not mock specific classes.

Do not pay attention to such nonsense.

0
source

Another way to abuse interfaces?

There is no obvious performance gain using interfaces. Conversely, this can adversely affect RTTI performance (based on your compiler capabilities).

0
source

Source: https://habr.com/ru/post/1306915/


All Articles