是否可以使用类别来“引导”一堆相关的类别?

发布于 2025-01-04 04:38:22 字数 307 浏览 0 评论 0原文

我有一个我想要“使用”的类别类,它作用于第三方服务类。服务类有一堆 xxxRequest 和 xxxResponse 类以及用于每个 xxx 调用的数据的附加子类。

我想在应用服务类别的同时自动将类别应用到每个附加类,且范围相同。

我可以列出 use 块中的所有类别类,但有很多,我不想公开该细节。基类不受我的控制,因此我无法使用@category/@mixin。

目前,我的服务类别中有一个静态初始化方法,它执行一系列 mixin 调用(以及添加奇怪的额外构造函数)。这工作正常,但没有提供我想要的范围。

有没有我错过的更简单的方法?

I have a category class that I want to "Use" that acts on a third-party service class. The service class has a bunch of xxxRequest and xxxResponse classes plus additional child classes for data for each xxx call.

I want to automatically apply categories to each of the additional classes at the same time as the service category is applied, with the same scope.

I could list all the category classes in the use block but there are a lot and I don't want to expose that detail. The base classes are out of my control so I can't use @category/@mixin.

Currently I have a static initialise method in my service category that does a series of mixin calls (as well as adding the odd extra constructor). This works ok but doesn't provide the scoping I want.

Is there an easier way that I have missed?

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

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

发布评论

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

评论(1

生生漫 2025-01-11 04:38:22

我不确定我明白你想做什么。如果您尝试在同一代码块中同时使用多个类别,但您不想每次使用它们时都列出所有这些类别(如果不是这样,请纠正我)你的意思是),你可以把那个长的 use 放在一个以代码块(闭包)作为参数的方法中,然后使用该方法而不是长的 use 。类似于:

@Category(String)
class ShoutCategory {
    def shout() {
        this.toUpperCase() + '!'
    }
}

@Category(Integer)
class ToEnglishCategory {
    def toEnglish() {
        // Only works for integers in 0..5 for now...
        ['zero', 'one', 'two', 'three', 'four', 'five'][this]
    }
}

def useAwesomeExtensions(Closure cl) {
    use (ShoutCategory, ToEnglishCategory) {
        cl()
    }
}

useAwesomeExtensions {
    // Here we can do all sorts of magic things.
    println 3.toEnglish().shout() // Outputs "THREE!"
}

// "hello".shout() -> Would throw a MissingMethodException here

在这个愚蠢的示例中,String 和 Integer 类将是您想要增强但您无权访问的类,而 useAwesomeExtensions 的作用就像一个包含一堆内容的大类别其他类别的。希望有帮助:)

I'm not sure I understand what you are trying to do. If you are trying to use many categories at once in the same block of code but you don't want to list all those categories each time you want to use them (please correct me if that is not what you meant), you can put that long use inside a method that takes a block of code (a Closure) as a parameter and then use that method instead of the long use. Something like:

@Category(String)
class ShoutCategory {
    def shout() {
        this.toUpperCase() + '!'
    }
}

@Category(Integer)
class ToEnglishCategory {
    def toEnglish() {
        // Only works for integers in 0..5 for now...
        ['zero', 'one', 'two', 'three', 'four', 'five'][this]
    }
}

def useAwesomeExtensions(Closure cl) {
    use (ShoutCategory, ToEnglishCategory) {
        cl()
    }
}

useAwesomeExtensions {
    // Here we can do all sorts of magic things.
    println 3.toEnglish().shout() // Outputs "THREE!"
}

// "hello".shout() -> Would throw a MissingMethodException here

In this silly example, the String and Integer classes would be the classes that you want to augment but you don't have access to, and the useAwesomeExtensions acts like a big category that includes a bunch of other categories. Hope that helps :)

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