该实例基于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)];}}
}