There is an existing class
from a third-party library I want to reuse, but it does not implement some of the required interfaces. I'm thinking of wrapping it inside a struct
to fulfill the required interfaces (basically like how Scala does it). The reason why I prefer struct
over class
for this use case is because it only stores one thing: the wrapped object, and it probably won't incur performance penalties? But I'm not sure if boxing would occur if I pass a struct
to some method that takes in an interface? And are there any other drawbacks about this approach?

- 319
- 1
- 3
- 13
-
4If you access a struct via an interface reference it will be boxed, negating most of the benefits. – Mike Zboray Sep 27 '13 at 06:06
3 Answers
Microsoft gives very clear and straightforward reasons where you should use a struct
instead of a class
:
√ CONSIDER defining a struct instead of a class if instances of the type are small and commonly short-lived or are commonly embedded in other objects.
X AVOID defining a struct unless the type has all of the following characteristics:
- It logically represents a single value, similar to primitive types (int, double, etc.).
- It has an instance size under 16 bytes.
- It is immutable.
- It will not have to be boxed frequently.
https://msdn.microsoft.com/en-us/library/ms229017.aspx
And seems like it's not your case. And btw yes, boxing will occur if you pass a struct
to a method that takes an interface
. Also take into account that structures don't support inheritance and they can't have explicit parameterless constructors.

- 2,202
- 17
- 32
Yes you can use struct in this case, if you do not want both interface implementation(existing + extra you want to add) at a time. There wont be any overhead of wrapping because struct will store only reference not the whole object. Because objects will always be accessed via reference i.e. stored on heap. So there won't be any performance penalties. However it is not recommended practice
However if you have cases where you can use both interface implementation, you should define a new class derived which will have a variable to store the object of base class. In this way, you can use object of derived class and keep variable pointing to base class object as null.
Refer http://adarshdchaurasia.wordpress.com/2013/08/12/decorator-design-pattern/ to know the details of implementaion.
Also refer Can you have a class in a struct?

- 1
- 1

- 1,134
- 7
- 21
-
It's a misuse of the Decorator pattern in this case. It's useful if you want to add additional responsibilities to an object at RUNTIME. It obviously seems to be an [Adapter](http://en.wikipedia.org/wiki/Adapter_pattern). – Olexander Ivanitskyi Sep 27 '13 at 06:40
-
@Olexander: As shengmin said "There is an existing class from a third-party library I want to reuse, but it does not implement some of the required interfaces." He wants to implement some interfaces which are not already present in existing one. If he just wanted compatibility of the 3rd party API with his API/class then Adapter can used without any doubt. – Adarsh Kumar Sep 27 '13 at 06:47
-
@Olexander: What is the main difference between Decorator and Adapter? I think naming here is very fragile and subjective. And border between this patterns is very fuzzy and unclear. P.S. http://stackoverflow.com/questions/350404/how-do-the-proxy-decorator-adapter-and-bridge-patterns-differ – abatishchev Sep 27 '13 at 07:33
-
@abatishchev, if it's an interview, I will answer=) Decorator allows to add an additional functionality to the decorating object at runtime, Adapter allows to adapt an existing interface to a different kind of consumer (at design time). The both are kind of implementation of delegation technique, but the difference is clear enough, isn't it? – Olexander Ivanitskyi Sep 27 '13 at 07:35
-
@Olexander: I'd personally call runtime wrapper a proxy. Also adapter in my opinion is something working in both ways transparently like US/Euro plug adapter: none of 2 parties are aware. Anyway my general point is that border is clear in synthetic examples and less clear in real world. – abatishchev Sep 27 '13 at 07:59
-
@abatishchev: - Adapter provides a different interface to its subject. Proxy provides the same interface. Decorator provides an enhanced interface. - Decorator and Proxy have different purposes but similar structures. Both describe how to provide a level of indirection to another object, and the implementations keep a reference to the object to which they forward requests. Refer http://adarshdchaurasia.wordpress.com/2013/08/12/proxy-design-pattern/ – Adarsh Kumar Sep 27 '13 at 08:08
-
1@abatishchev, I thought the most useful thing about patterns was to name things by generally accepted names=) – Olexander Ivanitskyi Sep 27 '13 at 08:42
Much simpler would be to directly inherit the 3rd-party class in your code and then implement the interface you desire on this derived class. No boxing; only references will be passed.

- 33,414
- 24
- 162
- 251
-
@shengmin: You can define a wrapper class which contains variable to hold 3rd party class object and implement extra classes you need to implement. – Adarsh Kumar Sep 27 '13 at 06:49