Java设计模式-观察者模式

观察者模式:当对象间存在一对多关系时,则使用观察者模式(Observer Pattern)。比如,当一个对象被修改时,则会自动通知它的依赖对象。观察者模式属于行为型模式。

使用场景:

  • 一个抽象模型有两个方面,其中一个方面依赖于另一个方面。将这些方面封装在独立的对象中使它们可以各自独立地改变和复用。
  • 一个对象的改变将导致其他一个或多个对象也发生改变,而不知道具体有多少对象将发生改变,可以降低对象之间的耦合度。
  • 一个对象必须通知其他对象,而并不知道这些对象是谁。
  • 需要在系统中创建一个触发链,A对象的行为将影响B对象,B对象的行为将影响C对象……,可以使用观察者模式创建一种链式触发机制。
  • 比如,开发中经常遇到的添加删除数据库表数据,

 

当主表的某一条记录被删除时,通知从表对应的外键记录也应该被删除。一般情况下,代码逻辑如下:

void 删除主表(int 主表ID){

      删除从表1(int id);

      删除从表2(int id);

      删除从表3(int id);

}

像web开发用到的ssm框架,代码函数随意写,甚至不知道写了多少删除主表的方法。程序员A写了一个删除主表方法,里面没写删除从表2,程序员B也可以写一个删除主表的方法,里面没有删除从表1的方法,这样就导致代码不规范,程序员C新来的,要调用删除主表的方法,达到从表1,2,3数据都被删除,但是不知道调用程序员A方法还是程序员B的方法,甚至新来的程序员C也自己写了一个删除主表的方法。随着业务增加,可能出现了从表4,主管这时候说让你去统一这删除主表的方法,只留下一个,我估计你心里颤抖,立马要出去首根烟压压惊,因为你不知道有多少个删除主表函数,也不知道哪个这功能的函数名是什么,你只能一步步走业务流程,慢慢跟着代码走。

你就会萌生一个念头,统一规范,写个函数,当执行删除主表函数时,自动通知其他删除从表函数。-------观察者模式

 

 

/**  
 * @文件名称: Subject.java
 * @类功能说明: 主题
 * @author: wfl
 * @创建时间: 2019年3月15日
 * @版本: V1.0
 */
public interface Subject {

	
	void add(UserInterface user);
	 
	void remove(UserInterface user);
	
	void notifyObserver();
}
public class UserImpl1 implements UserInterface{

	@Override
	public void update() {
		System.out.println("我UserImpl执行了");
	}

}
public class UserImpl2 implements UserInterface{


	@Override
	public void update() {
		System.out.println("我UserImpl2执行了");
	}

}
public interface UserInterface {

	public void update();
}
public class Watcher implements Subject{

	List<UserInterface> registers = null;
	
	
	/* (non-Javadoc)
	 * @see 接口设计.Subject#add(接口设计.UserInterface)
	 */
	
	/**   
	 * @名称:  Watcher 
	 * @作者:  wfl 
	 * @说明:  (这里用一句话描述这个方法的作用)   
	 * @param:    
	 * @throws   
	 */
	public Watcher() {
		registers = new ArrayList<UserInterface>();
	}
	
	@Override
	public void add(UserInterface user) {
		registers.add(user);
	}

	/* (non-Javadoc)
	 * @see 接口设计.Subject#remove(接口设计.UserInterface)
	 */
	@Override
	public void remove(UserInterface user) {
		int i = registers.indexOf(user);
		if(i>0)registers.remove(i);
	}

	/* (non-Javadoc)
	 * @see 接口设计.Subject#notifyObserver()
	 */
	@Override
	public void notifyObserver() {
		for (int i = 0; i < registers.size(); i++) {
			UserInterface user = registers.get(i);
			user.update();
		}
	}
}
public class TestMain {

	/**   
	 * @方法名称: main
	 * @作者: wfl
	 * @方法功能说明: TODO
	 * @param @param args
	 * @return: void      
	 * @throws   
	 */
	public static void main(String[] args) {
		UserInterface user1 = new UserImpl1();
		UserInterface user2 = new UserImpl2();
		UserInterface user3 = new UserImpl3();
		
		Watcher watcher = new Watcher();
		watcher.add(user1);
		watcher.add(user2);
		watcher.add(user3);
		
		watcher.notifyObserver();
		System.out.println("----------------------");
		watcher.remove(user2);
		watcher.notifyObserver();
	}

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java设计模式是一组经过实践验证的面向对象设计原则和模式,可以帮助开发人员解决常见的软件设计问题。下面是常见的23种设计模式: 1. 创建型模式(Creational Patterns): - 工厂方法模式(Factory Method Pattern) - 抽象工厂模式(Abstract Factory Pattern) - 单例模式(Singleton Pattern) - 原型模式(Prototype Pattern) - 建造者模式(Builder Pattern) 2. 结构型模式(Structural Patterns): - 适配器模式(Adapter Pattern) - 桥接模式(Bridge Pattern) - 组合模式(Composite Pattern) - 装饰器模式(Decorator Pattern) - 外观模式(Facade Pattern) - 享元模式(Flyweight Pattern) - 代理模式(Proxy Pattern) 3. 行为型模式(Behavioral Patterns): - 责任链模式(Chain of Responsibility Pattern) - 命令模式(Command Pattern) - 解释器模式(Interpreter Pattern) - 迭代器模式(Iterator Pattern) - 中介者模式(Mediator Pattern) - 备忘录模式(Memento Pattern) - 观察者模式(Observer Pattern) - 状态模式(State Pattern) - 策略模式(Strategy Pattern) - 模板方法模式(Template Method Pattern) - 访问者模式(Visitor Pattern) 4. 并发型模式(Concurrency Patterns): - 保护性暂停模式(Guarded Suspension Pattern) - 生产者-消费者模式(Producer-Consumer Pattern) - 读写锁模式(Read-Write Lock Pattern) - 信号量模式(Semaphore Pattern) - 线程池模式(Thread Pool Pattern) 这些设计模式可以根据问题的特点和需求来选择使用,它们提供了一些可复用的解决方案,有助于开发高质量、可维护且易于扩展的软件系统。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值