🌱 This post is a part of my digital garden. It's a place where ideas grow. It's not a polished article, but more of a living breathing breathing document. 🌱
We're not talking about user interfaces, so what is an interface anyway?
Do you remember the Nintendo64? Different cartriges (games) could be popped into and out of any console. That's because there was a specification somewhere that layed out exactly how the two pieces connect together. Every single N64 cartrige and console in the world was designed and built according to those same specifications.
In software, interfaces describe how a class is used by another class.
benefits behavior from multiple source (can't inherit multiple classes in C#) must use an interface for a struct as well (can't inherit from classes / other structs) I convention
1interface IBasicCalculator {2 int Add(int firstNumber, int secondNumber);3 int Subtract(int firstNumber, int secondNumber);4}
the interface doesn't care how the numbers are added, just that there is an add / subtract method.
Let's try to think about implementing an interface in a (fake) real life scenario. Say we have a software product called HappyCustomer that is used by other businesses. One portion of the application helps them handle customer relationship management (CRM). It only does a few things:
Here's the customer class that represents each record in the table :
1public class Customer2{3 public int CustomerId;4 public string FirstName;5 public string LastName;6 public string CustomerStatus;7}
And here's the CRM class:
1using System.Collections.Generic;23public class HappyCustomerCRM4 {5 public List<Customer> Search(string searchText)6 {7 List<Customer> SearchResults = new List<Customer>();8 SQLService Service = new SQLService();9 SearchResults = Service.SearchCustomers(searchText);10 return SearchResults;11 }1213 public Customer GetById(int customerId)14 {15 Customer Customer = new Customer();16 SQLService Service = new SQLService();17 Customer = Service.GetCustomerById(customerId);18 return Customer;19 }2021 public Customer UpdateStatus(int customerId, string newStatus)22 {23 Customer UpdatedCustomer = new Customer();24 SQLService Service = new SQLService();25 Customer = Service.UpdateCustomerStatus(customerId, newStatus);26 return Customer;27 }28 }
The compnay has never had a reason to change this code, but there's a problem. They're courting a new client who already uses Salesforce to handle customer relationship management. They want to use Deco's software, but they will only do so if they can use their existing CRM.
They could create a brand new class with it's own methods