策略模式结合简单工厂
2016-08-04 00:38 lijianhua 阅读(179) 评论(0) 编辑 收藏 举报public interface ITurn { string GetUrl(); }
public class NatiuBase:ITurn { public string StartDate { get; set; } public string EndDate { get; set; } public string PDUId { get; set; } public string Type { get; set; } public NatiuBase(DD d) { this.StartDate = d.StartDate; this.EndDate = d.EndDate; this.PDUId = d.PDUId; this.Type = d.Type; } public virtual string GetUrl() { return "我是父类"; } }
public class KDS:NatiuBase { public KDS(DD d): base(d) { } public override string GetUrl() { return string.Format("开始日期:{0},结束日期{1}", StartDate, EndDate); } }
//简单工厂 public class CsFactory { public static ITurn create(DD d) { ITurn iturn = null; switch (d.Type) { case "KDS_ALL": iturn = new KDS(d); break; case "Nat": iturn = new NatiuBase(d); break; } return iturn; } } //参数 public class DD { public string StartDate { get; set; } public string EndDate { get; set; } public string PDUId { get; set; } public string Type { get; set; } public DD(string startDate,string endDate,string pduId,string type) { this.StartDate = startDate; this.EndDate = endDate; this.PDUId = pduId; this.Type = type; } }
static void Main(string[] args) { //Context context = new Context(new KDS("2012-02-12", "2012-09-12", "50000", "kds_all")); //Console.WriteLine(context.GetUrl()); //Context context2 = new Context(new NatiuBase("2012-02-12", "2012-09-12", "50000", "kds_all")); //Console.WriteLine(context2.GetUrl()); DD d = new DD("2016-02-12", "2016-06-12", "500010", "KDS_ALL"); ITurn iturn = CsFactory.create(d); string ss = iturn.GetUrl(); Console.WriteLine(ss); Console.ReadKey(); }