Is it better to pass * an interface * or * an object * as a parameter to a function?

I am trying to convince a colleague that a function should perceive the interface as a parameter, and not the object itself. I think small objects can be beautiful, but for large objects I would give them an interface and just pass i / f, not all.

Note that there will be only one of these large classes - i / f will never be used for another object. This is just to hide the implementation of the object.

Do you agree that splitting a large class into an interface is good practice?
Are there any drawbacks to this?

Example:

public interface class IVeryLargeClass { void DoSomething(); ... }; public ref class VeryLargeClass : public IVeryLargeClass { public: virtual void DoSomething() { ... } ... }; public ref class AnotherClass { public: AnotherClass(VeryLargeClass^ vlc) { vlc->DoSomething(); } // OR AnotherClass(IVeryLargeClass^ vlc) { vlc->DoSomething(); } }; 
+4
source share
5 answers

One of the first principles that you learn when developing OO:

A program for an interface, not an implementation.

You indicate that "there will be only one of these large classes - i / f will never be used for another object." This may be true in your case, but I'm sorry that I did not have nickel for every time such an expression turned out to be wrong.

In addition to considering whether there can be several implementations of your interface, you should also consider whether your particular object is exporting (or can export) additional methods that do not share logical proximity with operations declared in the interface. In this case, you can simply declare additional operations in one or more additional interfaces. Then the client only needs a couple with an interface that exports operations in which he is interested.

Simply put, interfaces provide a means of managing communication between clients and providers.

+12
source

The principle of dependency inversion can be summarized as: It is better to depend on abstractions than nodules.

It is almost always better to pass an interface than pass a specific class.

Nevertheless, high consistency with internal types is good within a specific module, but it is very subjective when and how you should pass specific objects.

+7
source

If you pass the implementation, you may lose one of the advantages of using interfaces, that is, to separate the logic from the real implementation.

The interface of software module A is intentionally kept separate from the implementation of this module. the latter contains the actual procedure code and methods described in the interface, as well as other "private" variables, procedures, etc. Any other program module B (which may be referred to as a client for A) that interacts with A is forced to do this only through the interface. One practical advantage of this is that replacing an implementation of A with another that matches the same characteristics of an interface should not cause B to fail - as long as its use of A matches with the characteristics of an interface (see also Liskov’s replacement principle).

http://en.wikipedia.org/wiki/Interface_(computer_science)

+3
source

I would rather not create or create an interface to create an interface. If you can use this interface in more than one place, you have a winner - or if it is a public function and class, and you specifically want to simplify it.

+3
source

Another problem is your very large class. It may not be within the scope of what you are doing, but a very large class implies that it can do too much in the first place. Can you reorganize it into less class, where the information required in the function you are calling is encapsulated in its own smaller class? If you are creating an interface for this class, you may find it much more reusable than against the largest class that you have.

+1
source

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


All Articles