ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

迭代器模式实例与解析---实例:电视机遥控器

2021-06-14 10:57:39  阅读:174  来源: 互联网

标签:return void CCTV public --- 实例 currentIndex obj 遥控器


电视机遥控器就是一个迭代器的实例,通过它可以实现对电视机频道集合的遍历操作,本实例我们将模拟电视机遥控器的实现。

public class Client
{
	public static void display(Television tv)
	{
		TVIterator i=tv.createIterator();
		System.out.println("电视机频道:");
		while(!i.isLast())
		{
			System.out.println(i.currentChannel().toString());
			i.next();
		}
	}
	
	public static void reverseDisplay(Television tv)
	{
		TVIterator i=tv.createIterator();
		i.setChannel(5);
		System.out.println("逆向遍历电视机频道:");
		while(!i.isFirst())
		{
			i.previous();
			System.out.println(i.currentChannel().toString());
		}
	}
	
	public static void main(String a[])
	{
		Television tv;
		tv=(Television)XMLUtil.getBean();
		display(tv);
		System.out.println("--------------------------");
		reverseDisplay(tv);
	}
}

 

public class SkyworthTelevision implements Television
{
	private Object[] obj={"CCTV-1","CCTV-2","CCTV-3","CCTV-4","CCTV-5","CCTV-6","CCTV-7","CCTV-8"};
	public TVIterator createIterator()
	{
		return new SkyworthIterator();
	}

	private class SkyworthIterator implements TVIterator
	{
	   	private int currentIndex=0;
	   	 	
		public void next()
		{
			if(currentIndex<obj.length)
			{
				currentIndex++;
			}
		}
		
		public void previous()
		{
			if(currentIndex>0)
			{
				currentIndex--;
			}
		}	
		
		public void setChannel(int i)
		{
			currentIndex=i;
		}
		
		
		public Object currentChannel()
		{
			return obj[currentIndex];
		}
		
		public boolean isLast()
		{
			return currentIndex==obj.length;
		}
		
		public boolean isFirst()
		{
			return currentIndex==0;
		}
	}
}
public class TCLTelevision implements Television
{
	private Object[] obj={"湖南卫视","北京卫视","上海卫视","湖北卫视","黑龙江卫视"};
	public TVIterator createIterator()
	{
		return new TCLIterator();
	}
   
	class TCLIterator implements TVIterator
	{
	   	private int currentIndex=0;
	   	 	
		public void next()
		{
			if(currentIndex<obj.length)
			{
				currentIndex++;
			}
		}
		
		public void previous()
		{
			if(currentIndex>0)
			{
				currentIndex--;
			}
		}	
		
		public void setChannel(int i)
		{
			currentIndex=i;
		}
		
		
		public Object currentChannel()
		{
			return obj[currentIndex];
		}
		
		public boolean isLast()
		{
			return currentIndex==obj.length;
		}
	
		public boolean isFirst()
		{
			return currentIndex==0;
		}
	}
}

 

public interface Television
{
	TVIterator createIterator();
}

 

public interface TVIterator
{
	void setChannel(int i);
	void next();
	void previous();
	boolean isLast();
	Object currentChannel();
    boolean isFirst();
}

 

import javax.xml.parsers.*;
import org.w3c.dom.*;
import org.xml.sax.SAXException;
import java.io.*;
public class XMLUtil
{
//该方法用于从XML配置文件中提取具体类类名,并返回一个实例对象
	public static Object getBean()
	{
		try
		{
			//创建文档对象
			DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();
			DocumentBuilder builder = dFactory.newDocumentBuilder();
			Document doc;							
			doc = builder.parse(new File("config.xml")); 
		
			//获取包含类名的文本节点
			NodeList nl = doc.getElementsByTagName("className");
            Node classNode=nl.item(0).getFirstChild();
            String cName=classNode.getNodeValue();
            
            //通过类名生成实例对象并将其返回
            Class c=Class.forName(cName);
	  	    Object obj=c.newInstance();
            return obj;
           }   
           	catch(Exception e)
           	{
           		e.printStackTrace();
           		return null;
           	}
		}
}
配置文件
<?xml version="1.0"?>
<config>
	<className>SkyworthTelevision</className>
</config>

 

标签:return,void,CCTV,public,---,实例,currentIndex,obj,遥控器
来源: https://blog.csdn.net/bueke/article/details/117898787

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有