Java设计模式---桥接Bridge模式

本文通过一个生动的例子,讲解了桥接模式的原理及其在实际场景中的应用。通过将抽象部分和实现部分分离,使用桥接模式可以有效地解决礼物选择的问题,使代码更加灵活和易于扩展。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

参考于 :

  大话设计模式

  马士兵设计模式视频

  

    

  写在开头: 桥接模式主要用于一件事物分成了两个维度,进行排列组合,比如礼物,可以分成优雅的礼物(抽象),花(具体),排列组合优雅的花!

1.为什么使用桥接模式 

  小丁追小彭,送礼物必不可少。面向对象的思维,如何去实现这个礼物,先定义一个礼物的接口或抽象类,然后只要实现了这个接口那就算一个礼物。

  小彭是一个精致的女孩,我要送一个精致的礼物,好,按照上面的思路,实现礼物接口。小丁进了花店,哇,花朵,嗯,花朵也算礼物,实现礼物接口。

  小丁想挑精致的花朵,用面向对象的思想,嗯,直接继承精致的礼物就实现了精致花朵。

  

  但是,小丁仔细一想,这只是花店,只有花朵,实现起来不复杂,如果进了一家百货商店,那么就要这样实现,相当麻烦。

  

2.使用桥接模式

  使用前先解释上面图片中的话:

  将抽象部分和实现部分分离 : 上面例子中,精致的礼物就是一个比较抽象的部分,精致的花就是一个实现部分,上面例子中,这两个是继承关系,现在需要进行分离。

  使用聚合关系来替换继承关系

  下面代码中Gift接口可去,去了后类的关系就和最上面那张UML一样

  下面代码的UML类图

  

 

package com.dingyu;
/**
 * 实现了这个接口,表示你是一个礼物
 * @author dingyu
 *
 */
public interface Gift {
    
}
package com.dingyu;
/**
 * 一个精致的礼物
 * 抽象部分有一个具体的部分
 * @author dingyu
 *
 */
public class GraceGift implements Gift {
    private GiftImpl giftImpl;

    public GraceGift(GiftImpl giftImpl) {
        this.giftImpl = giftImpl;
    }

    public GiftImpl getGiftImpl() {
        return giftImpl;
    }

    public void setGiftImpl(GiftImpl giftImpl) {
        this.giftImpl = giftImpl;
    }

} 
package com.dingyu;
/**
 * 具体的礼物
 * @author dingyu
 *
 */
public interface GiftImpl {

}
package com.dingyu;

public class Flower implements GiftImpl {

}
package com.dingyu;

public class Shorts implements GiftImpl {
    
}
package com.dingyu;

/**
 * 男孩类
 * 
 * @author dingyu
 *
 */
public class Boy {
    private String name = "xiaoding";

    public void sendGift() {
        Gift gift1;
        Gift gift2;
        gift1 = new GraceGift(new Shorts());
        gift2 = new GraceGift(new Flower());
    }
}

 

  

 

转载于:https://www.cnblogs.com/dddyyy/p/10046548.html

Java设计模式是一组经过实践验证的面向对象设计原则和模式,可以帮助开发人员解决常见的软件设计问题。下面是常见的23种设计模式: 1. 创建型模式(Creational Patterns): - 工厂方法模式(Factory Method Pattern) - 抽象工厂模式(Abstract Factory Pattern) - 单例模式(Singleton Pattern) - 原型模式(Prototype Pattern) - 建造者模式(Builder Pattern) 2. 结构型模式(Structural Patterns): - 适配器模式(Adapter Pattern) - 桥接模式Bridge Pattern) - 组合模式(Composite Pattern) - 装饰器模式(Decorator Pattern) - 外观模式(Facade Pattern) - 享元模式(Flyweight Pattern) - 代理模式(Proxy Pattern) 3. 行为型模式(Behavioral Patterns): - 责任链模式(Chain of Responsibility Pattern) - 命令模式(Command Pattern) - 解释器模式(Interpreter Pattern) - 迭代器模式(Iterator Pattern) - 中介者模式(Mediator Pattern) - 备忘录模式(Memento Pattern) - 观察者模式(Observer Pattern) - 状态模式(State Pattern) - 策略模式(Strategy Pattern) - 模板方法模式(Template Method Pattern) - 访问者模式(Visitor Pattern) 4. 并发型模式(Concurrency Patterns): - 保护性暂停模式(Guarded Suspension Pattern) - 生产者-消费者模式(Producer-Consumer Pattern) - 读写锁模式(Read-Write Lock Pattern) - 信号量模式(Semaphore Pattern) - 线程池模式(Thread Pool Pattern) 这些设计模式可以根据问题的特点和需求来选择使用,它们提供了一些可复用的解决方案,有助于开发高质量、可维护且易于扩展的软件系统。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值