当前位置: 移动技术网 > 移动技术>移动开发>Android > Android观察者模式实例分析

Android观察者模式实例分析

2019年07月24日  | 移动技术网移动技术  | 我要评论

本文实例讲述了android观察者模式。分享给大家供大家参考。具体分析如下:

一、环境:

主机:win8
开发环境:eclipse

二、说明:

1.打开sd卡中的xml文件,如果不存在,这新建一个,并写入默认配置
2.读取xml文件
3.config_info.java为配置信息数据结构
4.if_config.java为配置类的存取接口,其他类可以通过此接口直接获取配置信息
5.if_subject_config.java为观察者模式目标类接口
6.if_observer_config.java为观察者模式观察者类接口
7.config.java为配置类,完成1,2两部工作,同时是观察者模式的目标类,一旦配置信息由变化着通知观察者类
8.testclass.java为观察者模式的观察者
通过存取接口+观察者模式可以实现松耦合的设计。

三、xml文件格式:

<?xml version="1.0" encoding="utf-8" standalone="true"?> 
-<config> 
<title>远程视频会见系统</title> 
<local_port>12600</local_port> 
<schedule_service_ip>10.58.1.59</schedule_service_ip>
<schedule_service_port>12601</schedule_service_port> 
</config>

四、源代码:

config_info.java:

/** 
 * 配置信息数据类型 
 * 新建时间:2014/12/8 by jdh 
 */ 
package com.example.helloanychat; 
public class config_info { 
 //标题 
 public string title; 
 //本机ip 
 public string local_ip; 
 //本机端口 
 public int local_port; 
 //调度服务器ip 
 public string schedule_server_ip; 
 //调度服务器端口 
 public int schedule_server_port; 
}

if_config.java:

/** 
 * 接口:配置类,读写 
 * 新建时间:2014/12/8 by jdh 
 */ 
package com.example.helloanychat; 
public interface if_config { 
 public config_info get_config_info(); 
}

if_subject_config.java:

/** 
 * 接口:配置类,观察者模式:目标 
 * 新建时间:2014/12/8 by jdh 
 */ 
package com.example.helloanychat; 
public interface if_subject_config { 
 public void register_observer(if_observer_config observer); 
 public void remove_observer(if_observer_config observer); 
 public void notify_observer(); 
}

if_observer_config.java:

/** 
 * 接口:配置类,观察者模式:观察者 
 * 新建时间:2014/12/8 by jdh 
 */ 
package com.example.helloanychat; 
public interface if_observer_config { 
 public void update(config_info info); 
}

config.java:

/** 
 * 配置信息类 
 * 新建日期:2014/12/8 by jdh 
 * 修改日期:2014/12/9 by jdh 
 */ 
