大侠 observer模式的例子,谢谢

myself100 2009-12-04 04:06:52
比如用JAVA实现SOCKET通信,是不是有种更好的写法: 用observer模式,EVENT触发,消息通知?
求这方面的例子,不是要observer模式例子,而是和通信相关的例子
...全文
99 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
myself100 2009-12-08
  • 打赏
  • 举报
回复
up
NeverGiveUp2016 2009-12-06
  • 打赏
  • 举报
回复
把你帖子 从第二页 不起眼的地方顶起来,顶到第一页。
NeverGiveUp2016 2009-12-06
  • 打赏
  • 举报
回复
Test.java

package com.bjsxt.dp.observer;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;


class WakenUpEvent {
private long time;
private String loc;
private Child source;

public WakenUpEvent(long time, String loc, Child source) {
super();
this.time = time;
this.loc = loc;
this.source = source;
}
public long getTime() {
return time;
}
public void setTime(long time) {
this.time = time;
}
public String getLoc() {
return loc;
}
public void setLoc(String loc) {
this.loc = loc;
}
public Child getSource() {
return source;
}
public void setSource(Child source) {
this.source = source;
}
}

class Child implements Runnable {
private List<WakenUpListener> wakenUpListeners = new ArrayList<WakenUpListener>();

public void addWakenUpListener(WakenUpListener l) {
wakenUpListeners.add(l);
}

void wakeUp() {
for(int i=0; i<wakenUpListeners.size(); i++) {
WakenUpListener l = wakenUpListeners.get(i);
l.ActionToWakenUp(new WakenUpEvent(System.currentTimeMillis(), "bed", this));
}
}

public void run() {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}

this.wakeUp();
}

}

class Dad implements WakenUpListener {

public void ActionToWakenUp(WakenUpEvent wakenUpEvent) {
System.out.println("feed child");
}

}

class GrandFather implements WakenUpListener {

public void ActionToWakenUp(WakenUpEvent wakenUpEvent) {
System.out.println("hug child");
}

}

class Dog implements WakenUpListener {

public void ActionToWakenUp(WakenUpEvent arg0) {
System.out.println("wang wang ...");
}

}

interface WakenUpListener {
public void ActionToWakenUp(WakenUpEvent wakenUpEvent);
}

public class Test {
public static void main(String[] args) {
Child c = new Child();


String[] observers = PropertyMgr.getProperty("observers").split(",");

for(String s : observers) {
try {
c.addWakenUpListener((WakenUpListener)(Class.forName(s).newInstance()));
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}


new Thread(c).start();
}
}

class PropertyMgr {
private static Properties props = new Properties();

static {
try {
props.load(Test.class.getClassLoader().getResourceAsStream("com/bjsxt/dp/observer/observer.properties"));
} catch (IOException e) {
e.printStackTrace();
}
}

public static String getProperty(String key) {
return props.getProperty(key);
}
}

class CryEvent {
}

abstract class Event {

}

Test.java

package com.bjsxt.dp.observer.awt;

import java.util.ArrayList;
import java.util.List;


public class Test {
public static void main(String[] args) {
Button b = new Button();
b.addActionListener(new MyActionListener());
b.addActionListener(new MyActionListener2());
b.buttonPressed();
}
}

class Button {

private List<ActionListener> actionListeners = new ArrayList<ActionListener>();

public void buttonPressed() {
ActionEvent e = new ActionEvent(System.currentTimeMillis(),this);
for(int i=0; i<actionListeners.size(); i++) {
ActionListener l = actionListeners.get(i);
l.actionPerformed(e);
}
}

public void addActionListener(ActionListener l) {
actionListeners.add(l);
}
}

interface ActionListener {
public void actionPerformed(ActionEvent e);
}

class MyActionListener implements ActionListener {

public void actionPerformed(ActionEvent e) {
System.out.println("button pressed!");
}

}

class MyActionListener2 implements ActionListener {

public void actionPerformed(ActionEvent e) {
System.out.println("button pressed 2!");
}

}

class ActionEvent {

long when;
Object source;

public ActionEvent(long when, Object source) {
super();
this.when = when;
this.source = source;
}


public long getWhen() {
return when;
}

public Object getSource() {
return source;
}

}

TestFrame.java

package com.bjsxt.dp.observer.awt;

import java.awt.Button;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class TestFrame extends Frame {
public void launch() {
Button b = new Button("press me");
b.addActionListener(new MyActionListener());
b.addActionListener(new MyActionListener2());
this.add(b);
this.pack();

this.addWindowListener(new WindowAdapter(){

@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}

});

this.setVisible(true);
}

public static void main(String[] args) {
new TestFrame().launch();
}

private class MyActionListener implements ActionListener {

public void actionPerformed(ActionEvent e) {
System.out.println("button pressed!");
}

}

private class MyActionListener2 implements ActionListener {

public void actionPerformed(ActionEvent e) {
System.out.println("button pressed 2!");
}

}
}
myself100 2009-12-04
  • 打赏
  • 举报
回复
多谢,继续等待,肯定有人做过的~~
believefym 2009-12-04
  • 打赏
  • 举报
回复
有时候observer也不一定好写,因为你不知道去怎么捕获事件
模拟起来很简单,比如server.receiveMsg(),然后触发,实际上收到消息这个事件在代码里不知道怎么实现

等待阻塞也许实现起来更简单
纯属个人意见,也没啥这方面经验
内容概要:本文介绍了一个基于Java与Vue的垃圾邮件识别与过滤系统的设计与实现,采用前后端分离架构,后端使用Spring Boot实现邮件接收、文本预处理、特征提取、模型预测与规则过滤等功能,前端通过Vue构建可视化管理界面。系统融合朴素贝叶斯分类模型、TF-IDF特征工程、关键词风险评分与规则引擎,实现对垃圾邮件的高效识别与可解释性判定。通过文本清洗、中文分词、概率计算与多维度风险融合策略,系统能有效应对垃圾内容表达变异、语义重叠等问题,并支持人工反馈驱动的模型持续优化。; 适合人群:具备Java Web开发、Vue前端技术基础,熟悉基本机器学习算法(如朴素贝叶斯、逻辑回归)的1-3年经验研发人员,尤其适合从事内容安全、反欺诈、文本分类相关项目的开发者;; 使用场景及目标:①应用于企业邮件系统、客服工单、站内信等场景中的垃圾信息自动过滤;②构建可解释的智能风控体系,降低误判率与网络诈骗风险;③作为前后端分离、算法集成与系统闭环设计的综合实践案例;; 阅读建议:建议结合提供的代码示例深入理解文本清洗、词频统计、贝叶斯训练与风险融合逻辑,重点关注模型与规则协同工作机制,同时可通过扩展模块(如接入中文分词服务、深度学习模型)进一步提升系统能力。

62,620

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