惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

K
Kaspersky official blog
Engineering at Meta
Engineering at Meta
D
DataBreaches.Net
Stack Overflow Blog
Stack Overflow Blog
Microsoft Security Blog
Microsoft Security Blog
Y
Y Combinator Blog
B
Blog RSS Feed
GbyAI
GbyAI
P
Proofpoint News Feed
aimingoo的专栏
aimingoo的专栏
MyScale Blog
MyScale Blog
D
Docker
阮一峰的网络日志
阮一峰的网络日志
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Recorded Future
Recorded Future
美团技术团队
The Register - Security
The Register - Security
V
Visual Studio Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
T
Tailwind CSS Blog
爱范儿
爱范儿
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
T
The Blog of Author Tim Ferriss
博客园 - 司徒正美
量子位
B
Blog
F
Fortinet All Blogs
Martin Fowler
Martin Fowler
博客园 - 【当耐特】
MongoDB | Blog
MongoDB | Blog
A
About on SuperTechFans
I
InfoQ
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
有赞技术团队
有赞技术团队
雷峰网
雷峰网
大猫的无限游戏
大猫的无限游戏
J
Java Code Geeks
L
LangChain Blog
Latest news
Latest news
S
SegmentFault 最新的问题
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Blog — PlanetScale
Blog — PlanetScale
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Cisco Talos Blog
Cisco Talos Blog
F
Full Disclosure
C
Cisco Blogs
D
Darknet – Hacking Tools, Hacker News & Cyber Security
W
WeLiveSecurity
T
Tenable Blog
T
Tor Project blog

博客园 - 广阔之海

Halcon的三角函数 Halcon轮廓插值方法 C# 控件选项变化事件处理 深度学习执行速度不稳定的解决方法 Halcon - 深度学习 - 目标分类 Halcon 画一个时钟 Halcon 解方程(solve_matrix) C# 使用Serilog日志框架 Halcon 生成标定板 Halcon 中的形态学 Halcon的提取中心线算法 C#中Task的用法 Halcon 通过坐标轴过滤点云数据 Halcon图像投影映射 C# 获取MySql的数据库结构信息 C# HttpClient的使用方法总结 MySql字符集导致特殊字符保存出错问题处理 Asp.Net Core 动态生成WebApi Asp.Net Core WebApi中集成Jwt认证
C# 消灭switch的面向映射编程
广阔之海 · 2022-09-12 · via 博客园 - 广阔之海

原有switch写法,所有分支集中在一个方法中,使得方法过于冗长

    class Calculator01
    {
        public double Num1 { get; set; }
        public double Num2 { get; set; }

        public double Run(string operate)
        {
            switch(operate)
            {
                case "+":
                    return Num1 + Num2;
                case "-":
                    return Num1 - Num2;
                case "*":
                    return Num1 * Num2;
                case "/":
                    if (Num2 == 0)
                    {
                        throw new Exception("除数不能为0");
                    }

                    return Num1 / Num2;
                default:
                    throw new Exception("未知的操作符");
            }
        }
    }

面向映射写法,将分支剥离为独立方法,避免主要方法过于冗长

    class Calculator02
    {
        // 内置委托Action、Func、Predicate的区别:
        // Action可以有多个参数但不可有返回值;
        // Func可以有多个参数且最后一个参数固定是返回值;
        // Predicate只能有1个参数且返回布尔值。  
        private Dictionary<string, Func<double, double, double>> map = new Dictionary<string, Func<double, double, double>>();

        public double Num1 { get; set; }
        public double Num2 { get; set; }        

        public Calculator02()
        {          
            // 注册操作符到方法的映射
            map.Add("+", Add);
            map.Add("-", Sub);
            map.Add("*", Mul);
            map.Add("/", Div);
        }

        public double Run(string operate)
        {
            if (!map.ContainsKey(operate))
                throw new Exception("未知的操作符");

            return map[operate](Num1, Num2);
        }

        private double Add(double num1, double num2)
        {
            return Num1 + Num2;
        }

        private double Sub(double num1, double num2)
        {
            return Num1 - Num2;
        }

        private double Mul(double num1, double num2)
        {
            return Num1 * Num2;
        }

        private double Div(double num1, double num2)
        {
            if (Num2 == 0)
            {
                throw new Exception("除数不能为0");
            }

            return Num1 / Num2;
        }
    }