码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2017-08-05 19:00:34      阅读:210      评论:0      收藏:0      [点我收藏+]

标签:ane   aging   inpu   mat   模式   navig   技术   null   hang   

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

简单工厂模式类图:

技术分享

界面:

技术分享

代码:

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>

 

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

标签:ane   aging   inpu   mat   模式   navig   技术   null   hang   

原文地址:http://www.cnblogs.com/dog2016/p/7290986.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!