Assuming I'm using a class from third party library that does not implement an interface, for example:
class ThirdPartyLibClass {
void DoThis() { ... }
void DoThat() { ... }
}
I want to create a very thin wrapper around it, directly reflecting class' interface and delegating to the ThirdPartyLibClass
. The purpose of this is to stub ThirdPartyLibClass
in my unit tests. Example:
interface IThirdPartyLibClass {
void DoThis();
void DoThat();
}
class DefaultImplementation : IThirdPartyLibClass {
private ThirdPartyLibClass realImplementation = new ThirdPartyLibClass ();
void DoThis() {
realImplementation.DoThis();
}
void DoThat() {
realImplementation.DoThat();
}
}
Is there a name for this pattern? Wrapper or Adapter seem to differ slightly, and I don't intend to ever swap the implementation in production code, so the interface is exactly the same as that of ThirdPartyLibClass
. Also, how would I call the DefaultImplementation
to make the pattern usage clear to the reader?
Thanks in advance.
2 Answers 2
Creating a wrapper interface and having the default implementation just call into a wrapper type is an example of the delegation pattern.
-
Thanks, yes this seems right. What about the other part of the question, i.e. naming of the wrapper (a suffix) so that the pattern is clearly visible. Since I'm using C# the word
delegate
already has specific meaning that can be confusing. "Delegator"? ;) "DelegatingWrapper"?Piotr– Piotr01/11/2017 10:19:16Commented Jan 11, 2017 at 10:19 -
1Anyway I think I'm taking this too far.
IThirdPartyLibClass
andDefaultThirdPartyLibClass
orDefaultThirdPartyLibClassWrapper
will suffice. I'll accept your answer. Thanks.Piotr– Piotr01/11/2017 10:25:26Commented Jan 11, 2017 at 10:25 -
According to wikipedia the "delegation pattern"-s purpose is to support "composition over inheritance". I prefer @Doc Brown-s "test wrapper"k3b– k3b01/11/2017 10:27:26Commented Jan 11, 2017 at 10:27
-
@k3b and composition is exactly what is being used in the OP's code. "Test wrapper" is an OK name for it, but it is an example of the delegation pattern.David Arno– David Arno01/11/2017 10:29:38Commented Jan 11, 2017 at 10:29
-
No offense, but sometimes I get the impression bored people invent new terms just by taking an existing term and write the word "pattern" after it.Doc Brown– Doc Brown01/11/2017 12:12:52Commented Jan 11, 2017 at 12:12
It's a wrapper, that is all. There is no software problem here that is being solved, so there is no design pattern here. The problem being solved is the bureaucratic problem that testable classes should have an interface and should be accessed through an interface, this one doesn't have an interface and cannot be modified, so you write a wrapper. No pattern.
-
-
2The point here is not to test
ThirdPartyLibClass
but to test something that uses it. ImagineThirdPartyLibClass
coming from a closed-source library and being for example a class operating on a database. In my own code, that uses this class, I don't want to instantiate it in the test (would require a database instance). I think it's quite obvious so unless I misunderstood your answer, I don't see how this is a bureaucratic problem?Piotr– Piotr01/12/2017 20:01:45Commented Jan 12, 2017 at 20:01
Explore related questions
See similar questions with these tags.
DefaultImplentation
just delegates all API calls directly to the lib, I don't know a shorter term to describe this.