该实例基于WPF实现,直接上代码,下面为三层架构的代码。
一 Model
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace 设计模式练习.Model.访问者模式
{public class Computer : ComputerPart{ComputerPart[] parts;public Computer(){parts = new ComputerPart[] { new Mouse(), new Keyboard(), new Monitor() };}public void accept(ComputerPartVisitor partVisitor){foreach (ComputerPart part in parts){part.accept(partVisitor);}partVisitor.Visit(this);}}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace 设计模式练习.Model.访问者模式
{//1,创建访问接口public interface ComputerPart{public void accept(ComputerPartVisitor partVisitor);}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using 设计模式练习.Model.建造者模式;namespace 设计模式练习.Model.访问者模式
{//3,创建实体访问类public class ComputerPartDisplayVisitor : ComputerPartVisitor{//记录访问信息public List<string> lists { get; } = new List<string>();public void Visit(Computer computer){lists.Add(computer.GetType().Name);}public void Visit(Mouse mouse){lists.Add(mouse.GetType().Name);}public void Visit(Keyboard keyboard){lists.Add(keyboard.GetType().Name);}public void Visit(Monitor monitor){lists.Add(monitor.GetType().Name);}}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace 设计模式练习.Model.访问者模式
{//2,创建访问者接口public interface ComputerPartVisitor{public void Visit(Computer computer);public void Visit(Mouse mouse);public void Visit(Keyboard keyboard);public void Visit(Monitor monitor);}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace 设计模式练习.Model.访问者模式
{public class Keyboard : ComputerPart{public void accept(ComputerPartVisitor partVisitor){partVisitor.Visit(this);}}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace 设计模式练习.Model.访问者模式
{public class Monitor : ComputerPart{public void accept(ComputerPartVisitor partVisitor){partVisitor.Visit(this);}}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace 设计模式练习.Model.访问者模式
{public class Mouse : ComputerPart{public void accept(ComputerPartVisitor partVisitor){partVisitor.Visit(this);}}
}
二 View
<Window x:Class="设计模式练习.View.访问者模式.ComputerWindow"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="ComputerWindow" Height="450" Width="800"><Grid><Grid.ColumnDefinitions><ColumnDefinition/><ColumnDefinition/></Grid.ColumnDefinitions><Grid><Grid.RowDefinitions><RowDefinition/><RowDefinition/><RowDefinition/><RowDefinition/></Grid.RowDefinitions><TextBlock Text="{Binding Res}" Grid.Row="0" FontSize="30" TextWrapping="Wrap"/><TextBlock Text="{Binding Res2}" Grid.Row="1" FontSize="30" TextWrapping="Wrap"/><TextBlock Text="{Binding Res3}" Grid.Row="2" FontSize="30" TextWrapping="Wrap"/><TextBlock Text="{Binding Res4}" Grid.Row="3" FontSize="30" TextWrapping="Wrap"/></Grid><Button Content="执行访问" Grid.Column="1" Command="{Binding visitCommand}"/></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 ComputerWindow_ViewModel : ObservableObject{[ObservableProperty]private string res;[ObservableProperty]private string res2;[ObservableProperty]private string res3;[ObservableProperty]private string res4;[RelayCommand]private void visit(){ComputerPart computer = new Computer();ComputerPartDisplayVisitor visitor = new ComputerPartDisplayVisitor();computer.accept(visitor);List<string> list = visitor.lists;Res = list[0];Res2 = list[1];Res3 = list[2];Res4 = list[3];}}
}