标签:style blog http color ar strong sp div 2014
工厂方法模式:
/// <summary> /// 雷锋 /// </summary> public class LeiFeng { public void Wash() { Console.WriteLine("洗衣"); } public void Sweep() { Console.WriteLine("扫地"); } } /// <summary> /// 大学生 /// </summary> public class Undergraduate : LeiFeng { } /// <summary> /// 社区志愿者 /// </summary> public class CommunityVolunteer : LeiFeng { } /// <summary> /// 雷锋工厂 /// </summary> interface IFactory { LeiFeng CreateLeiFeng(); } /// <summary> /// 大学生雷锋工厂 /// </summary> public class UndergraduateFactory : IFactory { public LeiFeng CreateLeiFeng() { return new Undergraduate(); } } /// <summary> /// 社区志愿者雷锋工厂 /// </summary> public class CommunityVolunteerFactory : IFactory { public LeiFeng CreateLeiFeng() { return new CommunityVolunteer(); } }
客户端调用
IFactory factory = new UndergraduateFactory(); LeiFeng leifeng = factory.CreateLeiFeng(); leifeng.Sweep(); leifeng.Wash(); factory = new CommunityVolunteerFactory(); leifeng = factory.CreateLeiFeng(); leifeng.Sweep(); leifeng.Wash();
结果
标签:style blog http color ar strong sp div 2014
原文地址:http://www.cnblogs.com/wzq806341010/p/4013521.html