package com.example.helloanychat; 
import java.io.file; 
import java.io.fileinputstream; 
import java.io.fileoutputstream; 
import java.io.ioexception; 
import java.io.stringwriter; 
import java.net.inet6address; 
import java.net.inetaddress; 
import java.net.networkinterface; 
import java.net.socketexception; 
import java.util.arraylist; 
import java.util.enumeration; 
import java.util.list; 
import java.util.timer; 
import java.util.timertask; 
import javax.xml.parsers.documentbuilder; 
import javax.xml.parsers.documentbuilderfactory; 
import android.os.environment; 
import android.util.log; 
import org.w3c.dom.document; 
import org.w3c.dom.element; 
import org.w3c.dom.nodelist; 
import org.xmlpull.v1.xmlpullparserfactory; 
import org.xmlpull.v1.xmlserializer; 
public class config implements if_config,if_subject_config { 
 //配置信息 
 private config_info info = new config_info(); 
 //存储观察者的列表 
 private list<if_observer_config> observers = new arraylist<if_observer_config>(); 
 //定时器 
 private timer timer_work = new timer(); 
 //工作间隔,单位:ms 
 private final int interval_work = 5000; 
 /** 
 * 构造函数 
 */ 
 public config() { 
 //生成配置信息 
 generate_config_info(); 
 //创建定时线程 
 timer_work.schedule(new task(),interval_work,interval_work);
 // 定时任务 
 } 
 //接口:读写 
 @override 
 public config_info get_config_info() { 
 return info; 
 } 
 //读写,观察者模式:目标 
 @override 
 public void register_observer(if_observer_config observer) { 
 observers.add(observer); 
 } 
 @override 
 public void remove_observer(if_observer_config observer) { 
 int index = observers.indexof(observer); 
 if (index >= 0) { 
  observers.remove(observer); 
 } 
 } 
 @override 
 public void notify_observer() { 
 for (int i = 0; i < observers.size(); i++) { 
  if_observer_config o = (if_observer_config) observers.get(i); 
  o.update(info); 
 } 
 } 
 /** 
 * 得到本机ip地址 
 * @return 本机ip地址 
 */ 
 private string getlocalipaddress() { 
 try { 
  for (enumeration<networkinterface> en = networkinterface 
   .getnetworkinterfaces(); en.hasmoreelements();) { 
  networkinterface intf = en.nextelement(); 
  for (enumeration<inetaddress> enumipaddr = intf 
   .getinetaddresses(); enumipaddr.hasmoreelements();) { 
   inetaddress inetaddress = enumipaddr.nextelement(); 
   //if (!inetaddress.isloopbackaddress()) { 
   if (!inetaddress.isloopbackaddress() && !(inetaddress instanceof inet6address)) { 
   return inetaddress.gethostaddress().tostring(); 
   } 
  } 
  } 
 } catch (socketexception ex) { 
  log.e("wifipreference ipaddress", ex.tostring()); 
 } 
 return null; 
 } 
 /** 
 * 生成xml配置文件的string数据流 
 * config_info的本机ip信息不会保存 
 * @param info:配置信息 
 * @return xml的string数据流 
 */ 
 private string produce_xml_string(config_info info) { 
 stringwriter stringwriter = new stringwriter(); 
 try { 
  // 获取xmlserializer对象 
  xmlpullparserfactory factory = xmlpullparserfactory.newinstance(); 
  xmlserializer xmlserializer = factory.newserializer(); 
  // 设置输出流对象 
  xmlserializer.setoutput(stringwriter); 
  //开始标签 
  xmlserializer.startdocument("utf-8", true); 
  xmlserializer.starttag(null, "config"); 
  //标题 
  xmlserializer.starttag(null, "title"); 
  xmlserializer.text(info.title); 
  xmlserializer.endtag(null, "title"); 
  //本机端口 
  xmlserializer.starttag(null, "local_port"); 
  xmlserializer.text(integer.tostring(info.local_port)); 
  xmlserializer.endtag(null, "local_port"); 
  //调度服务器ip 
  xmlserializer.starttag(null, "schedule_service_ip"); 
  xmlserializer.text(info.schedule_server_ip); 
  xmlserializer.endtag(null, "schedule_service_ip"); 
  //调度服务器端口 
  xmlserializer.starttag(null, "schedule_service_port"); 
  xmlserializer.text(integer.tostring(info.schedule_server_port)); 
  xmlserializer.endtag(null, "schedule_service_port"); 
  xmlserializer.endtag(null, "config"); 
  xmlserializer.enddocument(); 
 } catch (exception e) { 
  e.printstacktrace(); 
 } 
 return stringwriter.tostring(); 
 } 
 /** 
 * 工作任务:得到配置信息 
 */ 
 private void generate_config_info() 
 { 
 boolean ok; 
 file sd_path; 
 file file_cfg_dir; 
 file file_cfg; 
 fileoutputstream out; 
 string str; 
 fileinputstream in; 
 config_info info = new config_info(); 
 //得到本机ip地址 
 info.local_ip = getlocalipaddress(); 
 //获取sd卡目录 
 sd_path = environment.getexternalstoragedirectory(); 
 //判断文件夹是否存在 
 file_cfg_dir = new file(sd_path.getpath() + "//remote_meeting"); 
 if (!file_cfg_dir.exists() && !file_cfg_dir.isdirectory()) { 
  system.out.println("配置文件夹remote_meeting不存在!"); 
  ok = file_cfg_dir.mkdirs(); 
  if (ok) { 
  system.out.println("创建文件夹成功!");
  } else { 
  system.out.println("创建文件夹失败!");
  } 
 } 
 //判断配置文件是否存在 
 file_cfg = new file(file_cfg_dir.getpath(),"cfg.xml"); 
 if (!file_cfg.exists()) 
 { 
  system.out.println("配置文件cfg.xml不存在!"); 
  try { 
  file_cfg.createnewfile(); 
  system.out.println("创建文件cfg.xml成功!"); 
  //生成初始化的配置数据 
  try { 
   out = new fileoutputstream(file_cfg); 
   //保存默认配置 
   info.title = "远程视频会见系统"; 
   info.local_port = 12600; 
   info.schedule_server_ip = "10.58.1.59"; 
   info.schedule_server_port = 12601; 
   str = produce_xml_string(info); 
   out.write(str.getbytes()); 
   out.close(); 
   //保存本机ip 
   info.local_ip = info.local_ip; 
   //通知观察者 
   notify_observer(); 
  } catch (ioexception e) { 
   // todo auto-generated catch block 
   e.printstacktrace(); 
  } 
  } catch (ioexception e) { 
  // todo auto-generated catch block 
  e.printstacktrace(); 
  } 
 } 
 else 
 { 
  //解析xml文件 
  try { 
  in = new fileinputstream(file_cfg); 
  documentbuilderfactory factory = documentbuilderfactory.newinstance(); 
  documentbuilder builder = factory.newdocumentbuilder(); 
  document document = builder.parse(in); 
  // 获取根节点 
  element root = document.getdocumentelement(); 
  nodelist node = root.getchildnodes(); 
  //获得第1子节点:标题 
  info.title = node.item(0).getfirstchild().getnodevalue(); 
  //获得第2子节点:本机端口 
  info.local_port = integer.parseint(node.item(1).getfirstchild().getnodevalue()); 
  //获得第3子节点:调度服务器ip 
  info.schedule_server_ip = node.item(2).getfirstchild().getnodevalue(); 
  //获得第4子节点:调度服务器端口 
  info.schedule_server_port = integer.parseint(node.item(3).getfirstchild().getnodevalue()); 
  //判断配置信息是否变更 
  do 
  { 
   if (!info.title.equals(info.title)) 
   { 
   break; 
   } 
   if (!info.local_ip.equals(info.local_ip)) 
   { 
   break; 
   } 
   if (info.local_port != info.local_port) 
   { 
   break; 
   } 
   if (!info.schedule_server_ip.equals(info.schedule_server_ip)) 
   { 
   break; 
   } 
   if (info.schedule_server_port != info.schedule_server_port) 
   { 
   break; 
   } 
   //全部相同 
   return; 
  } while (false); 
  //赋值 
  info.title = info.title; 
  info.local_ip = info.local_ip; 
  info.local_port = info.local_port; 
  info.schedule_server_ip = info.schedule_server_ip; 
  info.schedule_server_port = info.schedule_server_port; 
  //通知观察者 
  notify_observer(); 
  } catch (exception e) { 
  e.printstacktrace(); 
  } 
 } 
 } 
 /** 
 * 定时器线程定时工作 
 */ 
 private class task extends timertask { 
 @override 
 public void run() { 
  generate_config_info(); 
 } 
 } 
}

testclass.java:

package com.example.helloanychat; 
public class testclass implements if_observer_config { 
 public testclass () { 
 } 
 @override 
 public void update(config_info info) { 
 system.out.printf("-------------更新数据:%s,%s,%d,%s,%d\n", 
 info.title,info.local_ip,info.local_port,info.schedule_server_ip,info.schedule_server_port); 
 } 
}

mainactivity:

testclass testclass = new testclass(); 
config config = new config(); 
meditip.settext(config.get_config_info().local_ip); 
config.register_observer(testclass); 

希望本文所述对大家的android程序设计有所帮助。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网