聊聊 if else 那些事

从这周起,公司技术部每周五组织一次内部的技术分享,日常工作中,发现大家对if掌握的不是很好,今天先来聊聊if。

一、场景

简历的价格是根据专业的父Id设定的,下面根据简历Id获取简历的价格。

    /// <summary>
    /// 专业
    /// </summary>
    public class Subject
    {
        public int Id { get; set; }
        public int Pid { get; set; }
    }
    /// <summary>
    /// 简历
    /// </summary>
    public class Resume
    {
        public int Id { get; set; }
        public int SubjectId { get; set; }
    }

 

二、if、三元运算符、Null合并运算符

下面的方法用来获取专业实体,如果出现专业数据异常,给一个默认的专业,这里主要为了说明三元运算符和Null合并运算符的用法

    public interface ISubjectService
    {
        Subject GetSubjectById(int id);
    }
    public interface ISubjectDao
    {
        Subject GetSubjectById(int id);
    }
    public class SubjectService : ISubjectService
    {
        private readonly ISubjectDao _subjectDao;

        /// <summary>
        /// 1、if判断
        /// </summary>
        /// <param name="subjectId"></param>
        /// <returns></returns>
        public Subject GetSubjectById(int subjectId)
        {

            var subject = _subjectDao.GetSubjectById(subjectId);
            if (subject == null)
                subject = new Subject() {Id = 1001, Pid = 10};

            return subject;
        }

        /// <summary>
        /// 2、三元运算符
        /// </summary>
        /// <param name="subjectId"></param>
        /// <returns></returns>
        public Subject GetSubjectById(int subjectId)
        {

            var subject = _subjectDao.GetSubjectById(subjectId) != null
                ? _subjectDao.GetSubjectById(subjectId)
                : new Subject() { Id = 1001, Pid = 10 };


            return subject;
        }


        /// <summary>
        /// 3、null 合并运算符
        /// </summary>
        /// <param name="subjectId"></param>
        /// <returns></returns>
        public Subject GetSubjectById(int subjectId)
        {
            return _subjectDao.GetSubjectById(subjectId) ?? new Subject() { Id = 1001, Pid = 10 };
        }
    }

 

三、if else和switch

根据简历Id计算简历价格

 public interface IResumeService
    {
        Resume GetResumeById(int id);
        int GetResumePrice(int id);
    }
    public interface IResumeDao
    {
        Resume GetResumeById(int id);
    }
    public class ResumeService 
    {
        private readonly IResumeDao _resumeDao;
        private readonly ISubjectService _SubjectService;
        /// <summary>
        /// 1、if else
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int GetResumePrice(int id)
        {
            var resume = _resumeDao.GetResumeById(id);
            var subject = _SubjectService.GetSubjectById(resume.SubjectId);
            if (subject.Pid == 1)
            {
                return 10;
            }
            else if (subject.Pid == 2)
            {
                return 20;
            }
            else if (subject.Pid == 3)
            {
                return 30;
            }

            return 10;
        }
/// <summary>
        /// 2、switch
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int GetResumePrice(int id)
        {
            var resume = _resumeDao.GetResumeById(id);
            var subject = _SubjectService.GetSubjectById(resume.SubjectId);
            switch (subject.Pid)
            {
                case 1:
                    return 10;
                case 2:
                    return 20;
                case 3:
                    return 30;
                default:
                    return 10;
            }
        }
    }

 

四、if else和卫语句

什么是卫语句?

许多程序员觉得函数应该只有一个出口 (return), 结果导致函数中的条件逻辑 (Conditional Logic) 本来完全可以终止下面的代码继续执行 (因为没有必要), 结果却只在函数最后 return, 使人难以看清程序的执行路径.“使用卫语句取代嵌套条件”用来解决这个问题, 它能带给代码可读性的提高, 还有性能上一点点的优化。

        public int GetResumePrice(int id)
        {
            var resume = _resumeDao.GetResumeById(id);
            var subject = _SubjectService.GetSubjectById(resume.SubjectId);
            if (subject.Pid == 1) return 10;
            if (subject.Pid == 2) return 20;
            if (subject.Pid == 3) return 30;

            return 10;
        }

 

五、if else和策略模式

/// <summary>
    /// 抽象一个计算简历价格的接口
    /// </summary>
    public interface IResumePrice
    {
        int GetResumePrice();
    }
    /// <summary>
    /// 专业One的简历价格
    /// </summary>
    public class OneSubjectPrice : IResumePrice
    {
        public int GetResumePrice()
        {
            return 10;
        }
    }
    /// <summary>
    /// 专业Two的简历价格
    /// </summary>
    public class TwoSubjectPrice : IResumePrice
    {
        public int GetResumePrice()
        {
            return 20;
        }
    }
    /// <summary>
    /// 专业Three的简历价格
    /// </summary>
    public class ThreeSubjectPrice : IResumePrice
    {
        public int GetResumePrice()
        {
            return 30;
        }
    }
    public class ResumeService
    {
        private readonly IResumePrice _resumePrice;
        /// <summary>
        /// 实例化IResumePrice
        /// </summary>
        /// <param name="resumePrice"></param>
        public ResumeService(IResumePrice resumePrice)
        {
            _resumePrice = resumePrice;
        }
        /// <summary>
        /// 技术简历价格
        /// </summary>
        /// <param name="subjectPrice"></param>
        /// <returns></returns>
        public int GetResumePrice(string subjectPrice)
        {
            return _resumePrice.GetResumePrice();
        }
    }

 

六、if else和状态模式

使用上面定义的ResumePrice的接口和实现,状态模式需要用到反射

  public class ResumeService
    {
        /// <summary>
        /// 专业1价格计算类
        /// </summary>
        public static String OneSubjectPrice = "OneSubjectPrice";
        /// <summary>
        /// 专业2价格计算类
        /// </summary>
        public static String TwoSubjectPrice = "TwoSubjectPrice";
        /// <summary>
        /// 专业3价格计算类
        /// </summary>
        public static String ThreeSubjectPrice = "ThreeSubjectPrice";

        /// <summary>
        /// 技术简历价格
        /// 利用了反射的伪代码
        /// </summary>
        /// <param name="subjectPriceType"></param>
        /// <returns></returns>
        public int GetResumePrice(string subjectPriceType)
        {
            var dll = Assembly.LoadFrom("assemblyName");
            var type = (IResumePrice)dll.GetType(subjectPriceType, true);
            return type.GetResumePrice();
        }
    }

    public class Main
    {
        public void CalPrice()
        {
            var resumeService = new ResumeService();
            var price1 = resumeService.GetResumePrice(ResumeService.OneSubjectPrice);
            var price2 = resumeService.GetResumePrice(ResumeService.TwoSubjectPrice);
            var price3 = resumeService.GetResumePrice(ResumeService.ThreeSubjectPrice);

        }
    }

 

posted @ 2015-04-10 11:04  青羽  阅读(896)  评论(0编辑  收藏  举报