该实例基于WPF实现,直接上代码,下面为三层架构的代码。
目录
一 Model
二 View
三 ViewModel
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();
}
}
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();
}
}
}