Post on 18-Dec-2015
transcript
2UCN 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
4UCN 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
5UCN 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).
6UCN 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
datastructure
View Demo
7UCN 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.
8UCN 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() );
9UCN Technology: Computer Science 2012
In .NET interfaces is used heavily.
• IComparable• ICloneable• IDisposable• IEnumerable & IEnumerator• IList• ISerializable• IDBConnection, IDBCommand, IDataReader• etc.
10UCN 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.
12UCN Technology: Computer Science 2012
Example: Sorting
• Goal: – To write a generic Sort() method as the one found in
System.Array
13UCN 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);}
14UCN 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 class interface
Student
Person
Also as generic
15UCN 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 }//for }
}
16UCN 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
17UCN 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.