Analysis of object-oriented programming design patterns 23 B of the king Iterator glory at an angle

·

Application iterator pattern in the glory of the King of

·

Here Insert Picture Description

A brief

King of glory is to allow a team of more than competitive game, in which teamwork MOBA games are the most important players in qualifying in general a lot of time to open the black points on the relatively easy, than single-row player points much stronger, so many players in the glory of the king hall interface, you want to invite friends to play a game, then the player can view the QQ / micro-letter friends' status through the list of games in order to determine whether to invite friends to join the game. When friends in the game, the player can play a game together by the reservation mode; when offline friends, then the player can invite friends on-line through QQ / micro-channel, open with black.
These are application examples iterator pattern in the glory of the king. This example assumes that the player online and offline player information are stored in a different form, or presented to the player in the online assumed that the list of players and sorting the players show the display information in a different data structures are processed; when sequentially player wants to view the status of each buddy when, do not understand the above and other content in the form of relationship that exists between the internal system and they want to know can be learned friends' status and other information.

Code section:
here we designed three categories: UseSet, Players, Application. Wherein UseSet class contains linked lists, hash tables, and tree set. Application class using Iterative mode involved, Example HashSet class, i.e., a set of analog buddy status; examples HashSet class calls the iterator () returns an iterator, with the iterator analog traverse and treatment processes, and further We are handled accordingly to different situations online players. Examples of the Application class is responsible for creating Players and add to the collection contained in UseSet, UseSet offers Find Players Players instance by the name attribute method also provides a method of sorting by minute property of an instance of Players.

Second, the iterative mode (Iterator Pattern)

Iterator pattern appreciated:
highly summarized: to provide a method for accessing a sequential polymerization individual elements of the object, but does not need to expose the interior of the object representation.
Construction and related operations rational organization of data is an important aspect of programming, such as programming, often we use such as linked lists, hash tables and other data structures. Linked lists and hash tables and other data structures can be stored in the collection are a number of objects, the difference is pressing a different way to store objects, we hope that no matter what set of procedures should be allowed in a uniform way to traverse the object in the collection without the need to know how these objects are represented in the collection and storage.
Iterator pattern is mature mode through the collection of key iterator pattern is the task through the collection of objects to a called iterator .

Iterative mode structure of four roles:
set (Aggregate) : an interface, a predetermined set of the specific operation to be achieved;
specific set (ConcreteAggregate) : Specific examples of the collection is a set of interface classes implement particular set of pressing store Object certain structure. DETAILED set should have a method that returns a specific iterator for the collection;
iterator (the Iterator) : An interface specifies the method to traverse specific set;
specific iterator (ConcreteIterator) : implements iterator interface instance of the class, in particular when the method iterator through the collection of specified iterations implemented to ensure that the first call of the method, the object is to find a set of data structures set pressing, and each time to find a set of object reference immediate successor objects according to the storage structure of the obtained set of traversal and ensure the method sequentially calls may traverse the entire collection.

Iterator pattern UML class diagram:

Here Insert Picture Description

Iterator pattern of disadvantages:
advantages:
① user access iterator objects in the collection, without needing to know how these objects are represented in the collection and storage;
② a plurality of users can simultaneously use iterates over a collection;
③ meet the "opening - closing principle";
disadvantages:
due to the separation of duties iterative mode and stores data through the data, adding new classes need a corresponding increase in new polymerization iterator class, the class number of pairs increases, which to some extent the increased complexity of the system.

Iterator pattern applicable scene:
① allow users to access objects in a collection, but do not want to expose the structure of objects stored in the collection;
② hope for a different loop through the collection, providing a unified interface.

Third, to achieve iterator pattern configuration diagram of the code and the angle of the king of glory

FIG structure eclipse
Here Insert Picture Description

[Main function application (Application)]
Application.java

package angle_iteratorPattern;

import java.util.*;

public class Application {

