构建对象 [英] Building an object

查看:120
本文介绍了构建对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这个博客中,我遇到了一种不寻常的方式来构建一个类的对象:
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 /


这是具有流畅界面构建器设计模式



正如您从文章中看到的,想法是互补的,通常一起使用(我已经看到一些称为流利的构建器),这么多,他们常常被困惑为同一件事:





使用没有流畅界面的构建器模式(例如一个具有简单设置器的构建器)。您还可以在更多的上下文中使用流畅的接口想法,而不仅仅是构建器(例如,提高一组具有许多参数和参数变体的重载方法的可读性)。

$ 似乎被高度接受为一个很好的方法来做这个(至少基于文章数量和博客文章,我看到proselytizing的想法)。


有什么好处?


每个创意都有自己独特的优点。例如,请参阅:




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:

Note 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:

这篇关于构建对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