Presentation is loading. Please wait.

Presentation is loading. Please wait.

Interfaces Reference: Joe Hummel. 2 UCN Technology: Computer Science 2012 Objectives “Good class design starts with good application design — how many.

Similar presentations


Presentation on theme: "Interfaces Reference: Joe Hummel. 2 UCN Technology: Computer Science 2012 Objectives “Good class design starts with good application design — how many."— Presentation transcript:

1 Interfaces Reference: Joe Hummel

2 2 UCN Technology: Computer Science 2012 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

3 3 UCN Technology: Computer Science 2012 Part 1 Interfaces…

4 4 UCN Technology: Computer Science 2012 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) public interface IEnumerator { void Reset(); // reset iterator to beginning bool MoveNext(); // advance to next element object Current { get; } // retrieve current element } Also as generic

5 5 UCN Technology: Computer Science 2012 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).

6 6 UCN Technology: Computer Science 2012 Example – Iterator Pattern This piece of client code iterates over any data structure that implements IEnumerator: IEnumerator iter; iter =...; // get ref to iterator object iter.Reset(); while ( iter.MoveNext() ) MessageBox.Show( iter.Current.ToString() ); iterator data structure View Demo

7 7 UCN Technology: Computer Science 2012 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.

8 8 UCN Technology: Computer Science 2012 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() );

9 9 UCN Technology: Computer Science 2012 In.NET interfaces is used heavily. IComparable ICloneable IDisposable IEnumerable & IEnumerator IList ISerializable IDBConnection, IDBCommand, IDataReader etc.

10 10 UCN Technology: Computer Science 2012 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.

11 11 UCN Technology: Computer Science 2012 Part 2 Interface-based programming…

12 12 UCN Technology: Computer Science 2012 Example: Sorting Goal: –To write a generic Sort() method as the one found in System.Array

13 13 UCN Technology: Computer Science 2012 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); }

14 14 UCN Technology: Computer Science 2012 Step 2: Classes must implement the interface Objects That are to be sorted must implement IComparable Example: –sort Student objects on id 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; } base classinterface Student Person Also as generic

15 15 UCN Technology: Computer Science 2012 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 }

16 16 UCN Technology: Computer Science 2012 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

17 17 UCN Technology: Computer Science 2012 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.

18 18 UCN Technology: Computer Science 2012 Exercise Make changes to the Student example, so students are sorted on grade average, on age, on number of courses passed etc.


Download ppt "Interfaces Reference: Joe Hummel. 2 UCN Technology: Computer Science 2012 Objectives “Good class design starts with good application design — how many."

Similar presentations


Ads by Google