工厂方法模式-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)];
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

code_shenbing

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值