赞
踩
该实例基于WPF实现,直接上代码,下面为三层架构的代码。
目录
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
-
- namespace 设计模式练习.Model.迭代器模式
- {
- //4,具体迭代器类
- public class ConcreteIterator : Iterator
- {
- //迭代器需要对集合对象就行遍历,这里需要引用集合对象
- private ConcreteList _list;
- private int _index;
-
- public ConcreteIterator(ConcreteList list)
- {
- _list = list;
- _index = 0;
- }
-
- public object GetCurrent()
- {
- return _list.GetElement(_index);
- }
-
- public bool MoveNext()
- {
- if (_index < _list.Length)
- {
- return true;
- }
-
- return false;
- }
-
- public void Next()
- {
- if (_index < _list.Length)
- {
- _index++;
- }
- }
-
- public void Reset()
- {
- _index = 0;
- }
- }
- }
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
-
- namespace 设计模式练习.Model.迭代器模式
- {
- //3,具体聚合类
- public class ConcreteList : IListCollection
- {
- object[] collections = null;
-
- public ConcreteList(object[] colls)
- {
- collections = colls;
- }
-
- public Iterator GetIterator()
- {
- return new ConcreteIterator(this);
- }
-
-
- public int Length
- {
- get { return collections.Length; }
- }
-
-
- public object GetElement(int index)
- {
- return collections[index];
- }
- }
- }
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
-
- namespace 设计模式练习.Model.迭代器模式
- {
- //1,定义抽象聚合类
- public interface IListCollection
- {
- Iterator GetIterator();
- }
- }
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
-
- namespace 设计模式练习.Model.迭代器模式
- {
- //2,定义迭代器抽象类
- public interface Iterator
- {
- bool MoveNext();
- object GetCurrent();
- void Next();
- void Reset();
- }
- }
- <Window x:Class="设计模式练习.View.迭代器模式.IteratorWindow"
- 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="IteratorWindow" Height="450" Width="800">
- <Grid>
- <Grid.ColumnDefinitions>
- <ColumnDefinition/>
- <ColumnDefinition/>
- </Grid.ColumnDefinitions>
- <Grid.RowDefinitions>
- <RowDefinition/>
- <RowDefinition/>
- <RowDefinition/>
- <RowDefinition/>
- </Grid.RowDefinitions>
- <TextBlock Text="{Binding Res1}" Grid.Row="0" Grid.Column="0" TextWrapping="Wrap"/>
- <TextBlock Text="{Binding Res2}" Grid.Row="1" Grid.Column="0" TextWrapping="Wrap"/>
- <TextBlock Text="{Binding Res3}" Grid.Row="2" Grid.Column="0" TextWrapping="Wrap"/>
- <TextBlock Text="{Binding Res4}" Grid.Row="3" Grid.Column="0" TextWrapping="Wrap"/>
-
- <Button Content="遍历集合1" Command="{Binding Col_1Command}" Grid.Row="0" Grid.Column="1"/>
- <Button Content="遍历集合2" Command="{Binding Col_2Command}" Grid.Row="1" Grid.Column="1"/>
- <Button Content="遍历集合3" Command="{Binding Col_3Command}" Grid.Row="2" Grid.Column="1"/>
- <Button Content="遍历集合4" Command="{Binding Col_4Command}" Grid.Row="3" Grid.Column="1"/>
-
- </Grid>
- </Window>
- 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 IteratorWindow_ViewModel : ObservableObject
- {
- [ObservableProperty]
- private string res1;
-
- [ObservableProperty]
- private string res2;
-
- [ObservableProperty]
- private string res3;
-
- [ObservableProperty]
- private string res4;
-
- [RelayCommand]
- private void Col_1()
- {
- object[] ints = { 1, 2, 3, 4, 5, 66, 77, 91, 453 };
- Iterator iterator;
- IListCollection list = new ConcreteList(ints);
- iterator = list.GetIterator();
- StringBuilder sb = new StringBuilder();
-
- while (iterator.MoveNext())
- {
- string str = iterator.GetCurrent().ToString();
- sb.Append(str + ",");
- //目标指向下一个
- iterator.Next();
- }
-
- Res1 = sb.ToString();
- }
-
- [RelayCommand]
- private void Col_2()
- {
- object[] ints = { "李俊", "谢军", "小露露", "", "菲利普" };
- Iterator iterator;
- IListCollection list = new ConcreteList(ints);
- iterator = list.GetIterator();
- StringBuilder sb = new StringBuilder();
-
- while (iterator.MoveNext())
- {
- string str = iterator.GetCurrent().ToString();
- sb.Append(str + ",");
- iterator.Next();
- }
-
- Res2 = sb.ToString();
- }
-
- [RelayCommand]
- private void Col_3()
- {
- object[] ints = { 12.44, 13.567, 33.789 };
- Iterator iterator;
- IListCollection list = new ConcreteList(ints);
- iterator = list.GetIterator();
- StringBuilder sb = new StringBuilder();
-
- while (iterator.MoveNext())
- {
- string str = iterator.GetCurrent().ToString();
- sb.Append(str + ",");
- iterator.Next();
- }
-
- Res3 = sb.ToString();
- }
-
- [RelayCommand]
- private void Col_4()
- {
- object[] ints = { Res1, Res2, Res3 };
- Iterator iterator;
- IListCollection list = new ConcreteList(ints);
- iterator = list.GetIterator();
- StringBuilder sb = new StringBuilder();
-
- while (iterator.MoveNext())
- {
- string str = iterator.GetCurrent().ToString();
- sb.Append(str + ",");
- iterator.Next();
- }
-
- Res4 = sb.ToString();
- }
- }
- }
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。