(Abstract) Base Classes vs. Interfaces
从 LingPipe Blog 作者:lingpipe
When definining a low-level abstraction as part of a framework, say vectors and matrices to make this concrete, there’s a choice of whether to code to interfaces or specify a (possibly abstract) base class.
There are two key differences between the base class and interface approach. First, in Java (and .NET from what I gather), classes must extend exactly one other class (Object is the default), but can implement zero or more interfaces. Second, interfaces can’t specify any implementations of methods (though they may define static constants).
But as Kirill Osenkov’s blog entry Choosing: Interface vs. Abstract Class points out, there are serious downstream consequences of this choice.
Most notably, once an interface is released, it has profound backward compatibility implications. Particularly, any attempt to add a method to the interface will break backward compatibility for anyone who has implemented the interface. As long as the base class implements the added method, there’s no such problem.
The conclusion I’ve come to after several years of coding to interfaces (following the Java collections framework for guidance), is that they almost all should’ve been (abstract) base classes. The exceptions are util.Compilable, util.Scored, and other lightweight marker-type interfaces, most notably corpus.Handler.
So what do you do if you forgot a critical method in an interface? I’m in that position right now with our matrix.Vector interface. I’ve just implemented multinomial logistic regression classification (aka max entropy, aka soft max, aka log linear classifiers) and found that I need two new vector operations to make the inner loop efficient. One, I need to be able to find the non-zero dimensions of a vector, and two, I need to add a scaled sparse vector to a vector.
I’ve added the methods to the interface and to the low-level abstract implementation matrix.AbstractVector.
I’m banking on no one having implemented Vector, because there’s really no reason to do it, so changing the interface won’t be so terrible.
I’d dearly love to add a method to the tokenizer.TokenizerFactory interface to create tokenizers from character sequences as well as slices. But alas, I fear too many users have coded to the existing interface, so I’m not going to do that.
Popular Posts
-
Resources about lucene Resources Introductions The API documentation contains a short and simple code example that show...
-
汉字编码问题 下面是搜集的多篇关于汉字编码问题文章的合集,相信你的问题一定包含在其中,如果没有请留言,一起把这方面的内容补充全。 一、汉字编码的种类 汉字编码中现在主要用到的有三类,包括GBK,GB2312和Big5。 1、 GB2312又称国标码 ,由国家标准总...
-
We examine top Python Machine learning open source projects on Github, both in terms of contributors and commits, and identify most popula...
-
ACL 2013: ACCEPTED PAPERS A Bayesian Model for Joint Unsupervised Induction of Sentiment, Aspect and Discourse Representations Angel...
-
When you are testing Solr in Eclipse, you may encounter the problem that “A SPI class of type org.apache.lucene.index.codecs.Codec with na...
-
Gensim – Topic Modelling for Humans Gensim is a free Python framework designed to automatically extract semantic topics from doc...
-
This plugin can be used to read a RSS feed and transform it into a custom piece of HTML. Setup <!DOCTYPE html> <html> <...
-
lftp is an amazing command line ftp tool, which lets you operate remote files just like in a local filesystem in a terminal (bash). If y...
-
After installing Scipy, when I import optimize from Scipy, the following error occurs. Traceback (most recent call last): File &q...
-
Journals TOIS – ACM Transactions on Information Systems Publication: 467 Citation: 13992 IPM – Information Processing and Management...

0 Comments:
Post a Comment