1 / 18

Interfaces

Interfaces. Reference: Joe Hummel. Objectives.

jerom
Download Presentation

Interfaces

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Interfaces Reference: Joe Hummel

  2. 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. Part 1 • Interfaces…

  4. 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 }

  5. 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. 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

  7. 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. 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. In .NET interfaces is used heavily. • IComparable • ICloneable • IDisposable • IEnumerable & IEnumerator • IList • ISerializable • IDBConnection, IDBCommand, IDataReader • etc.

  10. 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. Part 2 • Interface-based programming…

  12. Example: Sorting • Goal: • To write a generic Sort() method as the one found in System.Array

  13. 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. 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; } }

  15. 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 } }

  16. 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. 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. Exercise • Make changes to the Student example, so students are sorted on grade average, on age, on number of courses passed etc.

More Related