扩展已知类的功能

论坛 期权论坛 脚本     
匿名技术用户   2021-1-2 20:21   11   0

扩展已知类的功能

1.采用子类

缺点:
a、与具体类相关联:
如果继承了该类,那么使用范围就会被固定
继承了Connection,只能在mysql数据库中使用,在其他数据库就使用不了了
b、有些不能继承
c、信息不全

2.采用包装类(装饰者设计模式)

装饰者设计模式的步骤:
1. 在装饰类的内部维护一个被装饰类的引用。
2. 让装饰类有一个共同的父类或者是父接口。
采用包装类扩展已知类的功能
口诀:
a. 写一个类,实现和被包装类相同的接口 (使他们具有相同的行为)
b. 创建一个实例变量,引用被包装类对象 (最好做到与具体类无关)
c. 编写一个构造函数,传入被包装类对象 (注入: DI)
d. 对于需要改写的方法,写自己的代码
e. 对于不需要改写的方法,引用被包装类的对象的对应方法即可

3.静态代理

Person:
package com.hcx.proxy;

public class Person {

 public void eat() {
  System.out.println("吃饭");
 }
 
 
 public void sleep(){
  System.out.println("睡觉");
 }
}

ProxyPerson(代理类):
package com.hcx.proxy;

public class ProxyPerson {

 private Person p ;
 
 public ProxyPerson(Person p) {
  this.p = p ;
 }
 
 public void eat() {
  p.eat() ;
 }
 
 
 public void sleep(){
  p.sleep() ;
  System.out.println("睡觉时间: " + System.nanoTime());
 }
 
}
Test:
package com.hcx.proxy;

public class Test {

 public static void main(String[] args) {
  ProxyPerson p = new ProxyPerson(new  Person()) ;
  
  p.eat() ;
  p.sleep() ;
 }
}

4.动态代理

Human:(Interface):
package com.hcx.proxy1;

public interface Human {

 public void eat() ;
 
 public void sing(float money) ;
 
 public void dance(float money) ;
}
SpringBrother:(来学习的人,实现humen接口)
package com.hcx.proxy1;

public class SpringBrother implements Human {

 @Override
 public void eat() {
  System.out.println("吃饭中......");
 }

 @Override
 public void sing(float money) {
  System.out.println("拿到钱: " + money + "开唱");
 }

 @Override
 public void dance(float money) {
  System.out.println("拿到钱: " + money + "跳舞");
 }

}
ProxyHuman:(代理的人,也要实现human接口)
package com.hcx.proxy1;

//模拟动态代理的原理
public class ProxyHuman implements Human {
 
 private Human man ;
 
 public ProxyHuman(Human man) {
  this.man = man ;
 }

 @Override
 public void eat() {
  man.eat() ;
 }

 @Override
 public void sing(float money) {
  if(money > 1000)
   man.sing(money/2) ;
 }

 @Override
 public void dance(float money) {
  if(money > 2000)
   man.sing(money/2) ;
 }

}
Test:
package com.hcx.proxy1;

public class Test {

 /**
  * @param args
  */
 public static void main(String[] args) {
//  Human man = new SpringBrother() ;
//  
//  man.eat() ;
//  man.sing(100) ;
//  man.dance(200) ;
  
  Human man = new ProxyHuman(new SpringBrother()) ;
  
  man.eat() ;
  man.sing(2000) ;
  man.dance(4000) ;
  
 }

}

基于接口的动态代理

Human:
package com.hcx.proxy2;

public interface Human {

 public void eat() ;
 
 public void sing(float money) ;
 
 public void dance(float money) ;
}
SpringBrother:
package com.hcx.proxy2;

public class SpringBrother implements Human {

 @Override
 public void eat() {
  System.out.println("吃饭中......");
 }

 @Override
 public void sing(float money) {
  System.out.println("拿到钱: " + money + "开唱");
 }

 @Override
 public void dance(float money) {
  System.out.println("拿到钱: " + money + "跳舞");
 }

}
Test:
package com.hcx.proxy2;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

//基于接口的动态代理
public class Test {

 /**
  * @param args
  */
 public static void main(String[] args) {
//  Human man = new SpringBrother() ;
//  
//  man.eat() ;
//  man.sing(100) ;
//  man.dance(200) ;
  
  final SpringBrother sb = new SpringBrother() ;
  
  Human man = (Human)Proxy.newProxyInstance(sb.getClass().getClassLoader(),
    sb.getClass().getInterfaces(), 
    new InvocationHandler() {
     
    /**
     * invoke(Object proxy, Method method, Object[] args)
     * proxy: 代理人
     * method: 代理的方法
     * args: 方法的参数
     */
     @Override
     public Object invoke(Object proxy, Method method, Object[] args)
       throws Throwable {
      if(method.getName().equals("sing")){
       //说明要唱歌
       float money = (Float)args[0] ;
       if(money > 1000){
        Object retVal = method.invoke(sb, money/2) ;
        return retVal;
       }else
        return null ;
      }
      if(method.getName().equals("dance")){
       //说明要唱歌
       float money = (Float)args[0] ;
       if(money > 2000){
        Object retVal = method.invoke(sb, money/2) ;
        return retVal ;
       }else
        return null ;
      }
      Object ret =method.invoke(sb, args) ;
      return ret ;
      
     }
    }) ;
  
  man.eat() ;
  man.sing(1500) ;
  man.dance(2500) ;
 }

}

基于子类的动态代理:

采用第三方jar包实现动态代理:
cglib-nodep-2.1_3.jar
SpringBrother:
package com.hcx.proxy3;

public class SpringBrother {

 public void eat() {
  System.out.println("吃饭中......");
 }

 public void sing(float money) {
  System.out.println("拿到钱: " + money + "开唱");
 }

 public void dance(float money) {
  System.out.println("拿到钱: " + money + "跳舞");
 }

}
Test:
package com.hcx.proxy3;

import java.lang.reflect.Method;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

//基本子类的动态代理
public class Test {

 public static void main(String[] args) {
  /**
   * Enhancer.create(type, callback): //创建一个代理对象
   * type:是被代理对象的类型
   * callback: 相当于Proxy类中的 invocationHandler 代理类要做的事
   */
  final SpringBrother sb = new SpringBrother() ;
  SpringBrother h =  (SpringBrother)Enhancer.create(SpringBrother.class, new MethodInterceptor(){
  //创建的代理类的类型也是SpringBrother类型,因为在创建代理类的时候是以子类的形式创建的 
   //即创建的代理对象是被代理的子类
   @Override
   public Object intercept(Object arg0, Method method, Object[] args,
     MethodProxy arg3) throws Throwable {
    if(method.getName().equals("sing")){
     //说明要唱歌
     float money = (Float)args[0] ;
     if(money > 1000){
      Object retVal = method.invoke(sb, money/2) ;
      return retVal;
     }else
      return null ;
    }
    if(method.getName().equals("dance")){
     //说明要唱歌
     float money = (Float)args[0] ;
     if(money > 2000){
      Object retVal = method.invoke(sb, money/2) ;
      return retVal ;
     }else
      return null ;
    }
    Object ret =method.invoke(sb, args) ;
    return ret ;
   }
  }) ;
  
  
  h.eat() ;
  h.sing(1500) ;
  h.dance(2500) ;
 }
}









分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:7942463
帖子:1588486
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP