首页 > 其他分享 >迭代器模式

迭代器模式

时间:2023-06-03 21:48:22浏览次数:36  
标签:return 迭代 Iterator object 模式 interface aggregate public

The Iterator design pattern provides a way to acess the elements of an aggregate object sequentially without exposing its underlying representation.

迭代器模式提供了顺序访问聚合对象中元素的方式,而不需要暴露底层表示。

 UML Class Diagram

 Iterator; This is going to be an interface defining the operations for accessing and traversing elements in a sequence.

 ConcreteIterator: This is going to be a concrete class implementing the Iterator interface and providing implementation for Iterator interface method. This class also keep track of the current position of the elment in the traversal.

 Aggregate: This is going to be an interface that defines an operation to create an interator object.

 ConcreteAgregate: This is going to be a conrecte class that implements the Aggreate interface to return an instance of the proper Conrete Itorator class i.e. an instance of the Iterator class.

  Client:This is class that going to use the Iterator and Aggregate interface and access the elments.

Structure Code in C#

public interface IAggregate
{
    IIterator CreateIterator();
}

public class ConcreteAggregate : IAggregate
{
    private List<object> items = new List<object>();
    public IIterator CreateIterator()
    {
        return new ConcreteIterator(this);
    }

    public Int32 Count
    { 
        get { return items.Count; }
    }

    public object this[Int32 index]
    {
        get { return items[index]; }
        set { items.Insert(index, value); }
    }
}
Aggregate
public interface IIterator
{
    object First();
    object Next();
    bool IsLast();
    object CurrentItem();

}

public class ConcreteIterator: IIterator
{
    ConcreteAggregate aggregate;
    Int32 current = 0;
    public ConcreteIterator(ConcreteAggregate aggregate)
    {
        this.aggregate = aggregate;
    }

    public object First()
    {
        return aggregate[0];
    }

    public object Next()
    {
        object next = null;
        if (current < aggregate.Count - 1)
            next = aggregate[++current];
        return next;
    }

    public object CurrentItem()
    {
        return aggregate[current];
    }

    public bool IsLast()
    {
        return current >= aggregate.Count;
    }
}
Iterator

Why do we need to use the Iterator Design Pattern in C#?

 The Iterator Design Pattern in C# allows us to Access the elements of a collection wihout exposing its internal data structure. That means it allows you to navigate through a different collection of data using a common interface wihout knowing about their underlying implementation.

标签:return,迭代,Iterator,object,模式,interface,aggregate,public
From: https://www.cnblogs.com/qindy/p/17420022.html

相关文章

  • 状态模式
    TheStatedesignpatternallowsanobjecttoalteritsbehaviorwhenitsinternalstatechanges,Theobjectwillappeartochangeitsclass.状态设计模式允许当对象内部状态改变时改变其行为,对象似乎改变了它的类。UMLClassDiagram State:Thisisgoingtobea......
  • 享元模式
    TheFlyweightdesignpatternusessharingtosupportlargenumbersoffine-gainedobjectsefficiently.享元模式用共享有效支持大量细粒度的对象。UMLClassDiagram Flyweight:Theflyweightinterfaceenablessharingbutitdoesnotenforceit.Theconcreteobj......
  • 责任链模式
    TheChainofResponsibilitydesignpatternavoidscouplingthesenderoftherequesttoitsreceiverbygivingmorethanoneobjectachancetohandletherequest.Thispatternchainsthereceivingobjectsandpassestherequestalongthechainuntilano......
  • 命令模式
    TheCommanddesignpatternencapsulatesarequestasanobject,therebylettingyouparamizeclientswithdifferentrequests,queueorlogrequests,andsupportundoableoperations.命令模式封装请求作为一个对象,因此让你参数化客户端用不同的requests,队列或者日志r......
  • 解释器模式
    Givealanguage,theInterpreterdesignpatterndefinesarepresentationforitsgrammaralongwithaninterpreterthatusestherepresentationtointerpretsentencesinthelanguage.TheInterpreterDesignPatternprovidesawaytoevaluatelanguagegram......
  • 组合模式
    TheCompositedesignpatterncomposesobjectsintotreestructurestorepresentpart-wholehierarchies.Thispatternletsclientstreatindividualobjectandcompositionsofobjectsuniformly.组合模式将对象组合成tree结构代表部分-整体层次结构,这种模式允许客户......
  • 装饰器模式
    TheDecoratorDesignPatternattachesadditionalresponsibilitiestoanobjectdynamically.Thispatternprovideaflexiblealternativetosubclassingforextendingfunctionality.装饰器模式动态的给Object添加额外的职责,这个模式为SubClassing提供灵活的扩展功能。......
  • 外观模式
    TheFacade designpattenprovidesaunifiedinterfacetoasetofinterfacesinasubsystem.Thispatterndefinesahigher-levelinterfacethatmakesthesubsystemeasiertouse.外观模式为子系统一组接口提供了统一的接口,这种模式定义了高级接口,便于子系统调用。......
  • 适配器模式
    TheAdpativedesignpatternconvertstheinterfaceofaclasstoanotherinterfaceclientsexpect.Thisdesignpatternletsclassesworktogetherthatcouldn'totherwisebecauseofincompatibleinterfaces.适配器模式将类的接口转换为客户期望的另外一个接口,这种......
  • 桥接模式
    TheBridgedesignpatterndecouplesanabstractionfromitsimplementationsothathetwocanvaryindependently.桥接模式将抽象和实现解耦,以便两者可以独立变化。UMLClassDiagram Abstraction:definestheabstraction'sinterface;matainsareferencetoanobj......