指示类是否在Perl中实现接口有多重要? [英] How important is it to indicate if a class implements an interface in Perl?
问题描述
我一直在和朋友讨论一个代码风格问题。我们有一系列的包通过一个命名的子程序返回一个特定类型的值来实现一个接口。例如:
I've been discussing a code style issue with a friend. We have a series of packages that implement an interface by returning a specific type of value via a named subroutine. For example:
package Foo::Type::Bar;
sub generate_foo {
# about 5-100 lines of code
return stuff here;
}
所以你可以去:
my $bar_foo = Foo::Type::Bar->generate_foo;
my $baz_foo = Foo::Type::Baz->generate_foo;
我们有很多这些都在同一个 Foo :: Type: :*
层次结构。
We have many of these, all under the same Foo::Type::*
hierarchy.
我认为这些包应该清楚地表明它们实现了 foo_generate
interface,例如:
I think the packages should clearly indicate that they implement the foo_generate
interface, e.g.:
package Foo::Type::Bar;
use base 'Foo::Type';
sub generate_foo {
...
return stuff here;
}
我认为这是好的代码风格,更清晰,探索代码。它还允许您检查 Foo :: Type :: Bar-> isa('Foo :: Type')
,看看它是否实现了接口完全是OO)。
I think this is good code style, much more clear and clean for other coders exploring the code. It also lets you check Foo::Type::Bar->isa('Foo::Type')
to see if it implements the interface (other parts of the subsystem are entirely OO).
我的朋友不同意。他提出的一些论据是:
My friend disagrees. Some arguments he makes are:
-
Foo :: Type :: *
清楚地命名,并且仅在内部项目中使用,因此不用怀疑给定包是否实现了接口 - 这些包通常很小并且是独立子系统的一部分,并且他们感觉像批处理文件或conf文件,不是很重的Perl OO代码
- Perl通过继承表示实现,这可能是复杂或有问题的, li>
- 添加
Foo :: Type
超类不添加任何值,因为它将字面上是一个空包,仅用于启用- > isa
查找 - 以编程方式指示界面实现是个人代码风格的问题
Foo::Type::*
packages are clearly named, and used only in an internal project, and therefore there's no question of wondering whether or not a given package implements an interface- the packages are often small and part of a standalone subsystem, and they feel to him like batch files or conf files, not heavy Perl OO code
- Perl expresses implementation via inheritance, which may be complex or problematic, particularly when one gets to multiple inheritance
- adding a
Foo::Type
superclass doesn't add any value, as it would literally be an empty package, used only to enable->isa
lookups - programmatically indicating interface implementation is a matter of personal code style
我们中的一个是对吗?您会做什么?
Is one or the other of us "right"? What would you do?
编辑:在示例中,将Foo :: Generator重命名为Foo :: Type
推荐答案
如果你问这些问题,我想你应该升级到Moose。在那里你可以通过创建一个角色或类和适当的抽象方法来定义你的接口。
I think you should upgrade to Moose if you are asking these questions. There you'll be able to define your interface by creating a role or class with appropriate abstract methods.
我同意你这样做可以添加有价值的信息。在Java中有一个标记接口的概念,它是一个空接口,没有方法只存在一组类标记为对某个目的有用。最常见的例子是Serializable接口。
I agree with you that doing this can add valuable information. In Java there is the concept of a "marker interface," which is an empty interface which has no methods which exists only to tag a set of classes as being useful for a certain purpose. The most common example of this is the Serializable interface.
当然,只是因为它可能是有用的并不意味着它会在你的特殊情况。 :)
Of course, just because it can be useful does not mean it will be in your particular situation. :)
这篇关于指示类是否在Perl中实现接口有多重要?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!