	    public static void main(String[] args) {
	        System.out.println("");
	    	int friends=10;         //好友列表共10人
	        Collection<Online> set=new HashSet<Online>();  //集合对象
	        for(int i=1;i<=friends;i++){
	           if(i==7||i==8||i==9||i==10)
	             set.add(new Online(10,false));
	           else	
	             set.add(new Online(10,true)); 
	        }
	        Iterator<Online> iterator=set.iterator();       //迭代器
	        System.out.println("好友列表共有"+set.size()+"人");
	        int k=0; 
	        while(iterator.hasNext()){
	        	Online money=iterator.next();
	            k++; 
	            if(money.getIsTrue()==false){
	               iterator.remove();
	               k++;
	            }
	        }
	        System.out.println("好友列表在线"+set.size()+"人");  
	        System.out.println("------------------------------------------");  
	        
	        UseSet useSet=new UseSet(); 
	        useSet.addPlayers(new Players("爱喝爽歪歪    ","铂金Ⅳ           ",2));
	        useSet.addPlayers(new Players("韩信了你的邪","钻石Ⅲ            ",13));
	        useSet.addPlayers(new Players("沙滩、绅士   ","星耀Ⅰ              ",31));
	        useSet.addPlayers(new Players("崽种来打我呀","最强王者x23 ",16));
	        useSet.addPlayers(new Players("我想上分分   ","钻石Ⅴ             ",27));
	        useSet.addPlayers(new Players("峡谷一级演员","黄金Ⅱ             ",9));
	        System.out.println("在线好友按开局时长排列:");
	        useSet.printPlayersByMinutes();
	        System.out.println("------------------------------------------");  
	        
	        String n="峡谷一级演员";
	        System.out.println("查找名字为“"+n+"”的玩家:");
	        useSet.lookPlayers(n);
	        System.out.println("------------------------------------------");  
	        String s="油炸小可爱";
	        System.out.println("查找名字为“"+s+"”的玩家:");
	        useSet.lookPlayers(s);
	    }
	 
}
class Online{            //玩家在线状态
	   int value;
	   private boolean isTrue;
	   Online(int value,boolean b){
	      this.value=value;
	       isTrue=b;
	   } 
	   public boolean getIsTrue(){
	      return isTrue;
	   }
	   public int getValue(){
	      return value;
	   }
	}

UseSet class
UseSet.java

package angle_iteratorPattern;

import java.util.*; 

public class UseSet{
    LinkedList<Players> list;      //使用链表存放玩家对象
    //用一个散列表和一个树集存放链表中的对象
    Hashtable<String,Players> table;    
    TreeSet<Players> tree;          
    UseSet(){
       list=new LinkedList<Players>();
       table=new Hashtable<String,Players>();
       tree=new TreeSet<Players>(); 
    }
    public void addPlayers(Players player){
       list.add(player);
       update();
    }
    
    public void lookPlayers(String names){                 //使用散列表查询某个玩家状态信息
    	Players players=table.get(names);
    	if(players==null){
    		System.out.println("此人离线,您可通过QQ【邀请】他上线");
    	}
    	else{
            int minute=players.getMinute();
            System.out.println("开局"+minute+"分钟,可【预约】");
        }
    }

    public void printPlayersByMinutes(){              //通过树集将玩家按开局时长排序
          Iterator<Players> iterator=tree.iterator();
          while(iterator.hasNext()){
        	    Players player=iterator.next();
                String name=player.getName();
                String level=player.getLevel();
                int minute=player.getMinute();
                System.out.println("名字:"+name+"   段位:"+level+" 开局"+minute+"分钟");
       }
    }
    private void  update(){
       tree.clear(); 
       Iterator<Players> iterator=list.iterator();
       while(iterator.hasNext()){
    	   Players player=iterator.next();
           String name=player.getName();
           table.put(name,player);
           tree.add(player); 
       }
    }
}


Players like
Players.java

package angle_iteratorPattern;

import java.util.*; 

class Players implements Comparable{
    String name,level;     //段位星数,名字
    int minute=0;       //开局时长
    Players(String name,String level,int minute){
        this.name=name;
        this.level=level;
        this.minute=minute;
    }
    public int compareTo(Object b){       
    	Players plater=(Players)b;
        return (int)(this.minute-plater.minute);
    }
    public String getLevel(){
        return level;
    }
    public String getName(){
        return name;
    }
    public int getMinute(){
        return minute;
    }
}

Screenshot operating results

Here Insert Picture Description

More application design patterns in the glory of the king, please click on the My → Home

Welcome message, along with the exchange of learning

Thanks for reading

END

Published 11 original articles · won praise 156 · views 20000 +

Guess you like

Origin blog.csdn.net/IT_charge/article/details/105100206