In this part of my C# tutorial we’ll look at Interfaces using both a simple example and another that is rather complex. We’ll also look at Abstract Methods, Inheriting an Interface, How to Create Flexible Designs, and the Command Design Pattern.
For best results take notes on the cheat sheet provided below as you watch and leave any questions you have.
If you like videos like this consider donating a $1 on Patreon, but a Like is also greatly appreciated.
[googleplusone]
Code & Transcript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 |
// ---------- IDrivable.cs ---------- namespace CSharpTutA.cs { // An interface is a class with nothing but // abstract methods. Interfaces are used // to represent a contract an object may // decide to support. // Interfaces commonly have names that // are adjectives because adjectives // modify nouns (Objects). The also // describe abstract things // It is common to prefix your interfaces with // an I interface IDrivable { // Interfaces can have properties int Wheels { get; set; } double Speed { get; set; } // Classes that inherit an interface // must fulfill the contract and // implement every abstract method void Move(); void Stop(); } } // ---------- Vehicle.cs ---------- using System; namespace CSharpTutA.cs { class Vehicle : IDrivable { // Vehicle properties public string Brand { get; set; } public Vehicle(string brand = "No Brand", int wheels = 0, double speed = 0) { Brand = brand; Wheels = wheels; Speed = speed; } // Properties and methods from // the interface public double Speed {get; set;} public int Wheels {get; set;} public void Move() { Console.WriteLine($"The {Brand} Moves Forward at {Speed} MPH"); } public void Stop() { Console.WriteLine($"The {Brand} Stops"); Speed = 0; } } } // ---------- Program.cs ---------- using System; // A class can support multiple interfaces. // Create an interface Project -> Add New Item // and select Interface namespace CSharpTutA.cs { class Program { static void Main(string[] args) { // Create a Vehicle object Vehicle buick = new Vehicle("Buick", 4, 160); // Check if Vehicle implements // IDrivable if(buick is IDrivable) { buick.Move(); buick.Stop(); } else { Console.WriteLine("The {0} can't be driven", buick.Brand); } // We are now modeling the act of // picking up a remote, aiming it // at the TV, clicking the power // button and then watching as // the TV turns on and off // Pick up the TV remote IElectronicDevice TV = TVRemote.GetDevice(); // Create the power button PowerButton powBut = new PowerButton(TV); // Turn the TV on and off with each // press powBut.Execute(); powBut.Undo(); Console.ReadLine(); } } } // ---------- IElectronicDevice.cs ---------- namespace CSharpTutA.cs { // With interfaces you can create very // flexible systems. Here I'll model // generic electronic devices like // TVs and Radios and remotes that // control them and the buttons on the // remotes. interface IElectronicDevice { // We want each device to have // these capabilities void On(); void Off(); void VolumeUp(); void VolumeDown(); } } // ---------- Television.cs ---------- using System; namespace CSharpTutA.cs { // Because we implemented the // ElectronicDevice interface any // other device we create will know // exactly how to interface with it. class Television : IElectronicDevice { public int Volume { get; set; } public void Off() { Console.WriteLine("The TV is Off"); } public void On() { Console.WriteLine("The TV is On"); } public void VolumeDown() { if (Volume != 0) Volume--; Console.WriteLine($"The TV Volume is at {Volume}"); } public void VolumeUp() { if (Volume != 100) Volume++; Console.WriteLine($"The TV Volume is at {Volume}"); } } } // ---------- ICommand.cs ---------- namespace CSharpTutA.cs { interface ICommand { // We can model what happens when // a button is pressed for example // a power button. By breaking // everything down we can add // an infinite amount of flexibility void Execute(); void Undo(); } } // ---------- PowerButton.cs ---------- namespace CSharpTutA.cs { class PowerButton : ICommand { // You can refer to instances using // the interface IElectronicDevice device; // Now we get into the specifics of // what happens when the power button // is pressed. public PowerButton(IElectronicDevice device) { this.device = device; } public void Execute() { device.On(); } // You can provide a way to undo // an action just like the power // button does on a remote public void Undo() { device.Off(); } } } // ---------- TVRemote.cs ---------- namespace CSharpTutA.cs { class TVRemote { // Now we are modeling the action of // picking up the remote with our hand public static IElectronicDevice GetDevice() { return new Television(); } } } |
Leave a Reply