为我建议一个更好的 API 方法名称
简介:
我正在开发一个 API,它提供对 Picasa、Flickr 和其他一些图像服务的访问。
我有一个 WebAlbum 类(它提供对嵌套照片、相册(如果允许)和一些元信息的访问。
我的 API 不仅允许用户阅读相册,还允许他们创建新相册。一般情况下,为了创建新相册,API 用户应使用工厂方法来创建相册,然后调用方法 WebGallery#addAlbum (newAlbum)
。
但 Flickr 不允许创建空相册,它要求任何新相册中至少有一张预定义的照片(可能是为了制作漂亮的相册预览)。在 Flickr 术语中,第一张照片称为主照片
。因此,为了为 Flickr 创建相册,用户应该使用工厂方法,然后将图像添加到新相册,然后调用 WebGallery#addAlbum (newAlbum)
。
问题:
目前 WebAlbum 类有这个方法,
public interface WebAlbum {
...
public boolean requiresPrimaryPhoto ();
}
我不能保留这个名称,因为 PrimaryPhoto 只是一个 Flickr 术语。我可以将其更改为
public interface WebAlbum {
...
//with spaces: requires one added photo to create new album
public boolean requiresOneAddedPhotoToCreateNewAlbum ();
}
请建议一个具有相同含义的较短名称。
Introduction:
I'm working on an API which provides access to Picasa, Flickr and some other image services.
I have a class WebAlbum (it provides access to nested photos, albums if allowed, and some meta information).
My API allows the user not only to read albums but it also allows them to create new albums. In the general case, in order to create new album, the API user should use a factory method, which creates an album and then call the method WebGallery#addAlbum (newAlbum)
.
But Flickr doesn't allow creation of empty albums, it requires at least one predefined photo in any new album (to make nice album preview probably). In Flickr terms this first photo is called the Primary Photo
. So in order to create an album for Flickr, user should use a factory method, then add an image to the new album, and then call WebGallery#addAlbum (newAlbum)
.
Problem:
At the moment WebAlbum class has this method
public interface WebAlbum {
...
public boolean requiresPrimaryPhoto ();
}
I can't leave this name because PrimaryPhoto is just a Flickr term. I can change it to
public interface WebAlbum {
...
//with spaces: requires one added photo to create new album
public boolean requiresOneAddedPhotoToCreateNewAlbum ();
}
Please suggest a shorter name which has the same meaning.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
布尔值 isEmptyAlbumAllowed
boolean isEmptyAlbumAllowed
public boolean needDefault;
或者,更具描述性
public boolean needDefaultImg;
编辑
您应该问自己的另一个问题是该属性是否应该公开。如果您想让管理相册的体验在所有后端保持一致,那么您的图书馆可能可以在需要时提供默认图像。也许是您的应用程序的徽标。无论如何,用户不太可能长时间拥有空相册。
public boolean needsDefault;
or, more descriptive
public boolean needsDefaultImg;
EDIT
Another question you should ask yourself is if this property even should be exposed. If you want to make the experience of managing albums consistent on all backends, then maybe your library could provide default images where required. A logo for your app, maybe. Users are unlikely to have empty albums for very long anyway.
我会选择
或
I would go with
or
我会使用像allowsEmptyAlbum或emptyAlbumPermission这样的东西
,也就是说,添加一个额外的方法意味着该类的用户需要知道这甚至可能是一个问题,并记住在添加相册之前进行检查。这可能是一个问题,因为大多数开发人员“希望快速完成工作”并且不知道服务之间的差异。
即使在文档中添加注释也是不够的,因为许多调用“addAlbum”的人永远不会阅读该方法的文档,因为它看起来很简单(有关详细信息,请参阅我的研究)。
理想情况下,您可以为每个服务创建不同的工厂(并在其中提供信息),或者,如果您必须使用单个 API,请找到一种优雅地失败的方法,或者添加占位符图像。
I would use something like allowsEmptyAlbum or emptyAlbumPermitted
That being said, adding an extra method means that the user of the class needs to know that this could even be an issue and remember to make the check before adding the album. This could be an issue because most developers "want to get things done fast" and would not know about the differences between services.
Even adding a note in the documentation is not enough because many folks calling "addAlbum" would never read the documentation of the method since it seems straightforward (see my research for details).
Ideally, you would either be able to create different factories for each service (and provide the information there), or, if you have to use a single API, find a way of gracefully failing or maybe adding a placeholder image.
我认为你可以通过删除多余的专辑部分来使其更短。
I think you can make it even shorter by removing the redundant Album portion.
创建 FlickerWebAlbum 和 PicasaWebAlbum 类。它们中的每一个都代表每个提供商的特定行为。
Create FlickerWebAlbum and PicasaWebAlbum classes. Each of them will represent behavior specific to each provider.
对您的问题的简短回答:
较长的答案:并非所有网络相册都共享默认照片要求,因此它是使用继承的完美候选者。将该行为移至 Flickr 特定子类。您可以执行类似将 defaultImage 的创建添加到 Flickr.init() 中的操作。
The short answer to your question:
The longer answer: the default photo requirement isn't shared by all WebAlbums, so it's a perfect candidate for using inheritance. Move that behavior to a Flickr specific subclass. You could do something like add the creation of that defaultImage to a Flickr.init().