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

Posted 苹果园dog

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了设计模式——简单工厂模式相关的知识,希望对你有一定的参考价值。

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

简单工厂模式类图:

界面:

代码:

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>

 

以上是关于设计模式——简单工厂模式的主要内容,如果未能解决你的问题,请参考以下文章

设计模式学习——简单工厂模式工厂模式抽象工厂模式

设计模式之简单工厂模式

设计模式-简单工厂工厂方法模式抽象工厂模式详解

Delphi 设计模式:《HeadFirst设计模式》Delphi7代码---工厂模式之简单工厂

C#之三十八 简单工厂设计模式

设计模式之简单工厂模式