Java design pattern: from the [Warcraft, Starcraft, DOTA formation] analysis Iterator Pattern

Source: Internet
Author: User
Tags dota

In real-time strategic games and DOTA, we can select more troops to form a team. In Starcraft 1 and Warcraft 3, the maximum number of units of a team is 12. When we select a team, we can command them to move or attack in any direction, instead of controlling our units one by one. In the program, how do we implement the "Group" command to this team? The first thought was a loop-adding every unit in the team to a list, writing a for loop, accessing every Member in the list in sequence, and asking them to receive commands.

This time, I will introduce the iterator mode. The iterator is a familiar term for many people. It is also called the cursor mode. It can provide a sequence to access every element in a collection object, without exposing the internal representation of this object. To put it simply, for a set Team, we only need to use next to obtain the next element in the set and use hasNext to determine whether the set still has the next element, you can traverse all the elements in the Team set.

In the following example, assume that I have three Hero units, namely luxfa, Karl, and naivenmoore. Now I have added them to a team and traversed them to output their names:

Import java. util. Arrays; import java. util. List; interface Iterator
 
  
{T next (); boolean hasNext () ;}class Unit {private String name; public Unit (String name) {this. name = name;} public String getName () {return name;} public void setName (String name) {this. name = name ;}} class Team
  
   
{List
   
    
Members; int cursor = 0; public Iterator
    
     
GetIterator () {return new Iterator
     
      
() {Public T next () {T result = members. get (cursor); cursor ++; return result;} public boolean hasNext () {return cursor <members. size () ;};} public Team (List
      
        Members) {this. members = members;} class IteratorExample {public static void main (String [] args) throws InterruptedException {Unit u1 = new Unit ("Luxi Fa "); unit u2 = new Unit ("Karl"); Unit u3 = new Unit ("Nai Wen Moore"); List
       
         Units = Arrays. asList (u1, u2, u3); Team
        
          Team = new Team
         
           (Units); Iterator
          
            Iter = team. getIterator (); // get a new iterator while (iter. hasNext () {System. out. println (iter. next (). getName ());}}}
          
         
        
       
      
     
    
   
  
 

As you can see, the iterator interface mainly defines two methods: next () is to return the next element, and hasNext () returns whether there is a next element. In the Team class, we return an anonymous iterator through getIterator (), which contains a cursor, which is actually the cursor of the members list in the Team class, each time next () of the iterator is used, the cursor will increase by 1. We can see in the main method that through the combination of while, hasNext () and next (), we can traverse every element in the team, so the output result of the program is:

Luxfa

Karl

Nevenmoore

The following describes several key issues:

1. Why write Iterator as an internal class? Because we want to use getIterator to return a new iterator every time (that is, the cursor of each iterator starts from 0 ).

2. Is this a robust iterator? Answer: No. A robust iterator requires that the insert and delete operations do not interfere with traversal. In this iterator, if members is added or deleted, the value of cursor does not change, therefore, traversal is disturbed. (For example, if an element is inserted before the cursor position, two identical elements are output during traversal ). The iterator is the most widely used in foreach statements in Java and C, in addition, they are not allowed to add or delete elements to or from the objects in the original set when traversing through the iterator using the foreach statement, because this will interfere with the iterator.

3. If you want to implement a compatible foreach statement for a class in Java, this class must inherit Iterable Interface, which requires this class to have an iterator () method to return an Iterator Iterator. Similarly, to implement a class-compatible foreach statement in C #, you must inherit the IEnumerable This class must implement GetEnumerator () to return an IEnumerator iterator. They only have different names and have the same purpose and meaning.

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.