当前位置:   article > 正文

迭代器模式-C#实现

迭代器模式-C#实现

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

目录

一 Model

二 View

三 ViewModel


一 Model

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace 设计模式练习.Model.迭代器模式
  7. {
  8. //4,具体迭代器类
  9. public class ConcreteIterator : Iterator
  10. {
  11. //迭代器需要对集合对象就行遍历,这里需要引用集合对象
  12. private ConcreteList _list;
  13. private int _index;
  14. public ConcreteIterator(ConcreteList list)
  15. {
  16. _list = list;
  17. _index = 0;
  18. }
  19. public object GetCurrent()
  20. {
  21. return _list.GetElement(_index);
  22. }
  23. public bool MoveNext()
  24. {
  25. if (_index < _list.Length)
  26. {
  27. return true;
  28. }
  29. return false;
  30. }
  31. public void Next()
  32. {
  33. if (_index < _list.Length)
  34. {
  35. _index++;
  36. }
  37. }
  38. public void Reset()
  39. {
  40. _index = 0;
  41. }
  42. }
  43. }
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace 设计模式练习.Model.迭代器模式
  7. {
  8. //3,具体聚合类
  9. public class ConcreteList : IListCollection
  10. {
  11. object[] collections = null;
  12. public ConcreteList(object[] colls)
  13. {
  14. collections = colls;
  15. }
  16. public Iterator GetIterator()
  17. {
  18. return new ConcreteIterator(this);
  19. }
  20. public int Length
  21. {
  22. get { return collections.Length; }
  23. }
  24. public object GetElement(int index)
  25. {
  26. return collections[index];
  27. }
  28. }
  29. }
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace 设计模式练习.Model.迭代器模式
  7. {
  8. //1,定义抽象聚合类
  9. public interface IListCollection
  10. {
  11. Iterator GetIterator();
  12. }
  13. }

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace 设计模式练习.Model.迭代器模式
  7. {
  8. //2,定义迭代器抽象类
  9. public interface Iterator
  10. {
  11. bool MoveNext();
  12. object GetCurrent();
  13. void Next();
  14. void Reset();
  15. }
  16. }




  1. <Window x:Class="设计模式练习.View.迭代器模式.IteratorWindow"
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  5. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  6. xmlns:local="clr-namespace:设计模式练习.View.迭代器模式"
  7. mc:Ignorable="d"
  8. Title="IteratorWindow" Height="450" Width="800">
  9. <Grid>
  10. <Grid.ColumnDefinitions>
  11. <ColumnDefinition/>
  12. <ColumnDefinition/>
  13. </Grid.ColumnDefinitions>
  14. <Grid.RowDefinitions>
  15. <RowDefinition/>
  16. <RowDefinition/>
  17. <RowDefinition/>
  18. <RowDefinition/>
  19. </Grid.RowDefinitions>
  20. <TextBlock Text="{Binding Res1}" Grid.Row="0" Grid.Column="0" TextWrapping="Wrap"/>
  21. <TextBlock Text="{Binding Res2}" Grid.Row="1" Grid.Column="0" TextWrapping="Wrap"/>
  22. <TextBlock Text="{Binding Res3}" Grid.Row="2" Grid.Column="0" TextWrapping="Wrap"/>
  23. <TextBlock Text="{Binding Res4}" Grid.Row="3" Grid.Column="0" TextWrapping="Wrap"/>
  24. <Button Content="遍历集合1" Command="{Binding Col_1Command}" Grid.Row="0" Grid.Column="1"/>
  25. <Button Content="遍历集合2" Command="{Binding Col_2Command}" Grid.Row="1" Grid.Column="1"/>
  26. <Button Content="遍历集合3" Command="{Binding Col_3Command}" Grid.Row="2" Grid.Column="1"/>
  27. <Button Content="遍历集合4" Command="{Binding Col_4Command}" Grid.Row="3" Grid.Column="1"/>
  28. </Grid>
  29. </Window>

三 ViewModel

  1. using CommunityToolkit.Mvvm.ComponentModel;
  2. using CommunityToolkit.Mvvm.Input;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using 设计模式练习.Model.迭代器模式;
  9. namespace 设计模式练习.ViewModel.迭代器模式
  10. {
  11. partial class IteratorWindow_ViewModel : ObservableObject
  12. {
  13. [ObservableProperty]
  14. private string res1;
  15. [ObservableProperty]
  16. private string res2;
  17. [ObservableProperty]
  18. private string res3;
  19. [ObservableProperty]
  20. private string res4;
  21. [RelayCommand]
  22. private void Col_1()
  23. {
  24. object[] ints = { 1, 2, 3, 4, 5, 66, 77, 91, 453 };
  25. Iterator iterator;
  26. IListCollection list = new ConcreteList(ints);
  27. iterator = list.GetIterator();
  28. StringBuilder sb = new StringBuilder();
  29. while (iterator.MoveNext())
  30. {
  31. string str = iterator.GetCurrent().ToString();
  32. sb.Append(str + ",");
  33. //目标指向下一个
  34. iterator.Next();
  35. }
  36. Res1 = sb.ToString();
  37. }
  38. [RelayCommand]
  39. private void Col_2()
  40. {
  41. object[] ints = { "李俊", "谢军", "小露露", "", "菲利普" };
  42. Iterator iterator;
  43. IListCollection list = new ConcreteList(ints);
  44. iterator = list.GetIterator();
  45. StringBuilder sb = new StringBuilder();
  46. while (iterator.MoveNext())
  47. {
  48. string str = iterator.GetCurrent().ToString();
  49. sb.Append(str + ",");
  50. iterator.Next();
  51. }
  52. Res2 = sb.ToString();
  53. }
  54. [RelayCommand]
  55. private void Col_3()
  56. {
  57. object[] ints = { 12.44, 13.567, 33.789 };
  58. Iterator iterator;
  59. IListCollection list = new ConcreteList(ints);
  60. iterator = list.GetIterator();
  61. StringBuilder sb = new StringBuilder();
  62. while (iterator.MoveNext())
  63. {
  64. string str = iterator.GetCurrent().ToString();
  65. sb.Append(str + ",");
  66. iterator.Next();
  67. }
  68. Res3 = sb.ToString();
  69. }
  70. [RelayCommand]
  71. private void Col_4()
  72. {
  73. object[] ints = { Res1, Res2, Res3 };
  74. Iterator iterator;
  75. IListCollection list = new ConcreteList(ints);
  76. iterator = list.GetIterator();
  77. StringBuilder sb = new StringBuilder();
  78. while (iterator.MoveNext())
  79. {
  80. string str = iterator.GetCurrent().ToString();
  81. sb.Append(str + ",");
  82. iterator.Next();
  83. }
  84. Res4 = sb.ToString();
  85. }
  86. }
  87. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/blog/article/detail/45197
推荐阅读
相关标签
  

闽ICP备14008679号