zoukankan      html  css  js  c++  java
  • 北风设计模式课程---21、中介者模式

    北风设计模式课程---21、中介者模式

    一、总结

    一句话总结:

    中介者模式去交互:男人女人有问题有情况直接去找媒婆中介,男人类和女人类之间不交互,直接通过媒婆中介交互

    1、中介者模式主要解决什么问题?

    解决类之间的交互行为:比如男女匹配的时候男人类中调用女人类的方法,女人类中调用男人类的方法

    2、中介者模式 注意点?

    1、人到中介去注册:人的抽象类里面是有中介抽象类的对象的引用
    2、具体中介有具体类的对象:比如媒婆有男人和女人的对象的引用

    3、中介者模式和代理模式和外观模式的区别?

    中介者模式:两类(或多个类)之间的交互:比如男女类
    代理模式:一类的代理:比如你找代理给你买什么东西,代理里面可以添加方法
    外观模式:使外部类不需要知道子模块的逻辑,调用什么功能的时候直接调用外观类即可

    4、什么是中介者模式?

    中介者模式去交互:男人女人有问题有情况直接去找媒婆中介,男人类和女人类之间不交互,直接通过媒婆中介交互

    Mediator模式也叫中介者模式,是由GoF提出的23种
    软件设计模式的一种。Mediator模式是行为模式之一,
    在Mediator模式中,类之间的交互行为被统一放在
    Mediator的对象中,对象通过Mediator对象同其他对象
    交互,Mediator对象起着控制器的作用。

    5、解决多个类之间问题的设计模式?

    抽象方法模式、桥接模式、中介者模式、外观模式

    6、中介者模式的角色和职责?

    1、mediator:抽象中介类:中介者类的抽象父类。
    2、concreteMediator:比如媒婆:具体的中介者类。
    3、colleague:比如抽象人类:关联类的抽象父类。
    4、concreteColleague:比如男人类、女人类:具体的关联类。

    二、内容在总结中

    1、相关知识

    1、mediator:抽象中介类:中介者类的抽象父类。
    2、concreteMediator:比如媒婆:具体的中介者类。
    3、colleague:比如抽象人类:关联类的抽象父类。
    4、concreteColleague:比如男人类、女人类:具体的关联类。

    2、代码

    1、mediator:抽象中介类:中介者类的抽象父类。

    2、concreteMediator:比如媒婆:具体的中介者类。

    Mediator.java

    package com.ibeifeng.ex2;
    
    public class Mediator {
        private Man man;
        private Woman woman;
        
        public void setMan(Man man) {
            this.man = man;
        }
        public void setWoman(Woman woman) {
            this.woman = woman;
        }
        
        public void getPartner(Person person) {
            //将搭档设置上
            if(person instanceof Man) {
                this.setMan((Man)person);
            } else {
                this.setWoman((Woman)person);
            }
            //判断条件
            if(man == null || woman == null) {
                System.out.println("汗,我不是同性恋!");
            } else {
                
                if(man.getCondition() == woman.getCondition()) {
                    System.out.println(man.getName() + "和" + woman.getName() + "绝配");
                } else {
                    System.out.println(man.getName() + "和" + woman.getName() + "不相配");
                }
            }
        }
        
    }

    3、colleague:比如抽象人类:关联类的抽象父类。

    Person.java

    package com.ibeifeng.ex2;
    
    public abstract class Person {
        private String name;
        private int condition;
        private Mediator mediator;
    
        
        public Person(String name, int condition, Mediator mediator) {
            super();
            this.name = name;
            this.condition = condition;
            this.mediator = mediator;
        }
        
        public Mediator getMediator() {
            return mediator;
        }
    
        public void setMediator(Mediator mediator) {
            this.mediator = mediator;
        }
    
        
    
    
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public int getCondition() {
            return condition;
        }
        
        public void setCondition(int condition) {
            this.condition = condition;
        }
        
        public abstract void getPartner(Person person);
        
    }

    4、concreteColleague:比如男人类、女人类:具体的关联类。

    Man.java

    package com.ibeifeng.ex2;
    
    public class Man extends Person {
    
        public Man(String name, int condition,Mediator mediator) {
            super(name, condition, mediator);
        }
    
        public void getPartner(Person person) {
            this.getMediator().setMan(this);
            this.getMediator().getPartner(person);
        }
    }

    Woman.java

    package com.ibeifeng.ex2;
    
    public class Woman extends Person {
    
        public Woman(String name, int condition,Mediator mediator) {
            super(name, condition, mediator);
        }
    
        public void getPartner(Person person) {
            this.getMediator().setWoman(this);
            this.getMediator().getPartner(person);
        }
        
    }

    5、客户端调用

    MainClass.java

    package com.ibeifeng.ex2;
    
    public class MainClass {
        public static void main(String[] args) {
            Mediator mediator = new Mediator();
            Person zhangsan = new Man("张三",7,mediator);
            Person lisi = new Man("李四",7,mediator);
            Person xiaofang = new Woman("小芳",7,mediator);
            
            zhangsan.getPartner(lisi);
            
            xiaofang.getPartner(lisi);
        }
    }
     
  • 相关阅读:
    二叉树的下一个节点
    二叉树的对称
    CString,string和char*
    二叉平衡树
    二叉树的深度
    必应首页图片下载
    Git报错:fatal: remote origin already exists.
    sublime text3 自定义代码片段
    atom自定义C++代码片段
    vscode 自定义代码片段(snippets)
  • 原文地址:https://www.cnblogs.com/Renyi-Fan/p/11067219.html
Copyright © 2011-2022 走看看