Skip to content

Commit ec965ae

Browse files
authored
Merge pull request #4 from AmdHamdani/master
Adding NullObject pattern
2 parents 605d974 + c349564 commit ec965ae

File tree

5 files changed

+62
-0
lines changed

5 files changed

+62
-0
lines changed

NullObject/AbstractBook.cs

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
using System;
2+
3+
public abstract class AbstractCustomer {
4+
protected string title;
5+
6+
public abstract string GetTitle();
7+
}

NullObject/Book.cs

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System;
2+
3+
public class Book : AbstractCustomer {
4+
5+
public Book (String title) {
6+
this.title = title;
7+
}
8+
9+
public override string GetTitle() {
10+
return title;
11+
}
12+
}

NullObject/BookFactory.cs

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System;
2+
3+
public class BookFactory {
4+
5+
public static string[] books = {"Book1", "Book2", "Book3" };
6+
7+
public static AbstractCustomer GetBook(string title) {
8+
for(int i = 0; i < books.Length; i++)
9+
if(books[i] == title)
10+
return new Book(title);
11+
return new NullBook();
12+
}
13+
14+
}

NullObject/Client.cs

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System;
2+
3+
public class NullPatternDemo {
4+
5+
static void Main(string[] args) {
6+
7+
AbstractCustomer c1 = BookFactory.GetBook("Book1");
8+
AbstractCustomer c2 = BookFactory.GetBook("Book5");
9+
AbstractCustomer c3 = BookFactory.GetBook("Book2");
10+
AbstractCustomer c4 = BookFactory.GetBook("Book3");
11+
AbstractCustomer c5 = BookFactory.GetBook("Book9");
12+
13+
Console.WriteLine(c1.GetTitle());
14+
Console.WriteLine(c2.GetTitle());
15+
Console.WriteLine(c3.GetTitle());
16+
Console.WriteLine(c4.GetTitle());
17+
Console.WriteLine(c5.GetTitle());
18+
}
19+
20+
}

NullObject/NullBook.cs

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using System;
2+
3+
class NullBook : AbstractCustomer {
4+
5+
public override string GetTitle() {
6+
return "Not avaible in Customer Database";
7+
}
8+
9+
}

0 commit comments

Comments
 (0)