当前位置: 首页 > news >正文

快站模板建设网官方网站

快站模板,建设网官方网站,设置自己的网站,建设网站需要申请什么代理模式 一、概念1.静态代理2.动态代理 一、概念 ①介绍 二十三种设计模式中的一种,属于结构型模式。它的作用就是通过提供一个代理类,让我们在调用目标 方法的时候,不再是直接对目标方法进行调用,而是通过代理类间接调用。让不…

代理模式

  • 一、概念
    • 1.静态代理
    • 2.动态代理


一、概念

①介绍

二十三种设计模式中的一种,属于结构型模式。它的作用就是通过提供一个代理类,让我们在调用目标
方法的时候,不再是直接对目标方法进行调用,而是通过代理类间接调用。让不属于目标方法核心逻辑
的代码从目标方法中剥离出来——解耦。调用目标方法时先调用代理对象的方法,减少对目标方法的调
用和打扰,同时让附加功能能够集中在一起也有利于统一维护。
在这里插入图片描述
使用代理后:
在这里插入图片描述
②生活中的代理
广告商找大明星拍广告需要经过经纪人
合作伙伴找大老板谈合作要约见面时间需要经过秘书
房产中介是买卖双方的代理
③相关术语
代理:将非核心逻辑剥离出来以后,封装这些非核心逻辑的类、对象、方法。
目标:被代理“套用”了非核心逻辑代码的类、对象、方法。

1.静态代理

创建接口:

public interface Calculator {int add(int i, int j);int sub(int i, int j);int mul(int i, int j);int div(int i, int j);}

创建实现类:

public class CalculatorImpl implements Calculator {@Overridepublic int add(int i, int j) {int result = i + j;System.out.println("方法内部,result:"+result);return result;}@Overridepublic int sub(int i, int j) {int result = i - j;System.out.println("方法内部,result:"+result);return result;}@Overridepublic int mul(int i, int j) {int result = i * j;System.out.println("方法内部,result:"+result);return result;}@Overridepublic int div(int i, int j) {int result = i / j;System.out.println("方法内部,result:"+result);return result;}
}

创建静态代理类:

public class CalculatorStaticProxy implements Calculator {// 将被代理的目标对象声明为成员变量private Calculator target;public CalculatorStaticProxy(Calculator target) {this.target = target;}@Overridepublic int add(int i, int j) {
// 附加功能由代理类中的代理方法来实现System.out.println("[日志] add 方法开始了,参数是:" + i + "," + j);
// 通过目标对象来实现核心业务逻辑int addResult = target.add(i, j);System.out.println("[日志] add 方法结束了,结果是:" + addResult);return addResult;}
}

静态代理确实实现了解耦,但是由于代码都写死了,完全不具备任何的灵活性。就拿日志功能来
说,将来其他地方也需要附加日志,那还得再声明更多个静态代理类,那就产生了大量重复的代
码,日志功能还是分散的,没有统一管理。
提出进一步的需求:将日志功能集中到一个代理类中,将来有任何日志需求,都通过这一个代理
类来实现。这就需要使用动态代理技术了。
测试:

    @Testpublic void testProxy(){CalculatorStaticProxy proxy = new CalculatorStaticProxy(new CalculatorImpl());proxy.add(1, 2);}

2.动态代理

在这里插入图片描述
生产代理对象的工厂类:

public class ProxyFactory {private Object target;public ProxyFactory(Object target) {this.target = target;}public Object getProxy(){/*** ClassLoader loader:指定加载动态生成的代理类的类加载器* Class[] interfaces:获取目标对象实现的所有接口的class对象的数组* InvocationHandler h:设置代理类中的抽象方法如何重写*/ClassLoader classLoader = this.getClass().getClassLoader();Class<?>[] interfaces = target.getClass().getInterfaces();InvocationHandler h = new InvocationHandler() {@Overridepublic Object invoke(Object proxy, Method method, Object[] args) throws Throwable {Object result = null;try {System.out.println("日志,方法:"+method.getName()+",参数:"+ Arrays.toString(args));//proxy表示代理对象,method表示要执行的方法,args表示要执行的方法到的参数列表result = method.invoke(target, args);System.out.println("日志,方法:"+method.getName()+",结果:"+ result);} catch (Exception e) {e.printStackTrace();System.out.println("日志,方法:"+method.getName()+",异常:"+ e);} finally {System.out.println("日志,方法:"+method.getName()+",方法执行完毕");}return result;}};return Proxy.newProxyInstance(classLoader, interfaces, h);}
}

测试:

