设计模式(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;
}