E N D
Interfaces Reference: Joe Hummel
Objectives “Good class design starts with good application design — how many classes, how do they relate to one another, how decoupled do I want the system, etc. Inheritance and interfaces are the primary tools for going beyond basic class design into the realm of application design…” • Interfaces • Polymorphic programming • Interface-based programming
Part 1 • Interfaces…
Interfaces • An interface represents design • Example: • Designing an object that can be used for iterating over a data structure • Interface: only method signatures, no implementation! (All methods are abstract) Also as generic public interface IEnumerator { void Reset(); // reset iterator to beginning bool MoveNext(); // advance to next element object Current { get; } // retrieve current element }
Why use interfaces? • Formalise system design before implementation • especially useful with large systems. • Contract-based programming • the interface represents the contract between client and object. • Low coupling! • decouples specification and implementation. • facilitates reusable client code. • client code will work with both existing and future objects as long as the interface is not changed. • Multiple inheritance • A class may implement several interfaces, but only inherit one class. (No competing implementations).
iterator Example – Iterator Pattern • This piece of client code iterates over any data structure that implements IEnumerator: data structure IEnumerator iter; iter = ...; // get ref to iterator object iter.Reset(); while ( iter.MoveNext() ) MessageBox.Show( iter.Current.ToString() ); View Demo
Polymorphism • Polymorphism: when the same operation is supported across different types. • Example: • classes that inherit some method M from a common base class B and overrides it. • classes that implement a common interface I.
Polymorphism facilitates reusable code! • Example: • This piece of code works on all data structures that implements IEnumerable! • All data structures (Collections) in .NET implement IEnumerator. • All objects in .NET inherit the ToString() method from Object. IEnumerator iter; iter = ...; iter.Reset(); while ( iter.MoveNext() ) MessageBox.Show( iter.Current.ToString() );
In .NET interfaces is used heavily. • IComparable • ICloneable • IDisposable • IEnumerable & IEnumerator • IList • ISerializable • IDBConnection, IDBCommand, IDataReader • etc.
Typical Design in APIs • Specification in the interface. • Implementation in the concrete classes. • Operations with common implementation are implemented in the abstract class (no code duplication). • Operation specific for the different concrete classes are left abstract in the abstract class.
Part 2 • Interface-based programming…
Example: Sorting • Goal: • To write a generic Sort() method as the one found in System.Array
Step 1: Define the interface • Sorting requires a way of comparison of objects: • returns < 0 if this object < obj parameter • returns == 0 if this object = obj parameter • returns > 0 if this object > obj parameter public interface IComparable { int CompareTo(object obj); }
Person Student Step 2: Classes must implement the interface • Objects That are to be sorted must implement IComparable • Example: • sort Student objects on id Also as generic base class interface public class Student : Person, IComparable { private int m_ID; . . . int IComparable.CompareTo(Object obj) { Student other; other = (Student) obj; return this.m_ID – other.m_ID; } }
Step 3: Clients program towards the interface • Sort assumes that the elements in the array a implement IComparable: public class Array { public static void Sort(Array a) { IComparable icobj; for (int i = 0; i < a.Length-1; i++) { for (int j = i+1; j < a.Length; j++) { icobj = (IComparable) a.GetValue(i); if (icobj.CompareTo(a.GetValue(j)) > 0) swap(a, i, j); }//for }//for } }
Step 4: test! • Example: sort an array of Student objects Student[] students; students = new Student[n]; students[0] = new Student("jane doe", 22, 55630); students[1] = new Student("kim lee", 19, 81101); students[2] = new Student("jim bag", 28, 28254); . . . Array.Sort(students); foreach(Student s in students) MessageBox.Show(s.Name); View Code
Summing up • Inheritance is very useful, but… • Only single inheritance is supported, so use it right • Remember class A “is-a“ class B (Principle of Substitution). • Interfaces are useful • A class may implement any number of interfaces • Consider interfaces when class A interacts with several classes B, C, D, … • Goal: • Good design – low coupling - “separation of concerns”. • Exploiting polymorphic programming.
Exercise • Make changes to the Student example, so students are sorted on grade average, on age, on number of courses passed etc.