Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
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 UCN Technology: Computer Science 2012 2 Part 1 • Interfaces… UCN Technology: Computer Science 2012 3 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 } UCN Technology: Computer Science 2012 4 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). UCN Technology: Computer Science 2012 5 Example – Iterator Pattern • This piece of client code iterates over any data structure that implements IEnumerator: iterator IEnumerator iter; iter = ...; // get ref to iterator object data structure iter.Reset(); while ( iter.MoveNext() ) MessageBox.Show( iter.Current.ToString() ); View Demo UCN Technology: Computer Science 2012 6 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. UCN Technology: Computer Science 2012 7 • Polymorphism facilitates reusable code! • Example: – This piece of code works on all data structures that implements IEnumerable! IEnumerator iter = ...; iter; iter.Reset(); while ( iter.MoveNext() ) MessageBox.Show( iter.Current.ToString() ); • • All data structures (Collections) in .NET implement IEnumerator. All objects in .NET inherit the ToString() method from Object. UCN Technology: Computer Science 2012 8 In .NET interfaces is used heavily. • • • • • • • • IComparable ICloneable IDisposable IEnumerable & IEnumerator IList ISerializable IDBConnection, IDBCommand, IDataReader etc. UCN Technology: Computer Science 2012 9 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. UCN Technology: Computer Science 2012 10 Part 2 • Interface-based programming… UCN Technology: Computer Science 2012 11 Example: Sorting • Goal: – To write a generic Sort() method as the one found in System.Array UCN Technology: Computer Science 2012 12 Step 1: Define the interface • Sorting requires a way of comparison of objects: public interface IComparable { int CompareTo(object obj); } • returns < 0 • returns == 0 • returns > 0 if this object < obj parameter if this object = obj parameter if this object > obj parameter UCN Technology: Computer Science 2012 13 Step 2: Classes must implement the interface • Objects That are to be sorted must implement IComparable Also as • Example: generic – sort Student objects on id base class interface public class Student : Person, IComparable { private int m_ID; . . . Person Student int IComparable.CompareTo(Object obj) { Student other; other = (Student) obj; return this.m_ID – other.m_ID; } } UCN Technology: Computer Science 2012 14 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 } } UCN Technology: Computer Science 2012 15 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 UCN Technology: Computer Science 2012 16 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. UCN Technology: Computer Science 2012 17 Exercise • Make changes to the Student example, so students are sorted on grade average, on age, on number of courses passed etc. UCN Technology: Computer Science 2012 18