1 public class FactoryPattern
2 {
3 public interface IWorkLocation
4 {
5 string WorkingFrom();
6 }
7
8 public class Developer : Person, IWorkLocation
9 {
10 public Developer( string name, string designation )
11 {
12 this.Name = name;
13 this.Designation = designation;
14 }
15
16 public string WorkingFrom()
17 {
18 return "Name : " + this.Name + ", Work Location : Home";
19 }
20 }
21
22 public class Tester : Person, IWorkLocation
23 {
24 public Tester( string name, string designation )
25 {
26 this.Name = name;
27 this.Designation = designation;
28 }
29
30 public string WorkingFrom()
31 {
32 return "Name : " + this.Name + ", Work Location : Office";
33 }
34 }
35
36 public class Person : IWorkLocation
37 {
38 public string Designation { get; set; }
39 public string Name { get; set; }
40
41 public Person()
42 {
43 Name = "Base Person";
44 Designation = "";
45 }
46
47 public Person( string name, string designation )
48 {
49 Designation = designation;
50 Name = name;
51 }
52
53 public string WorkingFrom()
54 {
55 return "Name : " + this.Name + ", Work Location : Not Available";
56 }
57 }
58
59 //Creator
60 public class Creator
61 {
62 public IWorkLocation FactoryMethod( Person per )
63 {
64 if ( per.Designation.Equals( "Developer" ) )
65 return new Developer( per.Name, per.Designation );
66 else if ( per.Designation.Equals( "Tester" ) )
67 return new Tester( per.Name, per.Designation );
68 else
69 return new Person();
70 }
71 }
72
73 static void Main()
74 {
75 Creator crtr = new Creator();
76 var person = crtr.FactoryMethod( new Person( "Person1", "Developer" ) );
77 Console.WriteLine( person.WorkingFrom() );
78 //Output: Name : Person1, Work Location : Home
79
80 person = crtr.FactoryMethod( new Person( "Person2", "Tester" ) );
81 Console.WriteLine( person.WorkingFrom() );
82 //Output: Name : Person2, Work Location : Office
83
84 person = crtr.FactoryMethod( new Person() );
85 Console.WriteLine( person.WorkingFrom() );
86 //Output: Name : Base Person, Work Location : Not Available
87
88 Console.ReadLine();
89 }
90 }