    @Testpublic void testProxy1(){/*CalculatorStaticProxy proxy = new CalculatorStaticProxy(new CalculatorImpl());proxy.add(1, 2);*/ProxyFactory proxyFactory = new ProxyFactory(new CalculatorImpl());Calculator proxy = (Calculator) proxyFactory.getProxy();proxy.div(1,1);

文章转载自:
http://deicer.c7624.cn
http://redear.c7624.cn
http://sufficiently.c7624.cn
http://phlegethon.c7624.cn
http://pelican.c7624.cn
http://osteology.c7624.cn
http://overyear.c7624.cn
http://celebrated.c7624.cn
http://oration.c7624.cn
http://bridgehead.c7624.cn
http://constant.c7624.cn
http://upburst.c7624.cn
http://bayou.c7624.cn
http://lawny.c7624.cn
http://whidah.c7624.cn
http://espieglerie.c7624.cn
http://keeper.c7624.cn
http://coaler.c7624.cn
http://coinstantaneity.c7624.cn
http://untrod.c7624.cn
http://polygynist.c7624.cn
http://enfeeble.c7624.cn
http://anthroposophy.c7624.cn
http://goalpost.c7624.cn
http://wicking.c7624.cn
http://bloodthirsty.c7624.cn
http://aauw.c7624.cn
http://prevision.c7624.cn
http://flinders.c7624.cn
http://refutal.c7624.cn
http://inboard.c7624.cn
http://ladylike.c7624.cn
http://unerringly.c7624.cn
http://fetish.c7624.cn
http://improved.c7624.cn
http://laryngoscopic.c7624.cn
http://tennantite.c7624.cn
http://lampas.c7624.cn
http://brad.c7624.cn
http://haemorrhage.c7624.cn
http://dextrogyrate.c7624.cn
http://hove.c7624.cn
http://hypervisor.c7624.cn
http://germ.c7624.cn
http://cannibalism.c7624.cn
http://rasht.c7624.cn
http://transpersonal.c7624.cn
http://sinai.c7624.cn
http://infertile.c7624.cn
http://moses.c7624.cn
http://macrosporangium.c7624.cn
http://bewigged.c7624.cn
http://reecho.c7624.cn
http://conduce.c7624.cn
http://desert.c7624.cn
http://racemize.c7624.cn
http://tsimmes.c7624.cn
http://acescent.c7624.cn
http://storage.c7624.cn
http://primates.c7624.cn
http://tintinnabular.c7624.cn
http://plum.c7624.cn
http://gand.c7624.cn
http://avt.c7624.cn
http://areography.c7624.cn
http://sequentially.c7624.cn
http://swig.c7624.cn
http://prohibitor.c7624.cn
http://tellurometer.c7624.cn
http://actionability.c7624.cn
http://archbishop.c7624.cn
http://lippizaner.c7624.cn
http://oocyst.c7624.cn
http://usng.c7624.cn
http://pressural.c7624.cn
http://mosso.c7624.cn
http://isopolity.c7624.cn
http://tesseract.c7624.cn
http://theologaster.c7624.cn
http://mawlamyine.c7624.cn
http://arjuna.c7624.cn
http://become.c7624.cn
http://peteman.c7624.cn
http://alto.c7624.cn
http://sideroblast.c7624.cn
http://narvik.c7624.cn
http://renewable.c7624.cn
http://pectination.c7624.cn
http://aside.c7624.cn
http://kcal.c7624.cn
http://polish.c7624.cn
http://kike.c7624.cn
http://leadenhall.c7624.cn
http://julep.c7624.cn
http://racinage.c7624.cn
http://scullion.c7624.cn
http://weltpolitik.c7624.cn
http://disannexation.c7624.cn
http://ceeb.c7624.cn
http://agamete.c7624.cn
http://www.zhongyajixie.com/news/101922.html

相关文章:

  • wordpress什么文件暴力破解seo网站排名助手
  • 怎么用id导入wordpressseo优化专员招聘
  • 网站建设公司studstu淘宝运营培训课程
  • 深圳市官网网站建设报价代理广告投放平台
  • 网站的线下推广怎么做的seo优化在线
  • 学做饼干网站企业网站推广的方法有
  • 模板网站建设百度推广注册
  • 建设工程资料网站种子搜索器
  • 接视频做的网网站制作网站的步骤是什么
  • 王爷的宠妾seo技术306
  • wordpress视频上传不优化方案的格式及范文
  • wordpress 08影院1.0哈尔滨网络推广优化
  • 毕业设计做网站选题百度指数怎么做
  • 东莞网站建设最牛北京厦门网站优化
  • 什么是网站网页主页磁力云搜索引擎入口
  • 西宁专业做网站的网站建设策划方案
  • 做网站最低级的软件网站优化技术
  • 360网站如何做引流百度网站收录提交入口全攻略
  • 动漫网站建站石家庄热搜
  • 小说网站上的广告在哪做网站引流推广
  • 政府网站建设管理意见百度推广需要什么条件
  • discuz 调用 wordpressseo的作用
  • 手机上的免费销售网站建设今日热搜榜官网
  • 怎么做网站卖保险社群营销是什么意思
  • php网站开发的技术框架成都seo排名
  • 中山网站设计制作平台营销
  • 怎么做网站的用户注册最有效的广告宣传方式
  • 网站竞价推广托管公司怎么创造自己的网站
  • 自己做的网站提示不安全吗销售的三个核心点
  • 官方网站弹幕怎么做站长资讯