File tree 5 files changed +62
-0
lines changed
5 files changed +62
-0
lines changed Original file line number Diff line number Diff line change
1
+ using System ;
2
+
3
+ public abstract class AbstractCustomer {
4
+ protected string title ;
5
+
6
+ public abstract string GetTitle ( ) ;
7
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
1
+ using System ;
2
+
3
+ class NullBook : AbstractCustomer {
4
+
5
+ public override string GetTitle ( ) {
6
+ return "Not avaible in Customer Database" ;
7
+ }
8
+
9
+ }
You can’t perform that action at this time.
0 commit comments