这个图案的名字是什么? (答案:远程代理)

发布于 2024-11-16 02:51:50 字数 1085 浏览 0 评论 0原文

考虑一个类OriginalClass,它在运行时可能可用,也可能不可用。 OriginalClass 有一个 doSomething 方法,如果其类可用,则应执行该方法。

解决此问题的一种方法是创建一个类,该类还具有使用反射调用 OriginalClass.doSomethingdoSomething 方法。像这样:

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) {}
        }
    }

}

这里应用的设计模式的名称是什么?我怀疑它是适配器外观代理,但我不确定是哪一个。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

云朵有点甜 2024-11-23 02:51:50

我想说这是代理模式

您已经创建了一个代理类,它包装了血淋淋的反射内容并将方法调用委托给不同的对象。

代理,以其最一般的形式,是一个充当其他东西的接口的类。代理可以与任何东西交互:网络连接、内存中的大对象、文件或其他一些昂贵或无法复制的资源。

您的模式非常类似于通过网络。

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.

淡紫姑娘! 2024-11-23 02:51:50

对我来说闻起来像代理。但是使用 Java 的默认动态代理 API 不是更好吗

代理的定义:

代理强制对象方法调用
通过代理间接发生
对象,充当代理或
底层对象的委托
被代理。代理对象是
通常声明以便客户端
物体没有迹象表明它们
有一个代理对象实例。

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.

关于从前 2024-11-23 02:51:50

简单解释:

  • 适配器:当您有两个在语义上等效/相似但具有不同接口的类(A 和 B)时。适配器实现 A 的接口,但委托给 B,反之亦然,因此 A 和 B 可以互换使用
  • Bridge - 通常与整个继承树一起使用(但我从未使用过)
  • Facade > - 将一个或多个类的复杂性隐藏在更简单的接口后面
  • Proxy - 与目标对象相同的接口,委托给它,通常用于延迟加载和与目标解耦。

所以您的代码示例看起来像一个代理。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文