设计模式之桥接模式

118 阅读3分钟

本文已参与「新人创作礼」活动,一起开启掘金创作之路。

  • 个人简介:微信公众号关注:SteveCode。为您分享更多的知识学术。生于忧患死于安乐
  • 专注Java技术干货分享,Java基础技术、数据结构、相关工具、Spring全家桶、intellij idea......

桥接模式-结构化

在这里插入图片描述 在这里插入图片描述 在这里插入图片描述 在这里插入图片描述

代码分享

类图

在这里插入图片描述

/*******************************************************************************
 * Package: com.example.demo.bridg
 * Type:    Brand
 * Date:    2022-03-12 20:47
 *
 * Copyright (c) 2022 HUANENG GUICHENG TRUST CORP.,LTD All Rights Reserved.
 *
 * You may not use this file except in compliance with the License.
 *******************************************************************************/
package com.example.demo.bridg;

/**
 * 功能描述:为手机品牌做的接口
 *
 * @author Songxianyang
 * @date 2022-03-12 20:47
 */
public interface Brand {
    /**
     * 打电话
     */
    void call();
    
    /**
     * 上网
     */
    void net();
}

/*******************************************************************************
 * Package: com.example.demo.bridg
 * Type:    Flat
 * Date:    2022-03-12 20:55
 *
 * Copyright (c) 2022 HUANENG GUICHENG TRUST CORP.,LTD All Rights Reserved.
 *
 * You may not use this file except in compliance with the License.
 *******************************************************************************/
package com.example.demo.bridg;

/**
 * 功能描述:平板 手机
 *
 * @author Songxianyang
 * @date 2022-03-12 20:55
 */
public class FlatPhone extends StylePhone {
    private String name = "平板:";
    
    public FlatPhone(Brand brand) {
        super(brand);
    }
    
    @Override
    public void call() {
        System.out.println(name);
        super.call();
    }
    
    @Override
    protected void net() {
        System.out.println(name);
        
        super.net();
    }
}

/*******************************************************************************
 * Package: com.example.demo.bridg
 * Type:    FlipPhone
 * Date:    2022-03-12 20:55
 *
 * Copyright (c) 2022 HUANENG GUICHENG TRUST CORP.,LTD All Rights Reserved.
 *
 * You may not use this file except in compliance with the License.
 *******************************************************************************/
package com.example.demo.bridg;

/**
 * 功能描述:翻盖 手机
 *
 * @author Songxianyang
 * @date 2022-03-12 20:55
 */
public class FlipPhone extends StylePhone {
    private String name = "翻盖:";
    
    public FlipPhone(Brand brand) {
        super(brand);
    }
    
    @Override
    public void call() {
        System.out.println(name);
        super.call();
    }
    
    @Override
    protected void net() {
        System.out.println(name);
        super.net();
    }
}

/*******************************************************************************
 * Package: com.example.demo.bridg
 * Type:    XiaoMPhone
 * Date:    2022-03-12 20:49
 *
 * Copyright (c) 2022 HUANENG GUICHENG TRUST CORP.,LTD All Rights Reserved.
 *
 * You may not use this file except in compliance with the License.
 *******************************************************************************/
package com.example.demo.bridg;

/**
 * 功能描述:小米手机
 *
 * @author Songxianyang
 * @date 2022-03-12 20:49
 */
public class HuaweiPhone implements Brand {
    @Override
    public void call() {
        System.out.println("Huawei手机打电话");
    }
    
    @Override
    public void net() {
        System.out.println("Huawei手机上网");
    }
}

/*******************************************************************************
 * Package: com.example.demo.bridg
 * Type:    StylePhone
 * Date:    2022-03-12 20:53
 *
 * Copyright (c) 2022 HUANENG GUICHENG TRUST CORP.,LTD All Rights Reserved.
 *
 * You may not use this file except in compliance with the License.
 *******************************************************************************/
package com.example.demo.bridg;

/**
 * 功能描述:手机样式
 *
 * @author Songxianyang
 * @date 2022-03-12 20:53
 */
public abstract class StylePhone {
    
    private Brand brand;
    
    public StylePhone(Brand brand) {
        this.brand = brand;
    }
    
    
     // 开始搭桥
     
    
    protected void call(){
        brand.call();
    }
    
    
    
    protected void net() {
        brand.net();
    }
}

/*******************************************************************************
 * Package: com.example.demo.bridg
 * Type:    Test
 * Date:    2022-03-12 20:51
 *
 * Copyright (c) 2022 HUANENG GUICHENG TRUST CORP.,LTD All Rights Reserved.
 *
 * You may not use this file except in compliance with the License.
 *******************************************************************************/
package com.example.demo.bridg;

/**
 * 功能描述:桥接模式   : 抽象类与接口分开
 *
 * @author Songxianyang
 * @date 2022-03-12 20:51
 */
public class Test {
    public static void main(String[] args) {
        StylePhone flatPhone = new FlatPhone(new XiaoMiPhone());
        flatPhone.call();
        flatPhone.net();
        System.out.println("----------------------");
        StylePhone flipPhone = new FlipPhone(new XiaoMiPhone());
        flipPhone.call();
        flipPhone.net();
        System.out.println("-----------------");
        StylePhone huawei = new FlipPhone(new HuaweiPhone());
        huawei.call();
        huawei.net();
    }
}

/*******************************************************************************
 * Package: com.example.demo.bridg
 * Type:    XiaoMPhone
 * Date:    2022-03-12 20:49
 *
 * Copyright (c) 2022 HUANENG GUICHENG TRUST CORP.,LTD All Rights Reserved.
 *
 * You may not use this file except in compliance with the License.
 *******************************************************************************/
package com.example.demo.bridg;

/**
 * 功能描述:小米手机
 *
 * @author Songxianyang
 * @date 2022-03-12 20:49
 */
public class XiaoMiPhone implements Brand{
    @Override
    public void call() {
        System.out.println("Xiaomi手机打电话");
    }
    
    @Override
    public void net() {
        System.out.println("Xiaomi手机上网");
    }
}

在这里插入图片描述

jdbc源码分析

没有分析明白

桥接总结

在这里插入图片描述