I am going to keep this tutorial short-and-sweet with most of the learning hopefully coming from looking at code and brief remarks about it. Hopefully you leave the thread feeling a bit better about interfaces than you did before reading it. So lets just start right away and design a basic set of interfaces.
The goal is to make a design that allows quick and easy loading/saving/reading of custom settings. Now here is a question that really highlights the importance of object oriented programming in general, and as a bonus, the value of interfaces. What settings format do we want to design our system for? Json? XML? Ini? Some settings format we do not even know we want yet? How about one that does not even exist yet? The answer is.. none of the above.
That is the beauty of core object oriented programming. Solid interface design allows us to not care about the questions above. You can change the implementation, not the design. Changing your entire design 10 years into the applications life is not an easy task. Changing the implementation of the way you load/save settings from JSON to XML is.
So lets go ahead and design it step by step.
Note, for all the classes here, the only namespaces you need to include are:
using System.Collections.Generic; using System.IO; using System.Text; using System.Xml.Serialization;
First, we need a way to load and save the settings and access its contents. Lets make this in of its self an inter-face as it is useful for a wide number of operations. We will define four basic operations, which the method names describe.
Great, now we have defined a contract of sorts to handle the data we want.
Now lets design a basic interface to be used to manage a settings file. We use the <out T>, which is just using covariance, which allows you to use a more derived type than that specified by the generic parameter.
So there is our two interfaces. Now lets show how they can be used to implement a settings manager of sorts using the interfaces above.
Now lets tie them all together for something useful. Lets just say we are feeling XML today and that is what we want our settings to use. No problem, we just need to implement the ISerializeTool. Here is mine:
Now, for examples sake, here is a simple test you can use to try all the above.
I hope you have a better grasp on interfaces and C# after reading this. Good luck.