设计模式——简单工厂模式

文章目录

面向对象------如:活字印刷术 封装、继承、多态

通过面向对象的三大特性:封装、继承、多态 降低程序耦合度。使得程序易维护、易扩展、易复用

松耦合------业务逻辑与界面逻辑分离------简单工厂模式

例如构建一个计算器功能:

1.根据计算方式,只编译该类型与方法,而不是编译所有,易于维护。例如:使用加法运算只编译加法运算而不是所有运算。

2.添加其他运算方式。例如添加次方运算,不用给工程师整个代码,发生其他计算方式代码误操作、更改、泄密等其他情况。

以下分别举例紧耦合案列、松耦合案例:

紧耦合举例

所有方法汇总在一起,导致计算时需要全部加载

csharp 复制代码
       public void TryCal()
       {
            try
            {
                //输入数字A:
                string strNumberA = numberA.Text;
                //请选择运算符号(+、-、*、/)
                string strOperate = numberO.Text;
                //请输入数字B:
                string strNumberB = numberB.Text;
                numberR.Text = operation.GetResult(Convert.ToDouble(strNumberA), Convert.ToDouble(strNumberB), strOperate).ToString();
            }
            catch (Exception er)
            {
                LBError.Content = er.Message.ToString();
            }

        }
        public class operation
        {
            public static double GetResult(double A, double B, string operatesign)
            {
                double result = 0d;
                switch (operatesign)
                {
                    case "+":
                        result = A + B;
                        break;
                    case "-":
                        result = A - B;
                        break;
                    case "*":
                        result = A * B;
                        break;
                    case "/":
                        result = A / B;
                        break;
                    default:
                        break;
                }
                return result;
            }
        }

        private void numberR_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key==Key.Enter)
            {
                TryCal();
            }
        }

松耦合举例------简单工厂模式

利用简单工厂模式,根据条件实例化对象,避免增加不必要的实例化对象。

csharp 复制代码
       public void TryCall_Factory()
        {
            try
            {
               operationL opel = OperationFactory.createOperate(numberO.Text);
                opel.NumberA = Convert.ToDouble(numberA.Text);
                opel.NumberB = Convert.ToDouble(numberB.Text);
                numberR.Text = opel.Getresult().ToString();
            }
            catch (Exception er)
            {
                LBError.Content = er.Message.ToString();

            }
        }
        public class OperationFactory
        {
            public static operationL createOperate(string operatestr)
            {
                operationL operation = null;
                switch (operatestr)
                {
                    case "+":
                        operation = new OperationAdd();
                        break;
                    case "-":
                        operation = new OperationSub();
                        break;
                    case "*":
                        operation = new OperationMul();
                        break;
                    case "/":
                        operation = new OperationDiv();
                        break;
                }
                return operation;
            }
        }
        public class operationL
        {
            private Double _numberA;

            public Double NumberA
            {
                get { return _numberA; }
                set { _numberA = value; }
            }

            private Double _numberB;

            public Double NumberB
            {
                get { return _numberB; }
                set { _numberB = value; }
            }
            public virtual Double Getresult()
            {
                double result = 0d;
                return result;
            }
        }
        public class OperationAdd:operationL
        {
            public override double Getresult()
            {
                double result = 0d;
                result = NumberA + NumberB;
                return result;
            }
        }
        public class OperationSub : operationL
        {
            public override double Getresult()
            {
                double result = 0d;
                result = NumberA - NumberB;
                return result;
            }
        }
        public class OperationMul : operationL
        {
            public override double Getresult()
            {
                double result = 0d;
                result = NumberA* NumberB;
                return result;
            }
        }
        public class OperationDiv : operationL
        {
            public override double Getresult()
            {
                double result = 0d;
                if (NumberB == 0)
                    throw new Exception("除数不能为0");
                result = NumberA /NumberB;
                return result;
            }
        }
相关推荐
charlie1145141911 小时前
精读C++20设计模式——结构型设计模式:外观模式
c++·学习·设计模式·c++20·外观模式
拧之12 小时前
✅设计模式笔记
笔记·单例模式·设计模式
haoly198913 小时前
数据结构与算法篇--语义智能指针设计模式
数据结构·设计模式
青草地溪水旁13 小时前
设计模式(C++)详解——状态模式(State)(1)
c++·设计模式·状态模式
青草地溪水旁15 小时前
设计模式(C++)详解——策略模式(1)
c++·设计模式·策略模式
secondyoung15 小时前
Markdown转换为Word:Pandoc模板使用指南
开发语言·经验分享·笔记·c#·编辑器·word·markdown
o0向阳而生0o15 小时前
105、23种设计模式之策略模式(14/23)
设计模式·策略模式
charlie11451419118 小时前
精读C++设计模式20 —— 结构型设计模式:桥接模式
开发语言·c++·学习·设计模式·桥接模式·c++23·概论
da_vinci_x19 小时前
设计稿秒出“热力图”:AI预测式可用性测试工作流,上线前洞察用户行为
前端·人工智能·ui·设计模式·可用性测试·ux·设计师
andyguo20 小时前
AI模型测评平台工程化实战十二讲(第五讲:大模型测评分享功能:安全、高效的结果展示与协作)
人工智能·安全·c#