Why should I inherit a interface instead of inheriting its contract in solidity? - blockchain

I'm learning the usage of interface in solidity, and I think its most usage is to provide a API for other contract or Dapp to call.
I also notice a lot of contract inherit the interface. I wonder what is the advantage for a contract to inherit the interface of another contract instead of inheriting the another contract itself?
I can't convince myself since the following reasons:
Inheriting interface needs to write the interface first.
Inheriting interface needs to rewrite the function code.

Related

GMock - Mock class from third party libary

I'm looking at GMock and trying to decide if it is going to be feasible for some unit testing I want to do.
If I understand correctly, any class you Mock has to have an interface that the mock can be created from, and any class you want to test, has to be able to accept the mock via injection.
Am I correct so far?
If I am correct, so far, I do not see how it is going to help me in testing classes that use a class from a third party.
For example:
Suppose I wrote a ChatClient class, and when implementing it I use QWebSocket from the Qt Library. ChatClient has a member ChatClient::m_socket of type QWebSocket.
Now, there is no interface for GMock to create a mock websocket from, and my chat client doesn't take an interface via dependency injection in production code. How a the QWebSocket works is so specific, that I'd never be able to use a different concrete implementation.
However, I do want to test that my ChatClient, calls connect when its supposed to, passes the correct bytes to its calls to QWebSocket::sendBinaryMessage, reacts to incoming message correctly, etc., and it seems a mock framework would be useful to test my ChatClient in that way so I don't have to actually connect to a network.
What do I do, or is this just not doable in C++ like it is in other languages because we don't have reflection?
Do I just create my own interface for the third party class, with every call I make to it, forward it to the third party class, and then have my ChatClient take that interface, even though I will never ever use another concrete implementation, just to enable testing?
"Just to enable testing" is would be good enough for me, but I agree that if there're many methods there would be a lot of boilerplate...
See static dependency injection: Mock static method from external Class (that I can't change!)

Why are Interface Mocks More Desirable than Subclass Mocks when Unit Testing?

When a class implements an interface, then it is easy to mock, but, you have to create an interface for it.
You can also mock by subclassing and overriding. If your base class provides a parameterless protected constructor, then your subclass mocks are not tied to changes in the base class constructor.
At first glance, it seems like the subclassing method to creating mocks is more desirable than creating interfaces for everything, but obviously, that's not how most people are doing it.
So, why are mocks based on interfaces considered a better practice than mocks based on subclassing?
Contrary to most beliefs, even if you do Test-Driven Development (TDD), you should still follow good design practices. The trick is to make APIs testable and well-designed. TDD isn't a design methodology, but a feedback technique.
Therefore, it's not a question of Interface Mocks versus Subclass Mocks, but rather a question of interfaces versus subclasses.
That's a really old discussion that ought to have been over back in 1994; in Design Patterns, we learn that we should favour composition over inheritance.
There's a long discussion about this topic, but in short, in languages with single inheritance, basing an API design on inheritance is to constrain all future variability along other (yet unforeseen) dimensions. Inheritance in such languages is simply too restrictive. Since all major Object-Oriented languages (Java, C#, Ruby) have single inheritance, you ought to see most Object-Oriented code bases correctly avoiding inheritance.
When a design is based on composition over inheritance, it follows that if you use mock objects, you'll be mocking interfaces, not base classes.
When you subtype and override, you have the risk of missing overriding one of the methods you should have overridden, and actually running the "production" code, which you'd want to isolate away from your test.
When you mock an interface, 100% of its behavior must be mocked. Whatever isn't explicitly stated by mocking will just throw an exception, and force you address it.

Why shouldn't my ViewModel depends on concrete implementation of services?

My ViewModel usually have a dependency to some services through interfaces (DI containers will inject the actual implementations). So that I could create a mock behavior of those services for UnitTests.
public class MyViewModel
{
public MyViewModel(IMyService myService)
{
}
}
But we have some mocking framework exists like Moq, where you can setup mock behavior for your concrete implementations of services even without interfaces. Does that mean, the ViewModels can have a dependency with concrete service classes instead of interfaces? Is there any other purpose for having interfaces apart from UnitTests?
But we have some mocking framework exists like Moq, where you can setup mock behavior for your concrete implementations of services even without interfaces
Not exactly. With Moq you can only mock something that can be overriden/implemented. In most cases this would be an interface but sometimes a class with virtual/abstract methods.
Now, if your services expose methods as virtual (question is, why would they do that?) then sure, you can use Moq and be able to mock them. Otherwise not.
The point of passing interfaces is to rely on abstraction rather than concrete implementation. This is D of SOLID principles - Dependency Inversion Principle.
the general idea is to have a project that can easily evolve. interface gives you a bit less coupling because you can have different implementations, including test implementation (mock). that's the theory.
from my experience: if you have only one service implementation and you can easily mock it then it's sufficient to have only implementation without the interface. if, in future, you need another implementation, you can refactor. but still it requires two things.
defined interface: still you need a clear definition of the contract your service provides. so you need some methods to public, some other to be private. and your view can't use private methods or it will be much more difficult to do any changes
devs must understand the interface. if you have explicitly defined interface then it's a good documentation. if you don't have it, it's easier for your team to change it in a way it starts to use features that were supposed to be internal service implementation

COM Interface Methods limit

I am going to develop a COM component for a WIN CE device. Is there any limits on the number of methods that can be added to a COM Interface(Component)?
One of the benefits of an interface is that multiple classes can reuse your interface. This would allow programs to use the same methods on different classes without caring what they are.
One of the intentions of COM is to hide the implementation of the class you are accessing. The user isn't suppose to know, given an interface, what is exactly behind that interface.
Imagine you have a chair, now imagine you have a bike.
Both can use the interface ISeat. The user can sit on either object without knowing which the user sat on. Now bike can use the interface IPedal, and chair wouldn't use that interface. Now chair doesn't have to have all the methods that bike implements (like PedalForward).
If chair had to implement those methods because you only have one interface, you'd have to do something with those methods. You could have the method do nothing, but that would be useless to the user, and possibly dangerous, as the user could be trying to pedal a chair away from a car so he doesn't get hit by the car. Or, you could throw an exception. Which would be odd, as the user would wonder why the object can't pedal forward.
So, if you have too many methods in one interface, presence of god-class aside, you're not taking advantage of COM in the way it was meant to be used.
No.
But if you have to add a lot of methods to a single class, there's a very good chance your design is broken.
Also, keep in mind that once you publish a COM interface, your never "supposed" to change it.

Reconciling Unit testing with OOD

TDD is all the rage these days and an ever growing number of software shops are converting to agile,scrum, etc. I can certainly see advantages of automated testing but I also see TDD as contradicting some principles of good object oriented design.
TDD requires you to insert seams in your code which expose implementation details through the interface. Dependency injection or collaborator injection violates the principle of information hiding. If your class uses collaborator classes then the construction of these collaborators should be internal to the class and not exposed through the constructor or interface.
I haven't seen any literature addressing the conflicts between writing testable code and at the same time adhering to the principles of encapsulation, simplicity and information hiding. Have these problems been addressed in any standard way?
The methods and classes you think are implementation details are really seams that represent axes along which you can vary and recompose components into new constellations.
The classic idea about encapsulation tends to be too coarse-grained because when you hide a lot of moving parts you also make the code very inflexible. It also tends to violate a lot of the SOLID principles - most prominently the Single Responsibility Principle.
Most object-oriented design patterns tend to be rather fine-grained and fit well into the SOLID principles, which is another indicator that proper object-oriented design is fine-grained.
If your class uses collaborator classes then the construction of these collaborators should be internal to the class and not exposed through the constructor or interface.
These are two different things mixed together. I agree that in most cases the collaborators should not be exposed through the interfaces. However, exposing them through the constructors are the correct thing to do. Constructors are essentially implementation details of a class while the interfaces provide the real API.
If you want to preserve a coarse-grained API to target default functionality you can still do so by supplying a Facade. See this post for more details: Dependency Inject (DI) "friendly" library
Perhaps there is little literature because it is a false dichotomy?
TDD requires you to insert seams in your code which expose implementation details through the interface.
No, the constructor or method to inject the dependency with need not be part of the interface the calling class uses:
class Zoo {
Animal exhibit;
}
interface Animal {
void walk();
}
class Dog extends Animal {
DogFood food;
Dog(DogFood food) {
this.food = food;
}
}
If your class uses collaborator classes then the construction of these collaborators should be internal to the class and not exposed through the constructor or interface.
In the above example, the Zoo can not access the DogFood, as it gets the Dog after it has already been fed, and the Dog doesn't expose its food.
Mark Seemann's answer is excellent. "Units" in industry often break the Single Responsibility Principle and make the system difficult to maintain due to all the internally hard-wired dependencies. TDD exposes flaws like this nicely. Units can then be built up like Lego blocks to form larger functional units that actually perform useful business logic. Really, I see TDD as supporting building good OO systems very well.
Don't worry about hiding things so much. Think of private as meaning "you are not allowed to access this" not "the system does not currently need to access this". Use it carefully - usually for things that screw up the state of an object if accessed from outside at the wrong time.
There are a few things that can be done to integrate TDD with OOP, depending on the language in question. In Java, you can use reflection in order to test private functionality, and the test could be placed in the same package (preferably in a separate source tree) in order to test package-private functionality. Personally, I prefer testing functionality only via the public API of the code in question.
I don't know of any "official" resources on the subject, though I know that Uncle Bob has written extensively on the subject of TDD, and considers it compatible with his "SOLID" principles of OOP.
In my experience TDD is highly supportive of the principles of object oriented design.
Dependency Injection isn't an artifact of TDD, it's commonly used in the design of OO frameworks regardless of the development methodology.
Dependency Injection is about loose coupling - if Class A uses one or more objects from Class b, a good design will minimise the knowledge class A has of the internals of class B. I believe this is what you are referring to when you mention 'information hiding'.
Consider what happens if Class B changes it's implementation. Or, a more complex but still common occasion, what if you want to dynamically substitute in different sub-classes of B depending on the situation (you might be using the Strategy pattern, for example), but the class that makes this decision is not class A.
For this reason Java has Interfaces. Instead of making class A dependent on class B, you make it dependent on the Interface which class B implements. Then you can substitute any class that implements that Interface, without changing the code inside A.
This includes, but is in no way limited to, substituting fake objects for testing purposes.
TDD makes uses of Dependency Injection, absolutely. Just like TDD makes use of many principles of OO. But DI is a principle of OO, not a principle of TDD.
When using dependency injection, the object construction and object graph management will be handled in the factory. Objects are created via factories, so the object creator would not be aware of the dependencies of the created class.
For example if class A has dependencies B and C that are passed via the constructor, B and C will be provided by the factory method. The object creating A would not be aware of the objects B and C.
Dependency injection frameworks like Google Guice and Ninject can be used automate the factory creation.