设计模式(7)-桥接模式及实现

设计模式(7)-桥接模式及实现


基本概念

将抽象部分和具体部分分离,使它们可以独立地变化。在桥接模式中,通过创建一个桥接接口(或抽象类),其中包含一个指向具体实现的引用,将抽象部分和具体部分连接起来。这样,抽象部分和具体部分可以独立地进行扩展,而不会相互影响。这种方式也被称为“组合优于继承”。
优点:
桥接模式的应用能够提供更好的灵活性和可扩展性。它允许抽象部分和具体部分独立变化,避免了类层次结构的爆炸式增长。这样可以更容易地添加新的抽象部分和具体部分,而不会影响到彼此。
缺点:
使用桥接模式可能会引入一些复杂性,因为你需要管理更多的类和对象。

https://gitee.com/want-to-lose-another-30-jin/design-pattern-implementation

设计模式具体实现


角色

1、抽象(Abstraction): 定义了客户端使用的接口,它保持了对扩展对象的引用。
2、扩展抽象(Refined Abstraction): 扩展了抽象类,添加了具体的行为。
3、实现(Implementor): 定义了实现类接口,它不依赖于抽象类。
4、具体实现(Concrete Implementor): 实现了实现类接口的具体类。

java实现

package shejimoshi.qiaojiemoshi;
// 抽象类
public abstract class Abstraction {
    protected Implementor implementor;

    public Abstraction(Implementor implementor) {
        this.implementor = implementor;
    }

    abstract void operation();
}

package shejimoshi.qiaojiemoshi;
// 扩展抽象类
public class RefinedAbstraction extends Abstraction{
    public RefinedAbstraction(Implementor implementor) {
        super(implementor);
    }

    @Override
    void operation() {
        implementor.implementation();
    }
}

package shejimoshi.qiaojiemoshi;
// 实现类接口
public interface Implementor {
    void implementation();
}

package shejimoshi.qiaojiemoshi;
// 具体实现类
public class ConcreteImplementorA implements Implementor {
    @Override
    public void implementation() {
        System.out.println("Implementation A");
    }
}

package shejimoshi.qiaojiemoshi;

public class ConcreteImplementorB implements Implementor{
    @Override
    public void implementation() {
        System.out.println("Implementation B");
    }
}

package shejimoshi.qiaojiemoshi;

public class client {
    public static void main(String[] args) {
        Abstraction abstractionA = new RefinedAbstraction(new ConcreteImplementorA());
        Abstraction abstractionB = new RefinedAbstraction(new ConcreteImplementorB());

        abstractionA.operation();
        abstractionB.operation();
    }
}

c++实现

#include <iostream>

// 实现类接口
class Implementor {
public:
    virtual ~Implementor() {}
    virtual void implementation() = 0;
};

// 具体实现类
class ConcreteImplementorA : public Implementor {
public:
    void implementation() override {
        std::cout << "Implementation A" << std::endl;
    }
};

class ConcreteImplementorB : public Implementor {
public:
    void implementation() override {
        std::cout << "Implementation B" << std::endl;
    }
};

// 抽象类
class Abstraction {
protected:
    Implementor* implementor;

public:
    Abstraction(Implementor* implementor) : implementor(implementor) {}

    virtual ~Abstraction() {}

    virtual void operation() = 0;
};

// 扩展抽象类
class RefinedAbstraction : public Abstraction {
public:
    RefinedAbstraction(Implementor* implementor) : Abstraction(implementor) {}

    ~RefinedAbstraction() {}

    void operation() override {
        implementor->implementation();
    }
};

int main() {
    Implementor* implementorA = new ConcreteImplementorA();
    Implementor* implementorB = new ConcreteImplementorB();

    Abstraction* abstractionA = new RefinedAbstraction(implementorA);
    Abstraction* abstractionB = new RefinedAbstraction(implementorB);

    abstractionA->operation();
    abstractionB->operation();

    delete abstractionA;
    delete abstractionB;
    delete implementorA;
    delete implementorB;

    return 0;
}
  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值