迭代器模式-C#实现

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

目录

一 Model

二 View

三 ViewModel


一 Model

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>

三 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 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();
        }
    }
}

相关推荐

  1. 模式-C++实现

    2024-01-26 06:10:01       36 阅读
  2. 模式-C#实现

    2024-01-26 06:10:01       32 阅读
  3. C++初阶-反向模拟实现

    2024-01-26 06:10:01       34 阅读
  4. 模式(极简c++)》

    2024-01-26 06:10:01       16 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-01-26 06:10:01       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-26 06:10:01       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-26 06:10:01       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-26 06:10:01       20 阅读

热门阅读

  1. Unity截取UI

    2024-01-26 06:10:01       30 阅读
  2. 设计一套扑克牌

    2024-01-26 06:10:01       24 阅读
  3. Python3进行pdf文件分割及转word

    2024-01-26 06:10:01       33 阅读
  4. 开源CRM客户管理系统-FeelCRM

    2024-01-26 06:10:01       37 阅读
  5. Centos7.6之禅道开源版17.6.1安装记录

    2024-01-26 06:10:01       31 阅读
  6. Fastbee开源物联网项目RoadMap

    2024-01-26 06:10:01       30 阅读
  7. Go(四)gin框架

    2024-01-26 06:10:01       31 阅读