Eclipse Design Patterns – Proxy and Bridge in Workspace

home-core-workspace

1. Proxy and Bridge pattern in Core Workspace

The most important design patterns used in Core Workspace is called “Proxy and Bridge”. The most confusing question is about which part is proxy or which part is bridge.

The following diagram use IResource for demonstration, others are similar such as IFile, IFolder, IProject, IWorkspaceRoot, etc.

IResource - Proxy and Bridge

In a Workspace, each resource is represented by a handle. The handle acts like a key for a resource. Handles are small objects, and they will ever change once created.

Resource is the proxy for ResourceInfo. Everything requested to ResourceInfo is handled through its proxy Resource.
Resource is the implementer for IResource. Since there is only one implementer for a handle, this is called a simplified Bridge.

Proxy: if everything that A does is always done through B, then B is the proxy for A. This may not be precise, but it explains how we should understand the class diagram.
Bridge is a similar pattern to Adapter. In Bridge pattern, we define both abstract interface and the underlying implementation. We don’t want it to adapt to any interface. Normally there are multiple implementations instead of one in this example.

It is not important to map the design patterns you know to the pattern used here, because they are not obvious. Understanding how it works is more useful.

2. Code Example

Here is a code example that can illustrate how this pattern works.

IResource resource=ResourcesPlugin.getWorkspace().getRoot().findMember(path);
  if (resource == null) {
    return null;
  }
IJavaElement javaElement=JavaCore.create(resource);
  if (javaElement == null) {
    return null;
  }
  return javaElement;

It is worth to mention that Java Model in JDT uses the same design.

Leave a Comment