Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1279766
  • 博文数量: 296
  • 博客积分: 285
  • 博客等级: 二等列兵
  • 技术积分: 2986
  • 用 户 组: 普通用户
  • 注册时间: 2010-03-06 22:04
个人简介

Linuxer, ex IBMer. GNU https://hmchzb19.github.io/

文章分类

全部博文(296)

文章存档

2020年(10)

2019年(15)

2018年(43)

2017年(79)

2016年(79)

2015年(58)

2014年(1)

2013年(8)

2012年(3)

分类: Java

2017-02-24 18:02:50

1.这是个比较古老的例子,用了4个函数来实现Iterator,现在Java 的Iterator 应该只要实现 hasNext(), next(), remove() 这三个函数就好了。

点击(此处)折叠或打开

  1. package iterator;

  2. import java.util.LinkedList;

  3. interface ISubject {
  4.     public IIterator CreateIterator();
  5. }

  6. interface IIterator {
  7.     void First();        //Reset to first element
  8.     String Next();        //get next element
  9.     Boolean IsDone();        //End of collection check
  10.     String CurrentItem();    //Retrieve Current Item
  11. }

  12. class Science implements ISubject {
  13.     private LinkedList<String> subjects;

  14.     public Science(){
  15.         subjects = new LinkedList<String>();
  16.         subjects.addLast("Math");
  17.         subjects.addLast("Comp. Sc.");
  18.         subjects.addLast("Physics");
  19.     }

  20.     @Override
  21.     public IIterator CreateIterator(){
  22.         return new ScienceIterator(subjects);
  23.     }

  24.     //Containing the ScienceIterator
  25.     public class ScienceIterator implements IIterator{
  26.         private LinkedList<String> subjects;
  27.         private int position;

  28.         public ScienceIterator(LinkedList<String> subjects){
  29.             this.subjects = subjects;
  30.             position = 0;
  31.         }

  32.         public void First(){
  33.             position = 0;
  34.         }

  35.         public String Next(){
  36.             return subjects.get(position++);
  37.         }

  38.         public Boolean IsDone(){
  39.             return position >= subjects.size();
  40.         }

  41.         public String CurrentItem(){
  42.             return subjects.get(position);
  43.         }
  44.     }
  45. }


  46. class Arts implements ISubject {
  47.     private String[] subjects;

  48.     public Arts(){
  49.         subjects = new String[2];
  50.         subjects[0] = "Bengali";
  51.         subjects[1] = "English";
  52.     }

  53.     public IIterator CreateIterator(){
  54.         return new ArtsIterator(subjects);
  55.     }


  56.     //Containing the ArtsIterator
  57.     public class ArtsIterator implements IIterator{
  58.         private String[] subjects;
  59.         private int position;

  60.         public ArtsIterator(String[] subjects){
  61.             this.subjects = subjects;
  62.             position = 0;
  63.         }

  64.         public void First(){
  65.             position = 0;
  66.         }

  67.         public String Next(){
  68.             return subjects[position++];
  69.         }

  70.         public Boolean IsDone(){
  71.             return position >= subjects.length;
  72.         }

  73.         public String CurrentItem(){
  74.             return subjects[position];
  75.         }
  76.     }
  77. }


  78. public class IteratorPattern {
  79.     public static void main(String[] args){
  80.         System.out.println("*** Iterator Pattern Demo ***");
  81.         ISubject Sc_subject = new Science();
  82.         ISubject Ar_subjects = new Arts();

  83.         IIterator Sc_iterator = Sc_subject.CreateIterator();
  84.         IIterator Ar_iterator = Ar_subjects.CreateIterator();

  85.         System.out.println("\nScience Subjects :");
  86.         Print(Sc_iterator);

  87.         System.out.println("\nArts subjects: ");
  88.         Print(Ar_iterator);
  89.     }

  90.     public static void Print(IIterator iterator){
  91.         while(!iterator.IsDone()){
  92.             System.out.println(iterator.Next());
  93.         }
  94.     }
  95. }


2.  这个例子比较好,从这里抄了一个例子,使用了一个private inner class 来实现的Iterator,我在某一本书上看说用private inner class 比public inner class 要好。
http://www.journaldev.com/1716/iterator-design-pattern-java

