linkin大话设计模式--观察者模式
linkin大话设计模式--观察者模式
观察者模式定义了对象间的一对多依赖关系,让一个或者多个观察者观察一个对象主题。当主题对象的状态发生改变的时候,系统能通知所有的依赖于此对象的观察者对象,从而能自动更新。
在观察者模式中,被观察的对象常常也被称为目标和主题,依赖的对象被称为观察者。
代码如下:
<strong><span style="font-size:14px;">import java.util.ArrayList;
import java.util.Iterator;
import java.util.List; import javax.swing.JFrame;
import javax.swing.JLabel; public class Product extends ObServable{
//定义2个属性
private String name;
private double price; public Product(){ } public Product(String name, double price) {
super();
this.name = name;
this.price = price;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
notifyObServer(name);
} public double getPrice() {
return price;
} public void setPrice(double price) {
this.price = price;
notifyObServer(price);
} public static void main(String[] args) {
//创建一个被观察者
Product product = new Product("电视机",250);
//创建2个观察者 观察2个属性
NameObServer nameObServer = new NameObServer();
PriceObServer priceObServer = new PriceObServer();
//在被观察者对象上注册这个观察者
product.registObServer(nameObServer);
product.registObServer(priceObServer);
product.setName("电脑");
product.setPrice(1L);
product.setName("手机");
product.setPrice(2L);
} } //被观察者的抽象父类
abstract class ObServable{
List<ObServer> obServers = new ArrayList<ObServer>(); public void registObServer(ObServer o){
obServers.add(o);
} public void removeObServer(ObServer o){
obServers.remove(o);
} public void notifyObServer(Object value){
for(Iterator it = obServers.iterator();it.hasNext();){
ObServer o = (ObServer) it.next();
o.update(this, value);
}
}
} //观察者接口
interface ObServer{
public void update(ObServable o,Object arg);
} //下面提供2个观察者接口的实现 一个用来观察Product对象的name属性,一个用来观察Product对象的price属性
class NameObServer implements ObServer{ @Override
public void update(ObServable o, Object arg) {
if(arg instanceof String){
String name = (String) arg;
JFrame f = new JFrame("观察者");
JLabel j = new JLabel("名称改变为:"+name);
f.add(j);
f.pack();
f.setVisible(true);
System.out.println(NameObServer.class.getName()+"观察者:"+o+"物品的名称已经变为:"+name);
}else{
System.out.println(NameObServer.class.getName()+"类型不对,这里把这个逻辑过滤...");
}
}
} class PriceObServer implements ObServer{ @Override
public void update(ObServable o, Object arg) {
if(arg instanceof Double){
System.out.println(PriceObServer.class.getName()+"观察者:"+o+"物品的价格已经变为:"+arg);
}else{
System.out.println(PriceObServer.class.getName()+"类型不对,这里把这个逻辑过滤...");
}
} } </span></strong>
linkin大话设计模式--观察者模式的更多相关文章
- linkin大话设计模式--常用模式总结
linkin大话设计模式--常用模式总结 一,常用设计模式定义 Abstract Factory(抽象工厂模式):提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类. Adapter( ...
- linkin大话设计模式--单例模式
linkin大话设计模式 开文前先弱弱的问一句:什么是设计模式?我在研究java2ee的时候有研究过,在学js的时候也有看到.设计模式的概念最早源于建筑设计大师<建筑的永恒算法>一书,它表 ...
- linkin大话设计模式--适配器模式
linkin大话设计模式--适配器模式 大家知道,在java中只允许单继承,但是在实际问题中往往都需要多继承,java引入了接口这一概念.(一个类可以实现多个接口) 由于接口中都是抽象方法,那么我们在 ...
- linkin大话设计模式--建造模式
linkin大话设计模式--建造模式 建造模式是对象的创建模式,可以讲一个产品的内部表象与产品的生成过程分割开来,从而可以使一个建造过程生成具有不同的内部表象的产品对象. 建造模式的结构: 抽象建造者 ...
- linkin大话设计模式--模板方法模式
linkin大话设计模式--模板方法模式 准备一个抽象类,将部分逻辑以具体方法的形式实现,然后申明一些抽象方法来迫使子类实现剩余的逻辑.不同的子类可以以不同的方式实现这些抽象方法,从而对剩余的逻辑有不 ...
- linkin大话设计模式--桥接模式
linkin大话设计模式--桥接模式 桥接模式是一种结构化模式,他主要应对的是:由于实际的需要,某个类具有2个或者2个以上维度的变化,如果只是使用继承将无法实现功能,或者会使得设计变得相当的臃肿.我们 ...
- linkin大话设计模式--门面模式
linkin大话设计模式--门面模式 随着系统的不断改进和开发,他们会变得越来越复杂,系统会生成大量的类,这使得程序的流程更加难以理解.门面模式可以为这些类提供一个简易的接口,从而简化访问这些类的复杂 ...
- linkin大话设计模式--策略模式
linkin大话设计模式--策略模式 Strategy [ˈstrætədʒi] 策略 策略模式用于封装系列的算法,这些算法通常被封装在一个称为Context的类中,客户端程序可以自由的选择任何一种 ...
- linkin大话设计模式--命令模式
linkin大话设计模式--命令模式 首先考虑一种应用情况,某个方法需要完成某一个功能,这个功能的大部分功能已经确定了,但是有可能少量的步骤没法确定,必须等到执行这个方法才可以确定. 也就是说,我们写 ...
随机推荐
- fgets的用法
fgets的用法: char *fgets(char *s, int size, FILE *stream); fgets() reads in at most one les ...
- AI时代:推荐引擎正在塑造人类
We shape our tools and afterwards our tools shape us. ------Marshall McLuhan 麦克卢汉说:"我们塑造了工具,反过来 ...
- ogg的孩子-无损音频编解码flac
flac是一款无损的音频压缩编码,它的特点是对音频文件进行无损压缩,目前是被很多软件及智能硬件产品所支持. 从技术上来讲,该编解码的优点还是十分明显的,无损压缩,策略灵活,解码快速,硬件支持等特点都是 ...
- 有关python下二维码识别用法及识别率对比分析
最近项目中用到二维码图片识别,在python下二维码识别,目前主要有三个模块:zbar .zbarlight.zxing. 1.三个模块的用法: #-*-coding=utf-8-*- import ...
- java struts学习-拦截器
引言: Struts2拦截器,每个拦截器类只有一个对象实例,即采用单例模式,所有引用这个拦截器的Action都共享这一拦截器类的实例,因此,在拦截器中如果使用类变量,要注意同步问题. • ...
- 利用cmd制作一句话图片马
先cd进在图片和一句话木马里面的文件夹 假设选择我的图片是:1.jpg 一句话是:2.php 命令:copy 1.jpg/a+2.php/b 生成的图片马
- Git分支-分支简介
源地址:https://git-scm.com/book/zh/ch3-1.html 几乎所有的版本控制系统都以某种形式支持分支. 使用分支意味着你可以把你的工作从开发主线上分离开来,以免影响开发主线 ...
- 【经验分享】安装VirtualBox的时候遇到的问题
错误代码: Failed to instantiate CLSID_VirtualBox w/ IVirtualBox, but CLSID_VirtualBox w/ IUnknown works. ...
- Vijos P1035 贪婪的送礼者【模拟】
贪婪的送礼者 描述 对于一群要互送礼物的朋友,你要确定每个人送出的礼物比收到的多多少. 在这一个问题中,每个人都准备了一些钱来送礼物,而这些钱将会被平均分给那些将收到他的礼物的人. 然而,在任何一群朋 ...
- [bzoj2574] [Poi1999]Store-Keeper
坑啊.. 膜了半天byvoid大爷的题解.https://www.byvoid.com/blog/poi-1999-mag/?replytocom=1335/ 一开始从人的位置bfs一波,看看能走到初 ...