2

Consider a class OriginalClass that might or might not be available on runtime. OriginalClass has a method doSomething which should be executed if its class is available.

A way of solving this is creating a class that also has a doSomething method that calls the OriginalClass.doSomething using reflection. Something like this:

public class CompatibilityClass {

    private static Method originalClass_doSomething = null;

    static {
        initCompatibility();
    };

    private static void initCompatibility() {
        try {
            originalClass_doSomething = Class.forName("originalClass").getMethod("doSomething", new Class[] {});
        } catch (NoSuchMethodException nsme) {
        } catch (SecurityException se) {
        } catch (ClassNotFoundException cnfe) {}
    }

    public static void doSomething() {
        if (originalClass_doSomething != null) {
            try {
                originalClass_doSomething.invoke(null, new Object[]{});
            } catch (Exception e) {}
        }
    }

}

What is the name of the design pattern applied here? I suspect it's either Adapter, Bridge, Facade or Proxy, but I'm not sure which.

Dave Schweisguth
  • 36,475
  • 10
  • 98
  • 121
hpique
  • 119,096
  • 131
  • 338
  • 476
  • The answer goes in the answer field, not the question title field. Geez. – BoltClock Jun 20 '11 at 20:13
  • It's a way to make the title more useful. If not there's no way to differentiate this question from the other "What is the name of this pattern?" questions. Geez. ;) – hpique Jun 20 '11 at 20:15

3 Answers3

2

I'd say it's the proxy pattern.

You've create a proxy class that wraps the gory reflection stuff and delegates the method call to a different object.

A proxy, in its most general form, is a class functioning as an interface to something else. The proxy could interface to anything: a network connection, a large object in memory, a file, or some other resource that is expensive or impossible to duplicate.

You pattern is quite similar to something like performing some method call over a network.

aioobe
  • 413,195
  • 112
  • 811
  • 826
  • And what kind of Proxy would that be? A Remote Proxy, even if the original class is not exactly "remote"? http://wiki.java.net/bin/view/Javapedia/ProxyPattern – hpique Jun 20 '11 at 19:14
  • 1
    I agree.You are creating a proxy. – Mangesh Jun 20 '11 at 19:15
  • @hgpc, yeah. A remote proxy is probably the best match of those four. Just as in this situation though, I've seen proxies that doesn't really fit any of those four categories. – aioobe Jun 20 '11 at 19:30
  • @hgpc: As an example, I [implemented the observer pattern](http://whathecode.wordpress.com/2010/11/01/java-generic-observer/) using a proxy. The call is proxied to all listeners. No idea how such a proxy would be called. ;p – Steven Jeuris Jun 20 '11 at 19:43
2

Smells like proxy to me. But aren't you better off using Java's default Dynamic Proxy API?

Definition of proxy:

A proxy forces object method calls to occur indirectly through the proxy object, which acts as a surrogate or delegate for the underlying object being proxied. Proxy objects are usually declared so that the client objects have no indication that they have a proxy object instance.

Steven Jeuris
  • 18,274
  • 9
  • 70
  • 161
  • How would Dynamic Proxy API work with static methods, though? – hpique Jun 20 '11 at 19:27
  • @hgpc: You'll have to verify this, but as far as I remember the proxy works through an interface. Where you delegate the calls to doesn't matter, static or instance methods ... – Steven Jeuris Jun 20 '11 at 19:29
1

Simple explanation:

  • Adapter: when you have two classes (A and B) that are semantically equivalent/similar, but have different interfaces. Adapter implements interface of A but delegates to B or vice-versa so A and B can be used interchangeably
  • Bridge - typically used with whole inheritance tree (I never used it though)
  • Facade - hide complexity of one or more classes behind simpler interface
  • Proxy - same interface as the target object, delegating to it, typically used for lazy loading and decoupling from target.

So your code sample looks like a Proxy.

Tomasz Nurkiewicz
  • 334,321
  • 69
  • 703
  • 674