Ok, I know odd title, but I promise this will all get explained.
First, let’s start with some definitions of these concepts.
Polymorphism-
I like the Wiki version of this definition so here’s the link: http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming
For those who prefer not to click links and stay here, I will explain in a 1000 foot overview way. Basically in object-oriented programming, this is the ability to create a value, variable, function, or object that has more than one form. Shapeshifting so to speak, just as it would mean if not referring to coding. WOW! Simple enough right?
Structs-
MSDN does a fairly good job describing this:
http://msdn.microsoft.com/en-us/library/saxz13w4.aspx
Here, a struct is essentially the same as a class but it’s a value type. Just a little more limited, but can be less expensive, especially when using smaller objects that may be contained in large collections for example.
To get deep with it, a class is allocated in memory and allocates pointers (IntPtr) to the object wherever you place a field for reference type objects, classes.
However, structs, or value types, are allocated where you place them and do not generate pointers. This does mean they will copy and become unique allocations moving through the call stack and around the different scopes of your application, this can make them both easier and more difficult to control.
Interfaces-
MSDN also does their job well here:
http://msdn.microsoft.com/en-us/library/ms173156.aspx
Interfaces can be a complex topic for some, but to try to sum it up as simple as possible. They contain a reference to an object which implements the same methods, properties events that the interface defines. If you’ve dealt with C++ these will look a lot like class definitions in header files, and ALMOST behave like them. Interfaces cannot be instantiated and cannot define code implementation for it’s members.
Alright, Polymorphism in general is a huge topic of coding, so we’re not getting too deep, just showing one AMAZING example, though it may be hard to see all the benefits without trying it yourself.
If you’ve dealt with structs before you’ll know a few things about them, such as the inability to use inheritance, and the frustrating ref keyword when trying to pass them into methods as a reference type instead. So, their benefits typically get outweighed by the aggravations of these things. Here’s a tidbit that a lot of people forget, though structs cannot inherit they can implement an interface. It gets better though. Interfaces can inherit other interfaces….
Due to the two above facts, we can use polymorphism techniques to build a collection of structs which inherit each other, and can be passed as reference types without the ref keyword.
My example, which I’ve included the class diagram for, uses the following structs and interfaces:
- IVector
- IVector2
- IVector3
- IVector4
- Vector2
- Vector3
- Vector4

Since the structs, the one’s without the ‘I’, cannot inherit each other all the inheritance tree is in the IVector-IVector4, and it just goes incrementally.
Ok, got that part, but what does this allow me to do?
void RandomMethod( IVector aV1 ) { }
The above method declaration accepts IVector, so what can be passed into this method? This method can actually accept ANY Vector type as it’s interface description. It could use IVector.AxisCount to determine which one was actually passed and handle it appropriately.
That’s one use, which is pretty cool. What about this one?
Vector4 v = new Vector4( 1,1,1,1 );
IVector2 v2 = v;
Will this even work? YES, this just gave me a reference to the Vecto4, the object is obviously still the same object, but rather than converting the value, we made it accessible as a Vector2 interface. Of course you would need methods that accept the interface instead of the struct to use it. Hence why IVector2.ToVector2() exists. This is a simple convert, where if the internal object is not a Vector2, it converts it before returning it.
I know this is a lot to take in so I will stop here. Have fun!!!
No comments:
Post a Comment