zoukankan      html  css  js  c++  java
  • 对象行为型

    介绍

    顾名思义就是从对象和对象直接交接转(对象-对象)变成(对象-中介者-对象)
    

    定义

    用一个中介者对象封装一系列的对象交互,中介者使各对象不需要显示地相互作用,从而使耦合松散,而且可以独立地改变它们之间的交互
    

    使用场景

    对象与对象之间关系复杂呈现网状结构时,通过中介者可以适量降低对象与对象之间关系的复杂程度(耦合)
    

    入门案例:

    UML图解:

    代码:

    Children:

    public abstract class Children {
        //心情
        protected Mood mood;
        //说话
        protected String say;
        //角色
        protected String role;
    
        /**
         * @param role 角色
         * @param mood 心情
         * @param say 说话
         */
        protected Children(String role, Mood mood, String say) {
            this.role = role;
            this.mood = mood;
            this.say = say;
        }
    
        //没用中间人调解时使用的调解方法
        public void mediate(String msg, Children children) {
    
            if("对不起".equals(msg)){
                children.setMood(Mood.HAPPY);
                this.setMood(Mood.HAPPY);
                this.setSay("没关系,我这个做" + this.getRole() + "的也有不对");
            }else if ("不道歉".equals(msg)){
                children.setMood(Mood.SAD);
                this.setMood(Mood.SAD);
                this.setSay("哼,就是" + this.getRole() + "不对");
            }else{
                throw new RuntimeException("要不道歉要不滚蛋");
            }
        }
    
    
        //显示心情和说话
        public void showMood() {
            System.out.println("现在心情::" + this.getMood().getMsg() );
            System.out.println( this.getSay() );
        }
    
        public Mood getMood() {
            return mood;
        }
    
        public void setMood(Mood mood) {
            this.mood = mood;
        }
    
        public String getSay() {
            return say;
        }
    
        public void setSay(String say) {
            this.say = say;
        }
    
        public String getRole() {
            return role;
        }
    
        public void setRole(String role) {
            this.role = role;
        }
    }
    

    XiaoMing:

    public class XiaoMing extends Children {
    
        public XiaoMing(String role, Mood mood, String say) {
            super(role, mood, say);
        }
    
    }
    

    XiaoHong:

    public class XiaoHong extends Children {
    
        public XiaoHong(String role, Mood mood, String say) {
            super(role, mood, say);
        }
    
    }
    

    Mediator:

    //中间人
    public abstract class Mediator {
    
        protected Children children1;
        protected Children children2;
    
        protected Mediator(Children children1, Children children2){
            this.children1 = children1;
            this.children2 = children2;
        }
    
        /**
         * 中间人调解方法
         * @param say 道歉方说话 说话标准参考枚举 SAY
         * @param children1 道歉方
         * @param children2 接受方
         */
        public abstract void mediate(String say, Children children1, Children children2);
    
        //setter和getter
        public Children getChildren1() {
            return children1;
        }
    
        public void setChildren1(Children children1) {
            this.children1 = children1;
        }
    
        public Children getChildren2() {
            return children2;
        }
    
        public void setChildren2(Children children2) {
            this.children2 = children2;
        }
    }
    
    

    Mother:

    //ConcreteMediator
    public class Mother extends Mediator {
        public Mother(Children children1, Children children2) {
            super(children1, children2);
        }
    
        @Override
        public void mediate(String say, Children children1, Children children2) {
    
            switch (say){
                case "对不起":
                    children2.mediate(say, children1);
                    break;
    
                case "不道歉":
                    children2.mediate(say, children1);
                    break;
    
                default:
                    throw new RuntimeException("要不道歉要不滚蛋");
            }
    
        }
    }
    

    过程用到的枚举:

    //心情
    public enum Mood {
        SAD("难过"), //伤心
        HAPPY("高兴") //高兴
        ;
    
        private String msg;
    
        Mood(String msg) {
            this.msg = msg;
        }
    
        public String getMsg() {
            return msg;
        }
    }
    
    
    //话语
    public enum Say {
        SORRY("对不起"), //伤心
        NOWAY("不道歉") //高兴
        ;
    
        private String msg;
    
        Say(String msg) {
            this.msg = msg;
        }
    
        public String getMsg() {
            return msg;
        }
    }
    

    Client(测试):

    public class Main {
        public static void main(String[] args){
    
            /**
             * 情景:小明和小红是两姐弟,今天为了雪糕闹翻了
             */
            XiaoMing xiaoMing = new XiaoMing( "弟弟", Mood.SAD, "她吃了我的雪糕" );
            XiaoHong xiaoHong = new XiaoHong( "姐姐", Mood.SAD, "这是我的雪糕" );
    
            System.out.println("xxxxxxxxxxxxxxxxxxxxxxxxxxx 【调解关系之前】 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
    
            //调解之前
            xiaoMing.showMood();
            System.out.println("----------------------------");
            xiaoHong.showMood();
    
    
            System.out.println("xxxxxxxxxxxxxxxxxxxxxxxxxxx 【双方自我调解】 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
            //解决方案1:直接调解关系,小明认错
            xiaoMing.setSay(Say.SORRY.getMsg());
            xiaoHong.mediate(xiaoMing.getSay(), xiaoMing);
    
            xiaoMing.showMood();
            System.out.println("----------------------------");
            xiaoHong.showMood();
    
            System.out.println("xxxxxxxxxxxxxxxxxxxxxxxxxxx 【通过妈妈做中间人调解】 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
    
            //解决方案2:通过妈妈来调解,小明认错,但不好意思和姐姐说
    //        xiaoMing.setSay( Say.NOWAY.getMsg() );
            xiaoMing.setSay( Say.SORRY.getMsg() );
            Mother mother = new Mother(xiaoMing, xiaoHong);
            mother.mediate(xiaoMing.getSay(), xiaoMing, xiaoHong);
    
            xiaoMing.showMood();
            System.out.println("----------------------------");
            xiaoHong.showMood();
    
            /**
             * output:
                 xxxxxxxxxxxxxxxxxxxxxxxxxxx 【调解关系之前】 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
                 现在心情::难过
                 她吃了我的雪糕
                 ----------------------------
                 现在心情::难过
                 这是我的雪糕
                 xxxxxxxxxxxxxxxxxxxxxxxxxxx 【双方自我调解】 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
                 现在心情::高兴
                 对不起
                 ----------------------------
                 现在心情::高兴
                 没关系,我这个做姐姐的也有不对
                 xxxxxxxxxxxxxxxxxxxxxxxxxxx 【通过妈妈做中间人调解】 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
                 现在心情::高兴
                 对不起
                 ----------------------------
                 现在心情::高兴
                 没关系,我这个做姐姐的也有不对
             */
    
        }
    }
    
  • 相关阅读:
    线性代数基础知识的复习
    第一个机器学习算法:线性回归与梯度下降
    初识机器学习
    VScode中LeetCode插件无法登录的情况
    内存管理-内存管理功能
    分组密码
    Linux进程调度
    进程调度
    死锁
    临界区和缩
  • 原文地址:https://www.cnblogs.com/tandi19960505/p/8332206.html
Copyright © 2011-2022 走看看