A demonstration of the structure and operation of the Bridge Pattern. : Bridge Pattern « Design Pattern « Java Tutorial

Home
Java Tutorial
1.Language
2.Data Type
3.Operators
4.Statement Control
5.Class Definition
6.Development
7.Reflection
8.Regular Expressions
9.Collections
10.Thread
11.File
12.Generics
13.I18N
14.Swing
15.Swing Event
16.2D Graphics
17.SWT
18.SWT 2D Graphics
19.Network
20.Database
21.Hibernate
22.JPA
23.JSP
24.JSTL
25.Servlet
26.Web Services SOA
27.EJB3
28.Spring
29.PDF
30.Email
31.J2ME
32.J2EE Application
33.XML
34.Design Pattern
35.Log
36.Security
37.Apache Common
38.Ant
39.JUnit
Java Tutorial » Design Pattern » Bridge Pattern 
34.5.1.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();
  }
}
34.5.Bridge Pattern
34.5.1.A demonstration of the structure and operation of the Bridge Pattern.
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.