《大话设计模式》--简单工厂模式SimpleFactory(1)

news/2024/7/7 14:58:27

 

上图为UML图,简单工厂模式解决对象的创建问题。

 

一、创建一个Operation基类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DesignModeDemo
{
    public class Operation
    {

        public double Num1
        {
            get;
            set;
        }

        public double Num2
        {
            get;
            set;
        }

        public virtual double Operate()
        {
            double result = 0d;
            return result;
        }
    }
}


二、创建加、减、乘、除的子类,继承基数,重写Operate()方法。实现多态。注意:业务操作类对用户来说,不需要了解其业务实现内容机制,故业务类都是内部类,封装。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DesignModeDemo
{
    class OperationAdd : Operation
    {
        public override double Operate()
        {
            double result = 0d;
            result = Num1 + Num2;
            return result;
        }
    }
}


 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DesignModeDemo
{
    class OperationSubtract:Operation
    {
        public override double Operate()
        {
            double result = 0d;
            result = Num1 - Num2;
            return result;
        }
    }
}


 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DesignModeDemo
{
    class OperationMultiply:Operation
    {
        public override double Operate()
        {
            double result = 0d;
            result = Num1 * Num2;
            return result;
        }
    }
}


 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DesignModeDemo
{
    class OperationDivide:Operation
    {
        public override double Operate()
        {
            double result = 0d;
            if (Num2 != 0)
            {
                result = Num1 / Num2;
            }
            return result;
        }
    }
}


三、简单运算工厂类,根据运算符,实例化指定的业务类。注意这里使用静态方法。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DesignModeDemo
{
    public class OperationFactory
    {
        public static Operation CreateOperation(string operation)
        {
            Operation ope = null;            

            switch (operation)
            { 
                case "+":
                    ope = new OperationAdd();                    
                    break;
                case "-":
                    ope = new OperationSubtract();
                    break;
                case "*":
                    ope = new OperationMultiply();
                    break;
                case "/":
                    ope = new OperationDivide();
                    break;
            }
            return ope;
        }
    }
}

 

            Console.WriteLine("请输入数字A:");
            string num1 = Console.ReadLine();
            Console.WriteLine("请选择+,-,*,/");
            string ope = Console.ReadLine();
            Console.WriteLine("请输入数字B:");
            string num2 = Console.ReadLine();
            Operation operation = OperationFactory.CreateOperation(ope);
            operation.Num1 = double.Parse(num1);
            operation.Num2 = double.Parse(num2);
            Console.WriteLine(string.Format("结果是:{0}", operation.Operate()));



http://www.niftyadmin.cn/n/4076663.html

相关文章

jsp注释问题

2019独角兽企业重金招聘Python工程师标准>>> jsp常用注释包含三种&#xff1a; HTML注释&#xff08;输出注释&#xff09;&#xff1a; 指在客户端查看源代码时能看见注释。例如&#xff0c; <!-- this is an html comment.it will show up int the response. -…

灯塔arl安装centos7-docker

灯塔arl安装centos7-docker 参考链接 参考链接 https://blog.csdn.net/ydtsh_/article/details/115629346感谢这位作者 https://github.com/TophantTechnology/ARL/wiki/Docker-%E7%8E%AF%E5%A2%83%E5%AE%89%E8%A3%85-ARL环境的安装就按照第一个链接安装。 但是在拉去环境过程…

《大话设计模式》--商场促销--策略模式Strategy(2)

面向对象编程&#xff0c;并不类越多越好&#xff0c;类的划分是为了封装&#xff0c;但分类的基础是抽象&#xff0c;具有相同属性和功能的对象的抽象类集合才是类。 策略模式定义了算法家族&#xff0c;分别封装了起来&#xff0c;让它们之间可以互相替换&#xff0c;此模式…

前端记录代码1

2019独角兽企业重金招聘Python工程师标准>>> 1、页面返回 <a class"retrun-btn" id"returnclick" href"">返回></a> 对应的javascript为&#xff1a; $(function(){ $("#returnclick").click(function(){ h…

如何开放 Azure 虚拟机 Ping 功能

前言 文章《使用 PsPing & PaPing 进行 TCP 端口连通性测试》中提到&#xff0c;ICMP 协议的数据包无法通过 Azure 的防火墙和负载均衡器&#xff0c;所以不能直接使用 Ping 来测试 Azure 中的虚拟机和服务的连通性。实际上&#xff0c;我们仍然能够通过一些特殊设置&#…

LAMP ---Apache用户认证、域名跳转、Apache访问日志介绍······

2019独角兽企业重金招聘Python工程师标准>>> Apache 默认虚拟主机配置 一台服务器可以访问多个网站&#xff0c;每个网站都是一个虚拟主机概念&#xff1a;域名&#xff08;主机名&#xff09;、DNS、解析域名、hosts任何一个域名解析到这台机器&#xff0c;都可以访…

《大话设计模式》--拍摄UFO--单一职责原则Single Responsibility Principle(3) .

SRP:就一个类而言,应该仅有一个引起它变化的原因. 如果一个类承担的职责过多,就等于把这些职责耦合在一起,一个职责的变化可能会削弱或者抑制这个类完成其它职责的能力.这种耦合会导致脆弱的设计,当变化发生时,设计会遭受意到不到的破坏. 软件真正要做的许多内容,就是发现职责…

php对数组中的值进行排序

案例 <?php $a array(1124,1125,1126);$s1 1124;$s2 1125;$ks1 array_search($s1,$a);$ks2 array_search($s2,$a);$a[$ks1] $s2;$a[$ks2] $s1;print_r($a); ?>应用 $current_id $this->_request(current_id,intval); // 当前值 $exchange_id $this->_r…