有人用这种方式设计api或库代码吗?
我正在阅读一些有关如何设计好库或 API 的内容,并偶然发现了 Joshua Bloch 在 Google Tech Talks 上的精彩演讲。现在,虽然我距离专业的 API 开发人员还很远,但我认为对一堆类/函数进行编程是相似的,尽管同一件事的缩小版本 - 明确的操作分离,轻松愉快的使用,鼓励干净的代码等等。
我正在浏览一些广泛使用的开源 Java 代码并得到了这个想法(没什么新意,只是将其清晰地表达出来......)
让我们举一个例子伪代码(或 >也许 BASIC 的某种方言):
1. new label
2. set name 'hello world'
3. set color 'blue'
4. show 'topmost'
5. on click hide
现在受到 Java 代码的启发,我希望能够做这样的事情:
1. Label l = new Label()
2. .setName('Hello World')
3. .setColor('blue')
4. .show(zindex.top);
5. l.onClick = l.hide() ;
我的问题是这样的:
还有其他人像这样从伪代码开始设计 API 吗?
对于一些小事来说这是一个好主意吗?假设最多 10 个类,每个类可能有 10 个方法,每个方法内部的代码不超过 5-6 行。显然,这只是一组粗略的数字,用于显示要设计的类的大小 - 距离完整的 API 还差得很远,而不仅仅是一个业余爱好项目 - 一个专业包,可以做一些小事情,但做得很好。
有没有人发现这种方法有任何严重的缺点?
我认为一个真正的好处是它迫使您首先写下您的用例。
另一件事是名词和动词保持简单,使您的最终产品能够避免MultiPhraseAbstractParadigmDesignPatternImplementor综合症:-D
I was reading up some things about how to design a library or API well, and stumbled across Joshua Bloch's great talk at Google Tech Talks. Now although I am nowhere near a professional API developer, I think programming a bunch of classes/functions is a similar, although much scaled-down version of the same thing - clear-cut separation of actions, ease and pleasurable use, encouraging clean code, etc.
I was going through some widely used open source Java code and got this idea (nothing new, but just putting it up lucidly...)
Let us take an example pseudo code (or maybe some dialect of BASIC):
1. new label
2. set name 'hello world'
3. set color 'blue'
4. show 'topmost'
5. on click hide
Now inspired by the Java code I would want to be able to do something like this:
1. Label l = new Label()
2. .setName('Hello World')
3. .setColor('blue')
4. .show(zindex.top);
5. l.onClick = l.hide() ;
My question is this:
Does anyone else design APIs starting from pseudo-code like this?
Is it a good idea for something small? Say upto 10 classes each with maybe 10 methods, each method not more than than 5-6 lines code inside it. That is obviously just a rough set of numbers to to show the size of the classes to be designed - nowhere close to a full API and not just a hobby project - a professional package that does something small but does it well.
Has anyone found any serious drawbacks to this approach?
I think the one real benefit is that it forces you to write down your use-cases first.
The other thing is that the nouns and verbs stay simple, enabling your final product to dodge the MultiPhraseAbstractParadigmDesignPatternImplementor syndrome :-D
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是一种相当常见的设计模式,称为流畅界面。它在函数式语言中很常见,并且在其他地方也越来越流行。我第一次看到它是在Scheme中。
这是一种非常方便且可读的习惯用法,但请记住,有时您实际上确实希望在单个函数调用中初始化多个变量。例如,当必须根据数据组合设置内部状态时,或者设置和覆盖默认值的成本很高时。因此,与所有“模式”一样,请明智且深思熟虑地使用。
This is a fairly common design pattern called a fluent interface. It's common in functional languages and is gaining popularity elsewhere. I first saw it in Scheme.
It's a very convenient and readable idiom, but remember that there will often be times where you actually do want to initialize more than one variable in a single function call. For example, when there's internal state that has to be set depending on a combination of data, or when setting and overwriting default values is costly. So, as with all "patterns", use judiciously and with forethought.
Jquery 正是这样做的。来自 http://net.tutsplus.com/tutorials/javascript-ajax/jquery-1-4-released-the-15-new-features-you-must-know/:
或 1.4 之前版本:
我也在代码中用 C# 生成 WPF 做了类似的事情:
Jquery does exactly this. From http://net.tutsplus.com/tutorials/javascript-ajax/jquery-1-4-released-the-15-new-features-you-must-know/:
or for pre-1.4:
I've also done similar things in C# generating WPF in code:
是的,这就是 jQuery 的设计方式,它总是最终返回自身,以便您可以像这样链接方法。
Yes, this is the way that jQuery is designed, it always ends up returning itself so that you can chain methods like this.
我倾向于从空的类和方法开始。这实际上是一个自上而下与自下而上设计的问题。
不过,我真的更喜欢巨大的白板草图。
I tend to start with empty classes and methods. Its really a question of top down vs bottom up design.
I really prefer a huge whiteboard sketch though.