OOP Class12
Interfaces Part2
There are many interfaces in the .net framework. Some that we have been working with are the [ICollection https://msdn.microsoft.com/en-us/library/system.collections.icollection(v=vs.110).aspx] [IList https://msdn.microsoft.com/en-us/library/system.collections.ilist(v=vs.110).aspx] and [IEnumerable https://msdn.microsoft.com/en-us/library/system.collections.ienumerable(v=vs.110).aspx]
These interfaces allow us to use methods in a List and Array.
Here is an example of sorting a list of ints or an int[]
1 int[] ints = new int[] { 5, 4, 2, 3, 1 };
2 foreach (var number in ints)
3 {
4 Console.Write(string.Format("{0}\t", number));
5 }
6 Console.WriteLine();
7
8 Array.Sort(ints);
9 foreach (var number in ints)
10 {
11 Console.Write(string.Format("{0}\t", number));
12 }
13 Console.WriteLine();
14
15 ints = ints.Reverse().ToArray();
16 foreach (var number in ints)
17 {
18 Console.Write(string.Format("{0}\t", number));
19 }
20 Console.WriteLine();
21
22 //mess it up again
23 ints = new int[] { 5, 4, 2, 3, 1 };
24
25 ints = ints.OrderBy(i => i).ToArray();
26 foreach (var number in ints)
27 {
28 Console.Write(string.Format("{0}\t", number));
29 }
30 Console.WriteLine();
31
32 //mess it up again
33 ints = new int[] { 5, 4, 2, 3, 1 };
34
35 //last way slightly uses agregate function to Order array by int, turns array into list so we can call foreach
36 ints.OrderBy(i => i).ToList().ForEach(ii=>Console.Write(string.Format("{0}\t", ii)));
37 Console.WriteLine();
now with List
1
Now lets sort some dogs
1 class Dog : IComparable
2 {
3 public string Name;
4 protected int age;
5
6 public int Age { get { return age; } set { age = value; } }
7
8 public int Weight;
9 public string BarkSound;
10
11 public Dog()
12 {
13 this.Name = "fido";
14 this.BarkSound = "woof!";
15 this.Weight = 1;
16 }
17
18 public string About()
19 {
20 return string.Format("Hello my name is {0}. I'm {1} years old. I weigh {2} lbs", this.Name, this.Age, this.Weight);
21 }
22
23
24
25 public int CompareTo(object obj)
26 {
27 if (obj is Dog)
28 {
29 if (this.Age > ((Dog)obj).Age) return 1;
30 }
31 return 0;
32 }
33 }
and finally override some operators
1 public static int Compare(Dog left, Dog right)
2 {
3 if (object.ReferenceEquals(left, right))
4 {
5 return 0;
6 }
7 if (object.ReferenceEquals(left, null))
8 {
9 return -1;
10 }
11 return left.CompareTo(right);
12 }
13
14 public static bool operator <(Dog left, Dog right)
15 {
16 return (Compare(left, right) < 0);
17 }
18 public static bool operator >(Dog left, Dog right)
19 {
20 return (Compare(left, right) > 0);
21 }
Strategy Pattern
Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it.
http://www.dofactory.com/Patterns/PatternStrategy.aspx
Create Characters class and weapons class that uses the strategy pattern
http://iam.colum.edu/oop/classsource/ConsoleApplicationCharacters.zip
Use Case
http://en.wikipedia.org/wiki/Use_case
Diagrams
http://en.wikipedia.org/wiki/Use_case_diagram
Windows Course App Use Case
Actors
- particiant outside of the system
Activity
- something an actor does
Version 1 breif use case
Actor Admin Course #Add a course to the system #Delete a course to the system Student #Add a student to the system #Delete a student to the system #Student add a course #Student remove a course
Home work
Toastable project in moodle
Make one of your classes sortable by implementing IComparable