迭代器模式实例与解析---实例:电视机遥控器
作者:互联网
电视机遥控器就是一个迭代器的实例,通过它可以实现对电视机频道集合的遍历操作,本实例我们将模拟电视机遥控器的实现。
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