• Home
  • Forum
  • Terms and Privacy
17ebook! free ebooks! free download!
Complete online surveys and earn cash from GlobalTestMarket


Search here...
     

Layman’s language and reading notes-Iterator Design Pattern

Posted by admin 17 February, 2011

Get your FREE Diabetes Recipes eBook!Laymans language and reading notes Iterator Design Pattern

First, the mode before:
Iterator before talking about, look at the basic principles of an OO: Single Responsibility Principle (Single responsibility principle).
Low coupling, high cohesion (Low coupling, High cohesion) is a software engineering emphasis on the basic guidelines, but also for the majority of designers are looking for. The single responsibility principle is precisely for this issue should be. Each class emphasizes a single role should only be responsible for a function to deal with our old friend – change (Vary). Responsibilities (functions) are often the source of change, once too many responsibilities involved in a class (function), this class ‘handle’ was correspondingly increased.
In fact, this principle can also be ways for us to provide specification writing. Many have suggested that good programming practice should not be too long a way, a method should do only one thing, that is the best to do ‘atomic’.
We often encounter a class that contains the data collection situation, generally we will not worry about how users traverse the collection class (to traverse the collection of duties), it is entirely thanks to a framework to help us achieve iterator, iterator help us complete a traverse the collection responsibilities.
Second, the model began:
Iterator pattern and the situation described above has a little difference, the first two cases, a set is generally implemented as a list so you can use foreach to traverse party elements. The Iterator will shield the realization of the collection, that users do not know the collection or array, and so is the list.
Standard iterator model as follows figure:

Iterator interface to traverse the data set assumed the duties of (figure omitted specific iterator). HasNext determine whether it contains more elements, Next remove the next element, usually the case when in use are as follows:
….
while (iter.HasNext ())
{
object item = iter.Next ();
….
}
But as I said before, framework already contains a complete set of mechanisms for us to achieve iterator pattern: IEnumerable and IEnumerator interfaces and their generic counterparts.
IEnumerable first look at the code:
public interface IEnumerable
{
IEnumerator GetEnumerator ();
}
Very simple (similar to AbstractClass), look under the IEnumerator implementation:
public interface IEnumerator
{
bool MoveNext ();
object Current {get;}
void Reset ();
}
This interface contains the Iterator interface functions similar to the above method. Therefore, in C # to implement iterators, no need to detour:
using System;
using System.Collections;

public class Person
{
public Person (string fName, string lName)
{
this. firstName = fName;
this. lastName = lName;
}

public string firstName;
public string lastName;
}

public class People: IEnumerable
{
private Person [] _people;
public People (Person [] pArray)
{
_people = new Person [pArray.Length];

for (int i = 0; i {
_people [i] = pArray [i];
}
}
/ / Implement IEnumerable
public IEnumerator GetEnumerator ()
{
return new PeopleEnum (_people);
}
}
/ / Will walk into the PeopleEnum duties
public class PeopleEnum: IEnumerator
{
public Person [] _people;

/ / Enumerators are positioned before the first element
/ / Until the first MoveNext () call.
int position = – 1;

public PeopleEnum (Person [] list)
{
_people = list;
}

public bool MoveNext ()
{
position + +;
return (position <_people.Length);
}

public void Reset ()
{
position = – 1;
}

public object Current
{
get
{
try
{
return _people [position];
}
catch (IndexOutOfRangeException)
{
throw new InvalidOperationException ();
}
}
}
}
When we need access to each of the Person People can be like this:
static void Main ()
{
var peopleArray = new Person [3]
{
new Person (“John”, “Smith”),
new Person (“Jim”, “Johnson”),
new Person (“Sue”, “Rabon”),
};

var peopleList = new People (peopleArray);
var iter = peopleList.GetEnumerator ();
while (iter.MoveNext ())
{
var person = iter.Current as Person;
if (person! = null)
{
Console.WriteLine (person.firstName + ” ” + Person.lastName);
}
}

}
Heartening is that we do not always while (… MoveNext ()){…}, in. Net long as the implements IEnumerable (or its generic version), you can use foreach to traverse operation As a result, the world becomes very clean (using the generic version is cleaner ):
static void Main ()
{
var peopleArray = new Person [3]
{
new Person (“John”, “Smith”),
new Person (“Jim”, “Johnson”),
new Person (“Sue”, “Rabon”),
};

var peopleList = new People (peopleArray);
foreach (var obj in peopleList)
{
var person = obj as Person;
if (obj! = null)
{
Console.WriteLine (person.firstName + ” ” + Person.lastName);
}
}

}
The example above shows patterns for the purpose of common usage, a custom Enumerator, in fact, in the framework, most of the collection of structures has been achieved IEnumerble this interface, so often we do not own to realize this interface, and only need something like this:
public class People: IEnumerable
{
private object [] _people;
public People (Person [] pArray)
{
_people = new Person [pArray.Length];

for (int i = 0; i {
_people [i] = pArray [i];
}
}
/ / Implement IEnumerable
public IEnumerator GetEnumerator ()
{
return _people.GetEnumerator ();
}
}
Directly call the GetEnumerator method to these collections.
(Example code from MSDN , done some little changes)
Third, the model at the end:
Two key points:
1. Provide a way in order to access the elements of an aggregate object
2., Without exposing its internal representation of the object
The above individual learning experiences, Pleased to meet you.

Categories : book review Tags :

Comments

No comments yet.


Sorry, the comment form is closed at this time.

Subscribe via Email


Windows Touch Technics and Technology tablet Solution Manual Smart Security/Hacking Science Related Science/Engineering samsung Review Programming Phones phone Nokia Newspapers netbooks Netbook Mobile Medical/Medicine Laptops Laptop IT Certification Iphone Hobbies Health Graphics and Design Google Game For Women For Men features Ericsson Encyclopedias Economics and Finances Economics/Business deals DB Consumer Electronics Computer Related Computer Comics Apple android about
  • Categories

    • book review
    • Ebook Download
  • Meta

    • Log in
    • Entries RSS
    • Comments RSS
    • WordPress.org
  • Archives

    • May 2013
    • April 2013
    • March 2013
    • February 2013
    • January 2013
    • December 2012
    • November 2012
    • October 2012
    • September 2012
    • August 2012
    • July 2012
    • June 2012
    • May 2012
    • April 2012
    • March 2012
    • February 2012
    • January 2012
    • December 2011
    • November 2011
    • October 2011
    • September 2011
    • August 2011
    • July 2011
    • June 2011
    • May 2011
    • April 2011
    • March 2011
    • February 2011
    • January 2011
    • December 2010
    • November 2010
    • October 2010
    • September 2010
    • August 2010
    • July 2010
    • June 2010
    • May 2010
    • April 2010
    • March 2010
  • Recent posts

    • Windows 7 home premium number
    • Motorola Olympus vs Motorola Dext 2- Available vs Upcoming
    • new mobile phones
    • Android maintains the lead over Apple and Windows
    • Samsung J700 Chrome- A complete all-rounder mobile phone
    • Latest HTC Mobile Phone – HTC Touch P4350 Smartphone
    • Tips to Work In Windows Operating System
    • Azeem Azam On The Truth Behind The Transfer Window
    • HTC Hero- Gadget with advanced features
    • Sony Ericsson X1 – the mobile phone that does mobile broadband

Copyright © 2013 17ebook! free ebooks! free download! All right reserved

  • Home
  • Forum
  • Terms and Privacy