构建对象 [英] Building an object
问题描述
在这个博客中,我遇到了一种不寻常的方式来构建一个类的对象:
http://marchwicki.pl/blog/2010/11/building-a-pojo-in-an-elegant-way/ 。这是一个很好的方法来做到这一点。
我遇到了一种不寻常的方式来构建一个对象本博客中的类: http:// marchwicki .pl / blog / 2010/11 / building-a-pojo-in-an-elegant-way / 。
这是具有流畅界面的构建器设计模式。
正如您从文章中看到的,想法是互补的,通常一起使用(我已经看到一些称为流利的构建器),这么多,他们常常被困惑为同一件事:
-
-
A 流畅界面是一种API风格,它使用上下文和方法链接来提高代码的可读性。
- 请参阅Martin Fowler关于流畅界面的帖子:
http://www.martinfowler。 com / bliki / FluentInterface.html
- 请参阅Martin Fowler关于流畅界面的帖子:
使用没有流畅界面的构建器模式(例如一个具有简单设置器的构建器)。您还可以在更多的上下文中使用流畅的接口想法,而不仅仅是构建器(例如,提高一组具有许多参数和参数变体的重载方法的可读性)。
$ 似乎被高度接受为一个很好的方法来做这个(至少基于文章数量和博客文章,我看到proselytizing的想法)。
有什么好处?
每个创意都有自己独特的优点。例如,请参阅:
Builder - 何时使用构建器模式?
Fluent界面 - 什么是流畅的界面?
I came across a rather unusual way to build an object of a class in this blog : http://marchwicki.pl/blog/2010/11/building-a-pojo-in-an-elegant-way/. Is this a good way to do this. What are the benefits?
解决方案I came across a rather unusual way to build an object of a class in this blog : http://marchwicki.pl/blog/2010/11/building-a-pojo-in-an-elegant-way/.
This is the builder design pattern with a fluent interface.
As you see from the article, the two ideas are complementary and often used together (which I've seen some call a "fluent builder"), so much so, that they are often confused as the same thing:
The builder pattern abstracts and simplifies the construction of a complex object into simpler steps.
The builder pattern is discussed in the "Gang of Four"'s Design Patterns book
Joshua Bloch talks about builders as Item 2 in his book Effective Java
A fluent interface is an API-style that uses contexts and method chaining to improve the readability of code.
- See Martin Fowler's post about fluent interfaces:
http://www.martinfowler.com/bliki/FluentInterface.htmlNote that you can use the builder pattern without a fluent interface (e.g. a builder with simple setters). You can also use the fluent interface idea in more contexts than just builders (e.g. to improve the readability of a set of overloaded methods with many parameters and parameter variations).
Is this a good way to do this?
This "fluent builder" seems highly accepted as "a good way to do this" (at least based on the number of articles and blog posts I've seen proselytizing the idea).
What are the benefits?
Each idea has its own distinct advantages/benefits. For example, see:
Builder - When would you use the Builder Pattern?
Fluent Interface - What's a fluent interface?
这篇关于构建对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!