点击(此处)折叠或打开

  1. package iterator;

  2. import java.util.ArrayList;
  3. import java.util.List;

  4. enum ChannelTypeEnum{
  5.     ENGLISH,HINDI,FRENCH,ALL;
  6. }

  7. class Channel{
  8.     private double frequency;
  9.     private ChannelTypeEnum TYPE;

  10.     public Channel(double freq, ChannelTypeEnum TYPE){
  11.         this.TYPE= TYPE;
  12.         this.frequency = freq;
  13.     }

  14.     public double getFrequency(){
  15.         return frequency;
  16.     }

  17.     public ChannelTypeEnum getType(){
  18.         return TYPE;
  19.     }

  20.     @Override
  21.     public String toString(){
  22.         return "Frequery ="+this.frequency+", Type="+this.TYPE;
  23.     }
  24. }

  25. interface ChannelIterator{
  26.     public boolean hasNext();
  27.     public Channel next();
  28. }

  29. interface ChannelCollection{
  30.     public void addChannel(Channel c);
  31.     public void removeChannel(Channel c);
  32.     public ChannelIterator iterator(ChannelTypeEnum type);
  33. }

  34. class ChannelCollectionImpl implements ChannelCollection{
  35.     private List<Channel> channelList;

  36.     //Constructor
  37.     public ChannelCollectionImpl(){
  38.         channelList = new ArrayList<Channel>();
  39.     }

  40.     public void addChannel(Channel c){
  41.         this.channelList.add(c);
  42.     }

  43.     public void removeChannel(Channel c){
  44.         this.channelList.remove(c);
  45.     }

  46.     @Override
  47.     public ChannelIterator iterator(ChannelTypeEnum type){
  48.         return new ChannelIteratorImpl(type, this.channelList);
  49.     }

  50.     private class ChannelIteratorImpl implements ChannelIterator{
  51.         private ChannelTypeEnum type;
  52.         private List<Channel> channels;
  53.         private int position;

  54.         public ChannelIteratorImpl(ChannelTypeEnum ty, List<Channel> channelList){
  55.             this.type = ty;
  56.             this.channels = channelList;
  57.         }

  58.         @Override
  59.         public boolean hasNext(){
  60.             while(position < channels.size()){
  61.                 Channel c = channels.get(position);
  62.                 if(c.getType().equals(type) || type.equals(ChannelTypeEnum.ALL)){
  63.                     return true;
  64.                 }else
  65.                     position ++;
  66.             }
  67.             return false;
  68.         }

  69.         @Override
  70.         public Channel next(){
  71.             Channel c = channels.get(position);
  72.             position ++;
  73.             return c;
  74.         }
  75.     }
  76. }


  77. public class IteratorDemo {
  78.     public static void main(String[] args){
  79.         ChannelCollection channels = populateChannels();
  80.         ChannelIterator baseIterator = channels.iterator(ChannelTypeEnum.ALL);
  81.         while(baseIterator.hasNext()){
  82.             Channel c = baseIterator.next();
  83.             System.out.println(c.toString());
  84.         }
  85.         System.out.println("********");
  86.         //Channel Type Iterator
  87.         ChannelIterator englishIterator = channels.iterator(ChannelTypeEnum.ENGLISH);
  88.         while(englishIterator.hasNext()){
  89.             Channel c = englishIterator.next();
  90.             System.out.println(c.toString());
  91.         }
  92.     }

  93.     private static ChannelCollection populateChannels(){
  94.         ChannelCollection channels = new ChannelCollectionImpl();
  95.         channels.addChannel(new Channel(98.5, ChannelTypeEnum.ENGLISH));
  96.         channels.addChannel(new Channel(99.5, ChannelTypeEnum.HINDI));
  97.         channels.addChannel(new Channel(100.5, ChannelTypeEnum.FRENCH));
  98.         channels.addChannel(new Channel(101.5, ChannelTypeEnum.ENGLISH));
  99.         channels.addChannel(new Channel(102.5, ChannelTypeEnum.HINDI));
  100.         channels.addChannel(new Channel(103.5, ChannelTypeEnum.FRENCH));
  101.         channels.addChannel(new Channel(104.5, ChannelTypeEnum.ENGLISH));
  102.         channels.addChannel(new Channel(105.5, ChannelTypeEnum.HINDI));
  103.         channels.addChannel(new Channel(106.5, ChannelTypeEnum.FRENCH));
  104.         return channels;
  105.     }
  106. }

3. 记录下最近碰到的一个shell脚本出错的问题。
$1 在一个脚本调用其他脚本的过程中,如果有set 语句,会改变调用脚本的位置参数。
这是我要运行的脚本

点击(此处)折叠或打开

  1. #! /bin/bash -x

  2. echo "\$1 is $1"
  3. if [ -x "/etc/profile.d/sinit.sh" ];then
  4.   source /etc/profile.d/sinit.sh
  5. fi
  6. echo "\$1 is $1"
  7.   case "$1" in
  8.        start) echo "$1 is start";;
  9.        stop) echo "$1 is stop";;
  10.        *) echo "wrong coice";;
  11.   esac
这是另外一个被调用的脚本,当然其实这个应该算是个文本文件,而不能算是脚本,因为并没有#!(shebang), 当然在我实验的过程中,即使在第一行加上#!(shebang) ,
调用脚本的$1 仍然会被污染。不用.用source 也是一样。

点击(此处)折叠或打开

  1. /etc/profile.d/sinit.sh
  2. TMOUT=1800
  3. export TMOUT
  4. set autologout=30
请看执行, source 后$1就被污染了。

点击(此处)折叠或打开

  1. ./echo_env2.sh start
  2. + echo '$1 is start'
  3. $1 is start
  4. + '[' -x /etc/profile.d/sinit.sh ']'
  5. + source /etc/profile.d/sinit.sh
  6. ++ TMOUT=1800
  7. ++ export TMOUT
  8. ++ set autologout=30
  9. + echo '$1 is autologout=30'
  10. $1 is autologout=30
  11. + case "$1" in
  12. + echo 'wrong coice'
  13. wrong coice




阅读(530) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~