1 abstract class LeiFeng 2 { 3 public abstract void Sweep(); 4 public abstract void Wash(); 5 public abstract void BuyRice(); 6 } 7 8 class Undergraduate : LeiFeng 9 { 10 public override void Sweep() 11 { 12 Console.WriteLine("在校大学生,扫地"); 13 } 14 15 public override void Wash() 16 { 17 Console.WriteLine("在校大学生,洗衣"); 18 } 19 20 public override void BuyRice() 21 { 22 Console.WriteLine("在校大学生,买米"); 23 } 24 } 25 26 class Volunteer : LeiFeng 27 { 28 public override void Sweep() 29 { 30 Console.WriteLine("志愿者,扫地"); 31 } 32 33 public override void Wash() 34 { 35 Console.WriteLine("志愿者,洗衣"); 36 } 37 38 public override void BuyRice() 39 { 40 Console.WriteLine("志愿者,买米"); 41 } 42 } 43 44 interface IFactory 45 { 46 LeiFeng CreateLeiFeng(); 47 } 48 49 class UndergraduateFactory : IFactory 50 { 51 public LeiFeng CreateLeiFeng() 52 { 53 return new Undergraduate(); 54 } 55 } 56 57 class VolunteerFactory : IFactory 58 { 59 public LeiFeng CreateLeiFeng() 60 { 61 return new Volunteer(); 62 } 63 }
转自《大话设计模式》