Java语言设计模式之桥接(Bridge)模式

Bridge 模式的目的就是为了结构化你的代码,从而使得你可以很容易的添加新类型的前端(front-end)对象(这些前端对象是通过调用新类型的后端(back-end)对象的功能实现的)。这么一来,就可以对前端(front-end)和后端(back-end)互不干扰地进行修改。

通过使用 Bridge 模式,你可以避免(类)组合所带来的类的数目的爆炸省增长。但是别忘了 Bridge 模式所处理的一系列变化通常都是发生在编码阶段:当为了实现某个功能而必须处理越来越多的选项(options)时,Bridge 模式可以使你的代码保持条例性。

Bridge 模式实例
Java语言设计模式之桥接(Bridge)模式_第1张图片

BridgeStructure .java

package com.enfo.wd.mode;

class Abstraction{
    private Implementation implementation;
    public Abstraction(Implementation imp){
        implementation=imp;
    }
    public void service1(){
        implementation.facility1();
        implementation.facility2();
    }
    public void service2(){
        implementation.facility2();
        implementation.facility3();
    }
    public void service3(){
        implementation.facility1();
        implementation.facility2();
        implementation.facility4();
    }
    public Implementation getImplementation(){
        return implementation;
    }
}

class ClientService1 extends Abstraction{
    public ClientService1(Implementation imp){super(imp);}
    public void serviceA(){
        service1();
        service2();
    }
    public void serviceB(){
        service3();
    }
}
class ClientService2 extends Abstraction{
    public ClientService2(Implementation imp){super(imp);}
    public void serviceC(){
        service2();
        service3();
    }
    public void serviceD(){
        service1();
        service3();
    }
    public void serviceE(){
        getImplementation().facility3();
    }
}

interface Implementation{
    void facility1();
    void facility2();
    void facility3();
    void facility4();
}

class Library1{
    public void method1(){
        System.out.println("Library1.method1()");
    }
    public void method2(){
        System.out.println("Library1.method2()");
    }
}
class Library2{
    public void operation1(){
        System.out.println("Library2.operation1()");
    }
    public void operation2(){
        System.out.println("Library2.operation2()");
    }
    public void operation3(){
        System.out.println("Library2.operation3()");
    }
}

class Implementation1 implements Implementation{
    private Library1 delegate=new Library1();
    @Override
    public void facility1() {
        System.out.println("Implementation1.facility1()");
        delegate.method1();
    }

    @Override
    public void facility2() {
        System.out.println("Implementation1.facility2()");
        delegate.method2();
    }

    @Override
    public void facility3() {
        System.out.println("Implementation1.facility3()");
        delegate.method2();
        delegate.method1();
    }

    @Override
    public void facility4() {
        System.out.println("Implementation1.facility4()");
        delegate.method1();
    }

}
class Implementation2 implements Implementation{
    private Library2 delegate=new Library2();
    @Override
    public void facility1() {
        System.out.println("Implementation2.facility1()");
        delegate.operation1();
    }

    @Override
    public void facility2() {
        System.out.println("Implementation2.facility2()");
        delegate.operation2();
    }

    @Override
    public void facility3() {
        System.out.println("Implementation2.facility3()");
        delegate.operation3();
    }

    @Override
    public void facility4() {
        System.out.println("Implementation2.facility4()");
        delegate.operation1();
    }

}
public class BridgeStructure {
    public void test1(){
        ClientService1 cs1=new ClientService1(new Implementation1());
        cs1.serviceA();
        cs1.serviceB();
    }
    public void test2(){
        ClientService1 cs1=new ClientService1(new Implementation2());
        cs1.serviceA();
        cs1.serviceB();
    }
    public void test3(){
        ClientService2 cs2=new ClientService2(new Implementation1());
        cs2.serviceC();
        cs2.serviceD();
        cs2.serviceE();
    }
    public void test4(){
        ClientService2 cs2=new ClientService2(new Implementation2());
        cs2.serviceC();
        cs2.serviceD();
        cs2.serviceE();
    }

    public static void main(String[] args){
        BridgeStructure tructur=new BridgeStructure();
        tructur.test1();
        tructur.test2();
        tructur.test3();
        tructur.test4();
    }
}

你可能感兴趣的:(Java语言设计模式之桥接(Bridge)模式)