当前位置: 首页 > news >正文

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

声明:以下内容来源于《大话设计模式》,学习

简单工厂模式类图:

界面:

代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace 简单工厂模式
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            cmbOperator.ItemsSource = new List<string> { "+", "-", "*", "/" ,"+*"};
            cmbOperator.SelectionChanged -= ComboBox_SelectionChanged_1;
            cmbOperator.SelectedIndex = 0;
            cmbOperator.SelectionChanged += ComboBox_SelectionChanged_1;
        }

        private void ComboBox_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
        {
            string operType = cmbOperator.SelectedItem.ToString();
            Operation oper = OperationFactory.CreateOperation(operType);
            oper.Num1 = Convert.ToDouble(txtValue1.Text);
            oper.Num2 = Convert.ToDouble(txtValue2.Text);
            double resultValue = oper.GetResult();
            txtResult.Text = resultValue.ToString();
        }
    }

    public class Operation
    {
        private double _num1 = 0;
        private double _num2 = 0;
        public double Num1
        {
            set
            {
                _num1 = value;
            }
            get
            {
                return _num1;
            }
        }

        public double Num2
        {
            set
            {
                _num2 = value;
            }
            get
            {
                return _num2;
            }
        }

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

    public class OperationAdd : Operation
    {
        public override double GetResult()
        {
            return Num1 + Num2;
        }
    }

    public class OperationSub : Operation
    {
        public override double GetResult()
        {
            return Num1 - Num2;
        }
    }

    public class OperationMul : Operation
    {
        public override double GetResult()
        {
            return Num1 * Num2;
        }
    }

    public class OperationDiv : Operation
    {
        public override double GetResult()
        {
            if (Num2==0)
            {
                throw new Exception();
            }
            return Num1 / Num2;
        }
    }

    public class OperationPF : Operation
    {
        public override double GetResult()
        {
            return Math.Pow(Num1, Num2);
        }
    }

    public class OperationFactory
    {
        public static Operation CreateOperation(string operate)
        {
            Operation ope = null;
            switch (operate)
            {
                case "+":
                    ope = new OperationAdd();
                    break;
                case "-":
                    ope = new OperationSub();
                    break;
                case "*":
                    ope = new OperationMul();
                    break;
                case "/":
                    ope = new OperationDiv();
                    break;
                case "+*":
                    ope = new OperationPF();
                    break;
            }
            return ope;
        }
    }
}
<Window x:Class="简单工厂模式.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <Style TargetType="TextBox">
            <Setter Property="Width" Value="200"></Setter>
            <Setter Property="Height" Value="30"></Setter>
        </Style>
        <Style TargetType="TextBlock">
            <Setter Property="Width" Value="60"></Setter>
            <Setter Property="Height" Value="30"></Setter>
            <Setter Property="TextAlignment" Value="Right"></Setter>
            <Setter Property="FontSize" Value="20"></Setter>
        </Style>
    </Window.Resources>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition></RowDefinition>
            <RowDefinition></RowDefinition>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition></ColumnDefinition>
            <ColumnDefinition></ColumnDefinition>
        </Grid.ColumnDefinitions>
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition></RowDefinition>
                <RowDefinition></RowDefinition>
                <RowDefinition></RowDefinition>
                <RowDefinition></RowDefinition> 
            </Grid.RowDefinitions>
            <StackPanel Orientation="Horizontal" >
                <TextBlock >数值1:</TextBlock>
                <TextBox x:Name="txtValue1"></TextBox>
            </StackPanel>
            <StackPanel Orientation="Horizontal" Grid.Row="1">
                <TextBlock >数值2:</TextBlock>
                <TextBox x:Name="txtValue2"></TextBox>
            </StackPanel>
            <ComboBox x:Name="cmbOperator" Grid.Row="2" Width="60" SelectionChanged="ComboBox_SelectionChanged_1"></ComboBox>
            <StackPanel Orientation="Horizontal" Grid.Row="3">
                <TextBlock >结果:</TextBlock>
                <TextBox x:Name="txtResult"></TextBox>
            </StackPanel>
        </Grid>        
    </Grid>
</Window>

 

转载于:https://www.cnblogs.com/dog2016/p/7290986.html

相关文章:

  • 吐血分享:QQ群霸屏技术教程(接单篇)
  • js全局变量,局部变量
  • 2017百度之星资格赛 1003 度度熊与邪恶大魔王 背包DP
  • 8086汇编之 CALL 和 RET指令
  • c# 多线程编程中AutoResetEvent和ManualResetEvent
  • 【Python】 配置文件相对路径软件自动执行的工作目录
  • [SDUT](3361) 数据结构实验之图论四:迷宫探索 ---DFS(图)
  • proxy汇总-1
  • 使用for循环输出九九乘法表
  • ffmpeg学习(二) 通过rtsp获取H264裸流并保存到mp4文件
  • 安装node.js和npm
  • 推荐几款谷歌浏览器的使用插件
  • webservcie学习之webservice是什么
  • Win10《芒果TV》更新v3.6.0秋收版:新增追剧磁贴、记忆续播、跳转列表
  • Centos7安装TensorFlow
  • [译] 理解数组在 PHP 内部的实现(给PHP开发者的PHP源码-第四部分)
  • 【跃迁之路】【519天】程序员高效学习方法论探索系列(实验阶段276-2018.07.09)...
  • CentOS 7 修改主机名
  • create-react-app做的留言板
  • leetcode-27. Remove Element
  • Python socket服务器端、客户端传送信息
  • React 快速上手 - 06 容器组件、展示组件、操作组件
  • Vue2.x学习三:事件处理生命周期钩子
  • windows下mongoDB的环境配置
  • 不上全站https的网站你们就等着被恶心死吧
  • 动手做个聊天室,前端工程师百无聊赖的人生
  • 解析 Webpack中import、require、按需加载的执行过程
  • 前言-如何学习区块链
  • 使用agvtool更改app version/build
  • 微服务框架lagom
  • 异常机制详解
  • 掌握面试——弹出框的实现(一道题中包含布局/js设计模式)
  • Nginx实现动静分离
  • ​批处理文件中的errorlevel用法
  • #!/usr/bin/python与#!/usr/bin/env python的区别
  • #NOIP 2014#Day.2 T3 解方程
  • #图像处理
  • #我与Java虚拟机的故事#连载07:我放弃了对JVM的进一步学习
  • #我与Java虚拟机的故事#连载10: 如何在阿里、腾讯、百度、及字节跳动等公司面试中脱颖而出...
  • $.proxy和$.extend
  • (16)Reactor的测试——响应式Spring的道法术器
  • (2)关于RabbitMq 的 Topic Exchange 主题交换机
  • (floyd+补集) poj 3275
  • (二)Pytorch快速搭建神经网络模型实现气温预测回归(代码+详细注解)
  • (二)正点原子I.MX6ULL u-boot移植
  • (附源码)springboot优课在线教学系统 毕业设计 081251
  • (算法)Travel Information Center
  • (提供数据集下载)基于大语言模型LangChain与ChatGLM3-6B本地知识库调优:数据集优化、参数调整、Prompt提示词优化实战
  • (转)Android中使用ormlite实现持久化(一)--HelloOrmLite
  • (转)C#调用WebService 基础
  • (转)全文检索技术学习(三)——Lucene支持中文分词
  • (最简单,详细,直接上手)uniapp/vue中英文多语言切换
  • .NET 4.0中使用内存映射文件实现进程通讯
  • .net core 实现redis分片_基于 Redis 的分布式任务调度框架 earth-frost
  • .Net 访问电子邮箱-LumiSoft.Net,好用