由这个代理对象控制对该对象的访问

最后都变了- 提交于 2019-12-05 15:45:45

什么是代理模式

代理模式就是为一个对象提供一个代理对象,由这个代理对象控制对该对象的访问。

理解代理模式,可以对照生活中的一些具体例子,比如房产中介、二手车交易市场、经纪人等。

为什么要用代理模式

通过使用代理模式,我们避免了直接访问目标对象时可能带来的一些问题,比如:远程调用,需要使用远程代理来帮我们处理一些网络传输相关的细节逻辑;可能需要基于某种权限控制对目标资源的访问,可以使用保护代理等。

总的来说,通过是用代理模式,我们可以控制对目标对象的访问,可以在真实方法被调用前或调用后,通过代理对象加入额外的处理逻辑。

代理模式分类

代理模式分为静态代理和动态代理。动态代理根据实现不同又可细分为JDK动态代理和cglib动态代理。

静态代理是由程序员创建或工具生成代理类的源码,再编译代理类。所谓静态也就是在程序运行前就已经存在代理类的字节码文件,代理类和委托类的关系在运行前就确定了。

动态代理是在实现阶段不用关心代理类,而在运行时动态生成代理类的。

静态代理

以房哥买房子为例,用代码实现静态代理。

https://weibo.com/ttarticle/p/show?id=2309404442674693275995
https://weibo.com/ttarticle/p/show?id=2309404442674605195295
https://weibo.com/ttarticle/p/show?id=2309404442674336760088
https://weibo.com/ttarticle/p/show?id=2309404442674261262589
https://weibo.com/ttarticle/p/show?id=2309404442674173182149
https://weibo.com/ttarticle/p/show?id=2309404442674093490337
https://weibo.com/ttarticle/p/show?id=2309404442673955078303
https://weibo.com/ttarticle/p/show?id=2309404442673875386529
https://weibo.com/ttarticle/p/show?id=2309404442673728585757
https://weibo.com/ttarticle/p/show?id=2309404442673644699783
https://weibo.com/ttarticle/p/show?id=2309404442673502093372
https://weibo.com/ttarticle/p/show?id=2309404442673409818706
https://weibo.com/ttarticle/p/show?id=2309404442673208492308
https://weibo.com/ttarticle/p/show?id=2309404442673019748491
https://weibo.com/ttarticle/p/show?id=2309404442672910696453
https://weibo.com/ttarticle/p/show?id=2309404442672776478910
https://weibo.com/ttarticle/p/show?id=2309404442672692592743
https://weibo.com/ttarticle/p/show?id=2309404442672612900934
https://weibo.com/ttarticle/p/show?id=2309404442672466100401
https://weibo.com/ttarticle/p/show?id=2309404442672390602875
https://weibo.com/ttarticle/p/show?id=2309404442672327688295
https://weibo.com/ttarticle/p/show?id=2309404442672252190915
https://weibo.com/ttarticle/p/show?id=2309404442672176693388
https://weibo.com/ttarticle/p/show?id=2309404442672071835825
https://weibo.com/ttarticle/p/show?id=2309404442671971172451
https://weibo.com/ttarticle/p/show?id=2309404442671895674904
https://weibo.com/ttarticle/p/show?id=2309404442671820177563
https://weibo.com/ttarticle/p/show?id=2309404442671732097179
https://weibo.com/ttarticle/p/show?id=2309404442671560130566
https://weibo.com/ttarticle/p/show?id=2309404442671480438827
https://weibo.com/ttarticle/p/show?id=2309404442671396552731
https://weibo.com/ttarticle/p/show?id=2309404442671316861025
https://weibo.com/ttarticle/p/show?id=2309404442671245557838
https://weibo.com/ttarticle/p/show?id=2309404442671165866041
https://weibo.com/ttarticle/p/show?id=2309404442671081980109
https://weibo.com/ttarticle/p/show?id=2309404442671010676834
https://weibo.com/ttarticle/p/show?id=2309404442670872264752
https://weibo.com/ttarticle/p/show?id=2309404442670792573026
https://weibo.com/ttarticle/p/show?id=2309404442670654160901
https://weibo.com/ttarticle/p/show?id=2309404442670570275015
https://weibo.com/ttarticle/p/show?id=2309404442670490583375
https://weibo.com/ttarticle/p/show?id=2309404442670415085683
https://weibo.com/ttarticle/p/show?id=2309404442670343782592
https://weibo.com/ttarticle/p/show?id=2309404442670255702271
https://weibo.com/ttarticle/p/show?id=2309404442670092124353
https://weibo.com/ttarticle/p/show?id=2309404442669865631756
https://weibo.com/ttarticle/p/show?id=2309404442669735608538
https://weibo.com/ttarticle/p/show?id=2309404442669660110981
https://weibo.com/ttarticle/p/show?id=2309404442669576224878
https://weibo.com/ttarticle/p/show?id=2309404442669492339023
https://weibo.com/ttarticle/p/show?id=2309404442669412647079
https://weibo.com/ttarticle/p/show?id=2309404442669328760845
https://weibo.com/ttarticle/p/show?id=2309404442669240680680
https://weibo.com/ttarticle/p/show?id=2309404442669160988819
https://weibo.com/ttarticle/p/show?id=2309404442668997411013
https://weibo.com/ttarticle/p/show?id=2309404442668926107773
https://weibo.com/ttarticle/p/show?id=2309404442668846416031
https://weibo.com/ttarticle/p/show?id=2309404442668762529937
https://weibo.com/ttarticle/p/show?id=2309404442668687032415
https://weibo.com/ttarticle/p/show?id=2309404442668594757798
https://weibo.com/ttarticle/p/show?id=2309404442668515066019
https://weibo.com/ttarticle/p/show?id=2309404442667982389545
https://weibo.com/ttarticle/p/show?id=2309404442667898503260
https://weibo.com/ttarticle/p/show?id=2309404442667823005760
https://weibo.com/ttarticle/p/show?id=2309404442667726536890
https://weibo.com/ttarticle/p/show?id=2309404442667651039353
https://weibo.com/ttarticle/p/show?id=2309404442667575542013
https://weibo.com/ttarticle/p/show?id=2309404442667495850136
https://weibo.com/ttarticle/p/show?id=2309404442667416158521
https://weibo.com/ttarticle/p/show?id=2309404442667332272260
https://weibo.com/ttarticle/p/show?id=2309404442667256774837
https://weibo.com/ttarticle/p/show?id=2309404442667177083134
https://weibo.com/ttarticle/p/show?id=2309404442667097391315
https://weibo.com/ttarticle/p/show?id=2309404442667021893672
https://weibo.com/ttarticle/p/show?id=2309404442666946396571
https://weibo.com/ttarticle/p/show?id=2309404442666866704683
https://weibo.com/ttarticle/p/show?id=2309404442666766041207
https://weibo.com/ttarticle/p/show?id=2309404442666686349314
https://weibo.com/ttarticle/p/show?id=2309404442666589880657
https://weibo.com/ttarticle/p/show?id=2309404442666480828433
https://weibo.com/ttarticle/p/show?id=2309404442666090758162
https://weibo.com/ttarticle/p/show?id=2309404442666011066424
https://weibo.com/ttarticle/p/show?id=2309404442665692299525
https://weibo.com/ttarticle/p/show?id=2309404442665612607503
https://weibo.com/ttarticle/p/show?id=2309404442665516138682
https://weibo.com/ttarticle/p/show?id=2309404442665436446975
https://weibo.com/ttarticle/p/show?id=2309404442665222537485
https://weibo.com/ttarticle/p/show?id=2309404442665142845621
https://weibo.com/ttarticle/p/show?id=2309404442664979267913
https://weibo.com/ttarticle/p/show?id=2309404442664899576015
https://weibo.com/ttarticle/p/show?id=2309404442664819884131
https://weibo.com/ttarticle/p/show?id=2309404442664723415061
https://weibo.com/ttarticle/p/show?id=2309404442664647917761
https://weibo.com/ttarticle/p/show?id=2309404442664559837406
https://weibo.com/ttarticle/p/show?id=2309404442664316567769
https://weibo.com/ttarticle/p/show?id=2309404442664241070305
https://weibo.com/ttarticle/p/show?id=2309404442663481901255
https://weibo.com/ttarticle/p/show?id=2309404442663356072121
https://weibo.com/ttarticle/p/show?id=2309404442663150551055
https://weibo.com/ttarticle/p/show?id=2309404442663020527721
https://weibo.com/ttarticle/p/show?id=2309404442662961807615
https://weibo.com/ttarticle/p/show?id=2309404442662882115734
https://weibo.com/ttarticle/p/show?id=2309404442662798229652
https://weibo.com/ttarticle/p/show?id=2309404442662710149356
https://weibo.com/ttarticle/p/show?id=2309404442662622068910

流星花园导演否认欠款500万:对方已撤资 自己仍在推进
吴青峰被测试节目偷拍 回应:会更害怕跟陌生人说话
甄子丹一家不满被区别对待:中国人不是东亚病夫
小S晒与姐姐出席活动照:愿意当姐姐的配角!


https://weibo.com/ttarticle/p/show?id=2309404442662538182868

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!