1. 程式人生 > >設計模式——簡單工廠模式

設計模式——簡單工廠模式

idt index init == set ces edev ini property

聲明:以下內容來源於《大話設計模式》,學習

簡單工廠模式類圖:

技術分享

界面:

技術分享

代碼:

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>

設計模式——簡單工廠模式