自学内容网 自学内容网

七、结构型(桥接模式)

桥接模式

概念
桥接模式是一种结构型设计模式,旨在将抽象部分与其实现部分分离,使它们可以独立变化。它通过使用组合关系而非继承来实现接口和实现的解耦,从而提高系统的灵活性和可扩展性。


应用场景

  1. 多个维度的变化:当一个系统有多个变化维度,比如形状和颜色,使用桥接模式可以将这些维度分开处理,避免大量的子类。
  2. 需要避免类爆炸:在某些情况下,子类数量呈指数级增长,使用桥接模式可以有效减少类的数量。
  3. 抽象与实现解耦:当需要在不影响客户端的情况下,修改或扩展类的实现时,桥接模式非常有效。
  4. 需要统一接口:当需要为不同的实现提供一致的接口时,桥接模式可以帮助实现。

注意点

  • 系统复杂性:引入桥接模式会增加系统的复杂性,因为需要维护额外的桥接类。
  • 设计适用性:确保桥接模式适用于场景,避免在简单的场景中使用,导致不必要的复杂性。
  • 抽象与实现的角色:明确抽象与实现的角色分离,确保设计的清晰性。

核心要素

  1. 抽象类:定义抽象部分的接口,并持有对实现部分的引用。
  2. 具体抽象类:扩展抽象类,定义具体的抽象实现。
  3. 实现接口:定义实现部分的接口。
  4. 具体实现类:实现实现接口的具体实现类。

Java代码完整示例

// 实现接口
interface Implementor {
    void operationImpl();
}

// 具体实现类A
class ConcreteImplementorA implements Implementor {
    @Override
    public void operationImpl() {
        System.out.println("具体实现A的操作");
    }
}

// 具体实现类B
class ConcreteImplementorB implements Implementor {
    @Override
    public void operationImpl() {
        System.out.println("具体实现B的操作");
    }
}

// 抽象类
abstract class Abstraction {
    protected Implementor implementor;

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

    public abstract void operation();
}

// 具体抽象类
class RefinedAbstraction extends Abstraction {
    public RefinedAbstraction(Implementor implementor) {
        super(implementor);
    }

    @Override
    public void operation() {
        System.out.println("具体抽象类的操作");
        implementor.operationImpl(); // 委托给实现类
    }
}

// 客户端
public class Client {
    public static void main(String[] args) {
        Implementor implementorA = new ConcreteImplementorA();
        Abstraction abstractionA = new RefinedAbstraction(implementorA);
        abstractionA.operation();

        Implementor implementorB = new ConcreteImplementorB();
        Abstraction abstractionB = new RefinedAbstraction(implementorB);
        abstractionB.operation();
    }
}

各种变形用法完整示例

  1. 使用多个具体实现
    添加更多的实现类,展示灵活性。

    代码示例

    // 额外的具体实现类C
    class ConcreteImplementorC implements Implementor {
        @Override
        public void operationImpl() {
            System.out.println("具体实现C的操作");
        }
    }
    
    // 客户端
    public class ClientMultipleImplementors {
        public static void main(String[] args) {
            Implementor implementorA = new ConcreteImplementorA();
            Abstraction abstractionA = new RefinedAbstraction(implementorA);
            abstractionA.operation();
    
            Implementor implementorB = new ConcreteImplementorB();
            Abstraction abstractionB = new RefinedAbstraction(implementorB);
            abstractionB.operation();
    
            Implementor implementorC = new ConcreteImplementorC();
            Abstraction abstractionC = new RefinedAbstraction(implementorC);
            abstractionC.operation();
        }
    }
    
  2. 扩展抽象类
    添加新的抽象类以扩展功能。

    代码示例

    // 新抽象类
    abstract class ExtendedAbstraction extends Abstraction {
        public ExtendedAbstraction(Implementor implementor) {
            super(implementor);
        }
    
        public abstract void extendedOperation();
    }
    
    // 具体扩展抽象类
    class ConcreteExtendedAbstraction extends ExtendedAbstraction {
        public ConcreteExtendedAbstraction(Implementor implementor) {
            super(implementor);
        }
    
        @Override
        public void operation() {
            System.out.println("具体扩展抽象类的操作");
            implementor.operationImpl();
        }
    
        @Override
        public void extendedOperation() {
            System.out.println("具体扩展抽象类的扩展操作");
        }
    }
    
    // 客户端
    public class ClientExtendedAbstraction {
        public static void main(String[] args) {
            Implementor implementorB = new ConcreteImplementorB();
            ExtendedAbstraction extendedAbstraction = new ConcreteExtendedAbstraction(implementorB);
            extendedAbstraction.operation();
            extendedAbstraction.extendedOperation();
        }
    }
    
  3. 桥接模式与组合模式结合
    桥接模式与组合模式结合使用,可以处理更复杂的结构。

    代码示例

    // 组合模式的叶子类
    class Leaf implements Implementor {
        private String name;
    
        public Leaf(String name) {
            this.name = name;
        }
    
        @Override
        public void operationImpl() {
            System.out.println("叶子: " + name);
        }
    }
    
    // 组合模式的组合类
    class Composite implements Implementor {
        private List<Implementor> children = new ArrayList<>();
    
        public void add(Implementor child) {
            children.add(child);
        }
    
        @Override
        public void operationImpl() {
            for (Implementor child : children) {
                child.operationImpl();
            }
        }
    }
    
    // 客户端
    public class ClientComposite {
        public static void main(String[] args) {
            Composite composite = new Composite();
            composite.add(new Leaf("叶子1"));
            composite.add(new Leaf("叶子2"));
    
            Abstraction abstraction = new RefinedAbstraction(composite);
            abstraction.operation();
        }
    }
    

这些示例展示了桥接模式的基本用法及其变形。


原文地址:https://blog.csdn.net/xiaoqi270620903/article/details/142870882

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!