• java实现23种设计模式之中介者模式


    中介者模式(Mediator Pattern)是用来降低多个对象和类之间的通信复杂性。这种模式提供了一个中介类,该类通常处理不同类之间的通信,并支持松耦合,使代码易于维护。

    package com.ceshi21;
    
    public interface Mediator {
        public void createMediator();
        public void workAll();
    }
    package com.ceshi21;
    
    public abstract class User {
        private Mediator mediator;
        
        public Mediator getMediator(){
            return mediator;
        }
        
        public User(Mediator mediator) {
            this.mediator = mediator;
        }
    
        public abstract void work();
    }
    package com.ceshi21;
    
    public class User1 extends User{
        
        public User1(Mediator mediator){
            super(mediator);
        }
        
        @Override
        public void work() {
            System.out.println("user1 exe!");
        }
    }
    package com.ceshi21;
    
    public class User2 extends User{
    
        public User2(Mediator mediator){
            super(mediator);
        }
        
        @Override
        public void work() {
            System.out.println("user2 exe!");
        }
    
    }
    package com.ceshi21;
    
    public class MyMediator implements Mediator{
    
        private User user1;
        private User user2;
        
        public User getUser1() {
            return user1;
        }
    
        public User getUser2() {
            return user2;
        }
    
        @Override
        public void createMediator() {
            user1 = new User1(this);
            user2 = new User2(this);
        }
    
        @Override
        public void workAll() {
            user1.work();
            user2.work();
        }
    
    }

    主要是通过这个类进行关联

    package com.ceshi21;
    
    public class Test {
        public static void main(String[] args) {
            Mediator mediator = new MyMediator();
            mediator.createMediator();
            mediator.workAll();
        }
    }

    测试结果:

  • 相关阅读:
    将动态aspx页面转换成为静态html页面的几种方法
    IHttpHandler 概述
    .Net,你究竟有多慢
    Js离开提示
    vs2008Webconfig文件
    什么是URL转发和一个IP建多个Web站点主机头名法
    sqlserver 统计sql语句大全收藏
    web.config 读写操作
    httpModules 与 httpHandlers
    简单的介绍一下常用的正规表示式:
  • 原文地址:https://www.cnblogs.com/zhengyuanyuan/p/10757302.html
Copyright © 2020-2023  润新知