Java Tutorial/Design Pattern/Bridge Pattern

Материал из Java эксперт
Перейти к: навигация, поиск

A demonstration of the structure and operation of the Bridge Pattern.

class Abstraction {
  private Task task;
  public Abstraction(Task imp) {
    task = imp;
  }
  public void service1() {
    task.task1();
    task.task2();
  }
  public void service2() {
    task.task2();
    task.task3();
  }
  public void service3() {
    task.task1();
    task.task2();
    task.task4();
  }
  protected Task getImplementation() {
    return task;
  }
}
class ClientService1 extends Abstraction {
  public ClientService1(Task imp) {
    super(imp);
  }
  public void serviceA() {
    service1();
    service2();
  }
  public void serviceB() {
    service3();
  }
}
class ClientService2 extends Abstraction {
  public ClientService2(Task imp) {
    super(imp);
  }
  public void serviceC() {
    service2();
    service3();
  }
  public void serviceD() {
    service1();
    service3();
  }
  public void serviceE() {
    getImplementation().task3();
  }
}
interface Task {
  void task1();
  void task2();
  void task3();
  void task4();
}
class Integration1 {
  public void method1() {
    System.out.println("Integration1.method1()");
  }
  public void method2() {
    System.out.println("Integration1.method2()");
  }
}
class Integration {
  public void operation1() {
    System.out.println("Integration.operation1()");
  }
  public void operation2() {
    System.out.println("Integration.operation2()");
  }
  public void operation3() {
    System.out.println("Integration.operation3()");
  }
}
class Implementation1 implements Task {
  private Integration1 delegate = new Integration1();
  public void task1() {
    System.out.println("Implementation1.facility1");
    delegate.method1();
  }
  public void task2() {
    System.out.println("Implementation1.facility2");
    delegate.method2();
  }
  public void task3() {
    System.out.println("Implementation1.facility3");
    delegate.method2();
    delegate.method1();
  }
  public void task4() {
    System.out.println("Implementation1.facility4");
    delegate.method1();
  }
}
class Implementation2 implements Task {
  private Integration delegate = new Integration();
  public void task1() {
    System.out.println("Implementation2.facility1");
    delegate.operation1();
  }
  public void task2() {
    System.out.println("Implementation2.facility2");
    delegate.operation2();
  }
  public void task3() {
    System.out.println("Implementation2.facility3");
    delegate.operation3();
  }
  public void task4() {
    System.out.println("Implementation2.facility4");
    delegate.operation1();
  }
}
public class BridgeStructure {
  public static void main(String[] args) {
    ClientService1 cs1 = new ClientService1(new Implementation1());
    cs1.serviceA();
    cs1.serviceB();
    cs1 = new ClientService1(new Implementation2());
    cs1.serviceA();
    cs1.serviceB();
    ClientService2 cs2 = new ClientService2(new Implementation1());
    cs2.serviceC();
    cs2.serviceD();
    cs2.serviceE();
    cs2 = new ClientService2(new Implementation2());
    cs2.serviceC();
    cs2.serviceD();
    cs2.serviceE();
  }
}