Session 07: C# Design Patterns Design Patterns Composite Pattern Iterator Pattern Interfaces in the libraries FEN 2013-04-011AK - IT: Softwarekonstruktion.

Download Session 07: C# Design Patterns Design Patterns Composite Pattern Iterator Pattern Interfaces in the libraries FEN 2013-04-011AK - IT: Softwarekonstruktion.

Post on 18-Jan-2016

213 views

Category:

Documents

0 download

TRANSCRIPT

CSharpOOP-4

Session 07:C# Design PatternsDesign PatternsComposite PatternIterator PatternInterfaces in the libraries

FEN 2013-04-011AK - IT: Softwarekonstruktion1FEN 2013-04-01AK - IT: Softwarekonstruktion2Example: Composite-pattern

Composite: Graphical editor, Word processor, Inventories etc..(What will a Show()-method look like on Shape, Circle, Picture etc.)FEN 2013-04-01AK - IT: Softwarekonstruktion3public abstract class Shape {private int id;private string color;private int xpos;private int ypos;public void MoveTo(Position newPos){ // PRE none // POST pos'=newPos }

public abstract void Show();// PRE none // POST the shape is displayed

public abstract void Hide(); // PRE none // POST the shape is hidden public abstract float Area();// PRE none// POST Computes the area with 4 digits.}//end ShapeFEN 2013-04-01AK - IT: Softwarekonstruktion4public class Circle: Shape{ private int r; //radius public void Show(){ //PRE none //POST the circle is drawn //Must be implemented using appropriate //graphical routines } public void Hide(){ //PRE none //POST the circle is hidden //Must be implemented using appropriate //graphical routines } // More operations}//end CircleFEN 2013-04-01AK - IT: Softwarekonstruktion5public class Picture : Shape{ List pictList; // operations for adding, deleting etc. shapes

public void Show(){ //PRE none //POST The composite shape is drawn for(Shape s : pictList)s.Show(); } } public float Area() { float result=0; for(int i=0;i 0 if this object > obj parameterreturns < 0 if this object < obj parameter

public interface IComparable{ int CompareTo(T obj);}FEN 2013-04-0118AK - IT: SoftwarekonstruktionStep 2: Classes must implement the interfaceObjects That are to be sorted must implement IComparable>

Example:sort Student objects on idpublic class Student : Icomparable{ private int m_ID; . . .

int IComparable.CompareTo(Student other) { return this.m_ID other.m_ID; }}interfaceStudenttypeFEN 2013-04-0119AK - IT: Softwarekonstruktion8. InterfacesJanuary 2003Denmark .NET Workshop 2003 Joe Hummel19Recall from earlier lecture that Student class inherited from Person classStep 3: Clients program towards the interfaceSort 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 }

}FEN 2013-04-0120AK - IT: Softwarekonstruktion8. InterfacesJanuary 2003Denmark .NET Workshop 2003 Joe Hummel20Not a very efficient sort, but you get the idea this method will sort any array of objects, as long as those objects implement IComparable.Step 4: test!Example: sort an array of Student objectsView Code

FEN 2013-04-0121AK - IT: SoftwarekonstruktionFEN 2013-04-01AK - IT: Softwarekonstruktion22ExercisesExercise 5 on Session07.docx.

Recommended

View more >