java 代理模式,观察者模式
代理模式1
- import <a href="http://lib.csdn.net/base/17" class='replace_word' title="Java EE知识库" target='_blank' style='color:#df3434; font-weight:bold;'>Java</a>.lang.reflect.InvocationHandler;
- import java.lang.reflect.Method;
- import java.lang.reflect.Proxy;
- public class ProxyTest {
- public static void main(String[] args) {
- SaleComputer saleComputer = new ComputerMaker();
- InvocationHandler handler = new SaleProxy(saleComputer);
- SaleComputer proxy = (SaleComputer)Proxy.newProxyInstance(saleComputer.getClass().getClassLoader(), saleComputer.getClass().getInterfaces(), handler);
- proxy.saleComputer("花花");
- }
- }
- interface SaleComputer{ //卖电脑
- public void saleComputer(String type);
- }
- class ComputerMaker implements SaleComputer{
- public void saleComputer(String type) {
- System.out.println("卖出一台 " + type +" 牌电脑!");
- }
- }
- class SaleProxy implements InvocationHandler{
- Object delegate;
- public SaleProxy(Object delegate) {
- this.delegate = delegate;
- }
- public Object invoke(Object proxy, Method method, Object[] args)
- throws Throwable {
- giveMouse();
- return method.invoke(delegate,args);
- }
- private void giveMouse(){
- System.out.println("送鼠标...............");
- }
- }
代理模式2
- public class ProxyTest1 {
- public static void main(String[] args) {
- SaleComputer1 saleComputer = new ComputerMaker1();
- SaleComputer1 proxy = new SaleProxy1(saleComputer);
- proxy.saleComputer("鸿润");
- }
- }
- interface SaleComputer1{ //接口
- public void saleComputer(String type);
- }
- class ComputerMaker1 implements SaleComputer1{//实现类
- public void saleComputer(String type) {
- System.out.println("卖出一台 " + type +" 牌电脑!");
- }
- }
- class SaleProxy1 implements SaleComputer1{ //代理类
- SaleComputer1 delegate;
- public SaleProxy1(Object delegate){
- this.delegate = (SaleComputer1)delegate;
- }
- public void saleComputer(String type) {
- this.giveMouse();
- delegate.saleComputer(type);
- }
- private void giveMouse(){
- System.out.println("送鼠标...............");
- }
- }
观察者模式:
- package com.model;
- import java.util.Observable;
- import java.util.Observer;
- /**
- Java的API为我们提供了Observer接口和Observable类来实现所谓观察者模式。
- Observable(可观察者)类允许在自身发生改变时,通知其它对象(实现接口Observer,观察者)。
- */
- public class TestObserver{
- public static void main(String[] args){
- Produce produce = new Produce();
- NameObserver nameObserver = new NameObserver();
- PriceObserver priceObserver = new PriceObserver();
- produce.addObserver(nameObserver);
- produce.addObserver(priceObserver);
- produce.setName("Apple");
- produce.setPrice(100);
- }
- }
- //一个可观察者
- class Produce extends Observable{
- private String name;
- private Integer price;
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- setChanged(); //设置变化点
- notifyObservers(name); //通知观察者
- }
- public Integer getPrice() {
- return price;
- }
- public void setPrice(Integer price) {
- this.price = price;
- setChanged();
- notifyObservers(price);
- }
- }
- //两个观察者
- class NameObserver implements Observer{
- public void update(Observable o, Object arg) {
- if(arg instanceof String){
- System.out.println("观察者观察到:产品名字已经改为: " + arg);
- }
- }
- }
- class PriceObserver implements Observer{
- public void update(Observable o, Object arg) {
- if(arg instanceof Integer){
- System.out.println("观察者观察到:价格已经改为: " + arg);
- }
- }
- }