• 策略模式结合简单工厂


       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();            
            }
    

      

     

     

     

      

  • 相关阅读:
    Eureka 注册中心 简单搭建
    Eclipse properties.config.yml 配置文件中文编码问题。Eclipse 配置文件插件解决方案
    OracleDBConsole启动不了
    Codeforces Round #275(Div. 2)-C. Diverse Permutation
    Codeforces Round #275 (Div. 2)-B. Friends and Presents
    Codeforces Round #275 (Div. 2)-A. Counterexample
    NYOJ-106-背包问题
    NYOJ-71-独木舟上的旅行
    NYOJ-91-阶乘之和
    BestCoder Round#15 1002-Instruction
  • 原文地址:https://www.cnblogs.com/lijianhua/p/5735134.html
Copyright © 2020-2023  润新知