NBA之中介者模式

183 阅读1分钟

Design Pattern:

Mediator pattern is used to reduce communication complexity between multiple objects or classes

模式:中介模式用于减少多个对象或类之间的通信复杂性,该模式提供了一个中介类,它通常处理不同类之间的所有通信,并支持通过松散耦合来方便地维护代码

故事场景:教练说:大家都会过人,扣篮,但是不会联合使用,所以我准备教大家一个连贯过人后扣篮得分

篮球篮球运动员,库里,杜兰特
  public class Player {
  
      private String name;
  
      public Player(String name) {
          this.name = name;
      }
  
      public void play(){
          BasketballTrain.play(this);
      }
      public void dunk(){
          System.out.println("扣篮");
      }
      public void shoot(){
           System.out.println("投篮");
      }
      public String getName() {
          return name;
      }
  
      public void setName(String name) {
          this.name = name;
      }
  }
中介者 教练 把球员叫到位 然后指导
  public class BasketballTrain {
  
      public static void play(Player player){
          System.out.println(player.getName()+player.shoot()+player.dunk());
      }
  }

实验的开始
    public class MediatorDemo {
    
        public static void main(String[] args) {
    
            Player durant = new Player("杜兰特");
            Player curry = new Player("库里");
    
            durant.play();
            curry.play();
        }
    }

结果