chuckdawit wrote:
Should I replace the 'SomeObjectIveCreated2' with an interface to adhere to rules of encapsulation? This is helpful for unit testing but is more for the rules regarding the S.O.L.I.D principles.
You should understand that interfaces, as opposed to abstract classes and "classic" OOP (before introduction of interfaces) does not really "improve" encapsulation. Encapsulation is more basic OOP feature, not directly related to the heart of OOP, which is related to late binding and polymorphism.
Instead, interfaces provides
loose coupling between interfaces and implementation. (Please don't be confused here: I used the word "interface" in two different meanings in one phrase — sorry; in first case, "interface" is used in narrow sense of this word, as a kind of type in .NET and other technologies typically using keyword "interface; in second case, the meaning of interface is abstract, in this sense, the content of abstract class and .NET interface are both interfaces.)
Besides, in .NET and other technologies without
multiple inheritance in the sense C++, interfaces provides the only available multiple inheritance, sometimes called "weak form of multiple inheritance".
Typically, you use interfaces when you want some common features outside of the class hierarchy. Moreover, interfaces can be implemented by
struct
types, which are value types, not reference types. This way, polymorphism based on interfaces, is "more abstract": it hides the difference between
reference types and
value types: they all are represented in a polymorphic set through some base
compile-time type which is the interface managed pointer, as if they all were reference types. And the weak form of multiple inheritance allows the same object instance to participate in different polymorphic sets with different base types. Such kind of loose coupling allows to go beyond the tree structure of inheritance diagram.
Interfaces are often used in APIs to free the API users from fitting their implementation types in rigid class hierarchy.
Now, you need to identify if you really need the flexibility of interfaces in your application.
—SA