"None": iterator Mode

Source: Internet
Author: User
1. iterator mode: provides a method to access each element in a set object sequentially without exposing the internal representation of the object.
Here is its structure:

2. Application in the iterator mode: When you need to access a clustered object, no matter what these objects are All need to be traversed You can consider using this mode.
For example, I personally think that anti-virus software can be regarded as a typical iterator mode when "checking" Anti-Virus in folders. When we set anti-virus software to scan and kill viruses, no matter whether we set the file type to common files, compressed packages, or hidden system files, anti-virus software must traverse these files for virus database feature matching and removal.
3. Finally, paste Code : Using System;
Using System. collections;
Using System. Collections. Generic;

Namespace Iteratorpattern
{
///   <Summary>
/// Iterator abstract class
///   </Summary>
Abstract   Class Iterator
{
Public   Abstract   Object First ();
Public   Abstract   Object Next ();
Public   Abstract   Bool Isdone ();
Public   Abstract   Object Currentitem ();
}

///   <Summary>
/// Aggregation abstract class
///   </Summary>
Abstract   Class Aggregate
{
Public   Abstract Iterator createiterator (); // Create iterator
}

///   <Summary>
/// Specific iterator class. By default, the set elements are traversed from start to end. We can declare different specific iterators to traverse from end to end as needed.
///   </Summary>
Class Concreteiterator: iterator
{
Private Concreteaggregate aggregate; // Specific Aggregation
Private   Int Current =   0 ;

///   <Summary>
/// During initialization, the specific aggregation is passed in
///   </Summary>
///   <Param name = "aggregate"> </param>
Public Concreteiterator (concreteaggregate aggregate)
{
This . Aggregate = Aggregate;
}

Public Override ObjectFirst ()
{
ReturnAggregate [0];
}

Public   Override   Object Next ()
{
Object Retobj =   Null ;
Current ++ ;
If (Current < Aggregate. Count)
{
Retobj = Aggregate [current];
}
Return retobj;
}

Public override bool isdone ()
{< br> return current = aggregate. Count ? true : false ;< BR >}

Public Override ObjectCurrentitem ()
{
ReturnAggregate [current];//Returns the current clustered object.
}
}

///   <Summary>
/// Specific clustering class
///   </Summary>
Class Concreteaggregate: Aggregate
{
// Declare a list generic variable to store collection objects. Other sets, such as arraylist, can also be used.
Private Ilist < Object > Items =   New List < Object > ();
Public   Override Iterator createiterator ()
{
Return   New Concreteiterator ( This );
}

Public IntCount
{
Get{ReturnItems. Count ;}//Returns the total number of aggregates.
}

// Declare an Indexer
Public   Object   This [ Int Index]
{
Get { Return Items [Index];}
Set {Items. insert (index, value );}
}

}

///   <Summary>
/// Client call
///   </Summary>
Class Program
{
Static   Void Main ( String [] ARGs)
{
Concreteaggregate Aggregate =   New Concreteaggregate (); // Correspond to the folders in the example, that is, the specific aggregation

Aggregate [ 0 ] =   " C drive system32 folder File " ; // Add the folder to be attacked, that is, the object array.
Aggregate [ 1 ] =   " C drive program folder File " ;
Aggregate [ 2 ] =   " D Drive game folder files " ;
Aggregate [ 3 ] =   " All files on the edisk " ;

Iterator myiterator =   New Concreteiterator (aggregate ); // Declare the iterator object and prepare the selected folder to be traversed
Object Item = Myiterator. First (); // Retrieve the first folder
Console. writeline ( " First folder to be viewed and killed: {0} " , Item );
While ( ! Myiterator. isdone ())
{
Console. writeline ( " {0} has been killed " , Myiterator. currentitem ());
Myiterator. Next (); // Continue to traverse
}

Console. Readline ();
}
}
}

PS: For the. NET iterator, I have briefly introduced it in the last part of the previous article. You can refer to it for your reference.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.