1 using System; 2 3 namespace FactoryMethod 4 { 5 /// <summary> 6 /// 作者:bzyzhang 7 /// 时间:2016/5/23 20:39:09 8 /// 博客地址:http://www.cnblogs.com/bzyzhang/ 9 /// LeiFeng说明:本代码版权归bzyzhang所有,使用时必须带上bzyzhang博客地址 10 /// </summary> 11 public class LeiFeng 12 { 13 public void Sweep() 14 { 15 Console.WriteLine("扫地"); 16 } 17 18 public void Wash() 19 { 20 Console.WriteLine("洗衣"); 21 } 22 23 public void Cook() 24 { 25 Console.WriteLine("做饭"); 26 } 27 } 28 }
1 using System; 2 3 namespace FactoryMethod 4 { 5 /// <summary> 6 /// 作者:bzyzhang 7 /// 时间:2016/5/23 20:41:36 8 /// 博客地址:http://www.cnblogs.com/bzyzhang/ 9 /// Student说明:本代码版权归bzyzhang所有,使用时必须带上bzyzhang博客地址 10 /// </summary> 11 public class Student:LeiFeng 12 { 13 } 14 }
1 using System; 2 3 namespace FactoryMethod 4 { 5 /// <summary> 6 /// 作者:bzyzhang 7 /// 时间:2016/5/23 20:42:13 8 /// 博客地址:http://www.cnblogs.com/bzyzhang/ 9 /// Volunteer说明:本代码版权归bzyzhang所有,使用时必须带上bzyzhang博客地址 10 /// </summary> 11 public class Volunteer:LeiFeng 12 { 13 } 14 }
1 namespace FactoryMethod 2 { 3 interface IFactory 4 { 5 LeiFeng CreateLeiFeng(); 6 } 7 }
1 using System; 2 3 namespace FactoryMethod 4 { 5 /// <summary> 6 /// 作者:bzyzhang 7 /// 时间:2016/5/23 21:07:16 8 /// 博客地址:http://www.cnblogs.com/bzyzhang/ 9 /// StudentFactory说明:本代码版权归bzyzhang所有,使用时必须带上bzyzhang博客地址 10 /// </summary> 11 public class StudentFactory:IFactory 12 { 13 public LeiFeng CreateLeiFeng() 14 { 15 return new Student(); 16 } 17 } 18 }
1 using System; 2 3 namespace FactoryMethod 4 { 5 /// <summary> 6 /// 作者:bzyzhang 7 /// 时间:2016/5/23 21:08:19 8 /// 博客地址:http://www.cnblogs.com/bzyzhang/ 9 /// VolunteerFactory说明:本代码版权归bzyzhang所有,使用时必须带上bzyzhang博客地址 10 /// </summary> 11 public class VolunteerFactory:IFactory 12 { 13 public LeiFeng CreateLeiFeng() 14 { 15 return new Volunteer(); 16 } 17 } 18 }
1 namespace FactoryMethod 2 { 3 class Program 4 { 5 static void Main(string[] args) 6 { 7 IFactory factory = new StudentFactory(); 8 LeiFeng leiFeng = factory.CreateLeiFeng(); 9 10 leiFeng.Wash(); 11 leiFeng.Sweep(); 12 leiFeng.Cook(); 13 } 14 } 15 }