工厂方法模式-C#实现

该实例基于WPF实现,直接上代码,下面为三层架构的代码。

一 Model

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

namespace 设计模式练习.Model.工厂方法模式
{
    internal class ChinesePerson : Person
    {
        public ChinesePerson(string name, string sex, int age) : base(name, sex, age)
        {
        }

        public override string work()
        {
            string msg = $"开始工作,姓名:{Name},性别:{Sex},年龄:{Age}";
            return msg;
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 设计模式练习.Model.工厂方法模式
{
    internal class ChinesePersonFactory : CreatorFactory
    {
        public override Person createPerson()
        {
            return new ChinesePerson("中国人", "男", 22);
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 设计模式练习.Model.工厂方法模式
{
    //抽象工厂类
    public abstract class CreatorFactory
    {
        //工厂方法
        public abstract Person createPerson();
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 设计模式练习.Model.工厂方法模式
{
    internal class JapanPerson : Person
    {
        public JapanPerson(string name, string sex, int age) : base(name, sex, age)
        {
        }

        public override string work()
        {
            string msg = $"开始工作,姓名:{Name},性别:{Sex},年龄:{Age}";
            return msg;
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 设计模式练习.Model.工厂方法模式
{
    internal class JapanPersonFactory : CreatorFactory
    {
        public override Person createPerson()
        {
            return new JapanPerson("日本人", "女", 30);
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 设计模式练习.Model.工厂方法模式
{
    public abstract class Person
    {
        protected Person(string name, string sex, int age)
        {
            Name = name;
            Sex = sex;
            Age = age;
        }


        public string Name { get; set; }
        public string Sex { set; get; }
        public int Age { get; set; }

        //工作
        public abstract string work();
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 设计模式练习.Model.工厂方法模式
{
    internal class USAPerson : Person
    {
        public USAPerson(string name, string sex, int age) : base(name, sex, age)
        {
        }

        public override string work()
        {
            string msg = $"开始工作,姓名:{Name},性别:{Sex},年龄:{Age}";
            return msg;
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 设计模式练习.Model.工厂方法模式
{
    internal class USAPersonFactory : CreatorFactory
    {
        public override Person createPerson()
        {
            return new USAPerson("美国人", "女", 33);
        }
    }
}

二 View

<Window x:Class="设计模式练习.View.工厂方法模式.FactoryMethod"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:设计模式练习.View.工厂方法模式"
        mc:Ignorable="d"
        Title="FactoryMethod" Height="450" Width="800">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="8*"/>
            <ColumnDefinition Width="2*"/>
        </Grid.ColumnDefinitions>

        <StackPanel Grid.Column="0">
            <TextBlock Text="{Binding Chinese}"/>
            <TextBlock Text="{Binding USA}"/>
            <TextBlock Text="{Binding Japan}"/>
        </StackPanel>

        <Button Content="生产随机对象" Grid.Column="1" Command="{Binding psCommand}"/>
    </Grid>
</Window>

三 ViewModel

using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using 设计模式练习.Model.工厂方法模式;

namespace 设计模式练习.ViewModel.工厂方法模式
{
    partial class FactoryMethod_ViewModel : ObservableObject
    {
        [ObservableProperty]
        private string chinese;

        [ObservableProperty]
        private string japan;

        [ObservableProperty]
        private string uSA;


        [RelayCommand]
        private void ps()
        {
            Person ch = new ChinesePersonFactory().createPerson();
            Person jp = new JapanPersonFactory().createPerson();
            Person us = new USAPersonFactory().createPerson();

            string[] persons = { ch.work(), jp.work(), us.work() };

            Chinese = persons[new Random().Next(0, 3)];
            Japan = persons[new Random().Next(0, 3)];
            USA = persons[new Random().Next(0, 3)];
        }
    }
}

相关推荐

  1. 工厂方法模式-C#实现

    2024-01-27 00:38:01       66 阅读
  2. C++中的工厂方法模式

    2024-01-27 00:38:01       30 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-27 00:38:01       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-27 00:38:01       101 阅读
  3. 在Django里面运行非项目文件

    2024-01-27 00:38:01       82 阅读
  4. Python语言-面向对象

    2024-01-27 00:38:01       91 阅读

热门阅读

  1. C++核心编程:类和对象 笔记

    2024-01-27 00:38:01       46 阅读
  2. 2024美赛数学建模D题思路模型代码论文

    2024-01-27 00:38:01       60 阅读
  3. Python高超音速导弹

    2024-01-27 00:38:01       51 阅读
  4. Android studio 之 ListView

    2024-01-27 00:38:01       68 阅读
  5. Linux面试系列-02

    2024-01-27 00:38:01       51 阅读
  6. Ubuntu22.04安装4090显卡驱动

    2024-01-27 00:38:01       55 阅读
  7. mysql字符集

    2024-01-27 00:38:01       48 阅读
  8. SpringCloudStream整合MQ(待完善)

    2024-01-27 00:38:01       56 阅读