(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.
loading..
Popular Posts
-
Just read a post from http://blog.bigml.com/2013/02/21/everything-you-wanted-to-know-about-machine-learning-but-were-too-afraid-to-ask-pa...
-
Resources about lucene Resources Introductions The API documentation contains a short and simple code example that show...
-
Repost from http://terrytao.wordpress.com/advice-on-writing-papers/ There are three rules for writing the novel. Unfortunately, no on...
-
汉字编码问题 下面是搜集的多篇关于汉字编码问题文章的合集,相信你的问题一定包含在其中,如果没有请留言,一起把这方面的内容补充全。 一、汉字编码的种类 汉字编码中现在主要用到的有三类,包括GBK,GB2312和Big5。 1、 GB2312又称国标码 ,由国家标准总...
-
This plugin can be used to read a RSS feed and transform it into a custom piece of HTML. Setup <!DOCTYPE html> <html> <...
-
Kernel Density Estimation with scipy Repost from http://glowingpython.blogspot.ca/2012/08/kernel-density-estimation-with-scipy.html ...
-
From https://de.dariah.eu/tatom/preprocessing.html Also refer to http://www.nltk.org/api/nltk.tokenize.html#module-nltk.tokenize ...
-
This is a very useful list of surveys from Doug Oard , which would be definitely helpful for those who want to enter this territory. I...
-
We examine top Python Machine learning open source projects on Github, both in terms of contributors and commits, and identify most popula...
-
SIGIR 2014 accepted Full Papers Included here is a tentative list of the full papers and their allocation into sessions. Note: titles, a...
0 Comments:
Post a Comment