• 设计模式之代理模式


     

          照旧我们还是先来看看代理模式的定义:为其他对象提供一种代理以控制对这个对象的访问,定义很简单明了。让我们来看看这样一个场景吧,假如你要结婚,然后就必须要去见一见女方的家长。但是这个时候,你女朋友又担心你最笨说错话。于是你们就商量好了,当岳父岳母问你的时候,尽量交由你女朋友来回答,避免岳父岳母对你不满意。好的,下面我们就将通过代理模式来模拟出这个场景。

          首先我们需要定义一个基接口IMeet来约定好你见岳父岳母过程中,应该做哪些事情。代码如下:

    public interface IMeet {
     
        void IntroduceName();
        void IntroduceAge();
        void IntroduceWork();
        void IntroduceFaimly();
    }

      然后我们肯定需要将你这个男朋友定义出来,同时让他集成上面的接口,完成上面的那些行为,才有可能得到岳父岳母的同意。代码如下:

    public class BoyFriend implements IMeet {
     
        @Override
        public void IntroduceName() {
            System.out.println("xiaocai");
        }
     
        @Override
        public void IntroduceAge() {
            System.out.println("20");
        }
     
        @Override
        public void IntroduceWork() {
            System.out.println("软件工程师");
        }
     
        @Override
        public void IntroduceFaimly() {
            System.out.println("家里还有个妹妹");
        }
    }

      最后要开始见面了,肯定少不了你女朋友这个见面代理人(MeetProxy)咯,同理她肯定也需要集成IMeet接口,因为代理者肯定也需要被代理人需要执行哪些行为。MeetProxy代码如下:

    public class MeetProxy implements IMeet {
     
        private IMeet boyFriend;
         
        public MeetProxy(IMeet boyFriend){
            this.boyFriend=boyFriend;
        }
         
        @Override
        public void IntroduceName() {
            this.boyFriend.IntroduceName();
        }
     
        @Override
        public void IntroduceAge() {
            this.boyFriend.IntroduceAge();
        }
     
        @Override
        public void IntroduceWork() {
            this.boyFriend.IntroduceWork();
        }
     
        @Override
        public void IntroduceFaimly() {
            this.boyFriend.IntroduceFaimly();
        }
    }
    

      

      好了,上面已经定义出来代理模式里面最重要的三个基本要素:代理人(MeetProxy)、被代理人(BoyFriend)、需要执行的行为协议(IMeet),接下来所有的对男朋友的访问都将通过你的女朋友来完成。代码如下:

    public class MainTest {
     
        public static void main(String[] args) {
             
            IMeet boyFriend=new BoyFriend();
            MeetProxy grilFriend=new MeetProxy(boyFriend);
            grilFriend.IntroduceName();
            grilFriend.IntroduceAge();
            grilFriend.IntroduceFaimly();
            grilFriend.IntroduceWork();
        }
    }
    

      

      ok,通过上面的代码你可以发现确实所有对男朋友的访问都屏蔽掉了,但是如果你仔细看代码会发现我还需要New出来一个BoyFriend,也就是说我还需要明确知道代理者现在具体要代理的是哪个对象。那么有没有什么办法能够让代理者内部消化掉这些判断逻辑呢?答案是肯定的,我们只需要修改一下代理者,代码如下:

    public class MeetProxy implements IMeet {
     
        private IMeet boyFriend=new BoyFriend();
         
        @Override
        public void IntroduceName() {
            this.boyFriend.IntroduceName();
        }
     
        @Override
        public void IntroduceAge() {
            this.boyFriend.IntroduceAge();
        }
     
        @Override
        public void IntroduceWork() {
            this.boyFriend.IntroduceWork();
        }
     
        @Override
        public void IntroduceFaimly() {
            this.boyFriend.IntroduceFaimly();
        }
    }
    

      

        上面的代码,我们只是简单去掉了代理者里面的构造函数,直接new出来了被代理者对象。这样我们就能够很明显的知道,女朋友只代理男朋友回答自己父母的问题。同时调用的时候,我们也不再需要new一个BoyFriend了,因为女朋友这个代理类里面已经知道自己男朋友是谁了,调用代码也会很简单。代码如下:

    public class MainTest {
     
        public static void main(String[] args) {
             
            MeetProxy grilFriend=new MeetProxy();
            grilFriend.IntroduceName();
            grilFriend.IntroduceAge();
            grilFriend.IntroduceFaimly();
            grilFriend.IntroduceWork();
        }
    }
    

      好了,自己水平有限。  今天就说那么多吧!

  • 相关阅读:
    查看full gc频率怎么样
    【LeetCode每天一题】Linked List Cycle II(循环链表II)
    【LeetCode每天一题】Word Break()
    【LeetCode每天一题】Candy(分糖果)
    【LeetCode每天一题】Single Number II(数组中单个数字II)
    【LeetCode每天一题】Gas Station(汽油站)
    【LeetCode每天一题】Single Number(数组中单独的数字)
    【LeetCode每天一题】Sum Root to Leaf Numbers(二叉树所有根到叶节点之和)
    【LeetCode每天一题】Longest Consecutive Sequence(最长的连续序列
    【LeetCode每天一题】 Word Ladder(单词阶梯)
  • 原文地址:https://www.cnblogs.com/baizhanshi/p/5648821.html
Copyright © 2020-2023  润新知