java少用_绝不在java中用string(或者少用)

不要使用未包装的String or long or int. 这是为什么呢? 因为这些原始类型没有语义含义。 他们很难理解,维护以及扩展. 我一直再宣传这个概念 ,“Object calisthenics” finally prompted be to write this post.

假如现在有一个关于电影预订的系统。

更新:如果你只是丢下一个评论告诉我你找到了反抗我的理由,好吧,只是不要这么做。我欣赏你的意见,但坐下来,想想和并去实践。当你读别人写的代码“String id”,你会疑惑id到底是什么,回来读读我这篇文章代码。

.把

public void bookTicket(

String name,

String firstName,

String film,

int count,

String cinema);

和这段比。(我所知面向对象编程)

public void bookTicket(

Name name,

FirstName firstName,

Film film,

Count count,

Cinema cinema);

二是非常容易理解的,尤其是当你的IDE是完成一个方法调用bookticket(String arg0,String  arg1,String  arg2,int arg3,String arg4)与bookTicket(Name arg0, FirstName arg1, Film arg2, Count arg3, Cinema arg4). 。二是更容易阅读。

void book(String orderId);

void book(OrderId order);

对比。

在第一个例子中‘开发’者看着代码会有两种疑问

a.) 从哪获取orderId 同时 b.) orderId 到底是1212", "ABC-123"或者 "12-34-45-SCHWEINEBACKE",这三种情况的哪一个. 在第二个例子中,他可以搜索这个OrderId类的位置,发现关于它的使用,读javadoc,同时知道验证,并且应用到项目中.(译者以为这就是方便项目合作‘开发’)。 你可能会想 orderId 就是orderId,很容易理解。遗留系统通常不一致的更改id,命名和语义. 我见过的系统把订单 ID 命名为“OrderID”,“auftragsid”,“id”等其他几个命名,同时它们都表示同一个!

一个有语义类比String更容易理解。‘开发’者们不易陷入混乱。如果你依靠静态类型和使用startic型语言(对象和引用)然后你最大化效益,创造更多的类。将来OrderId class很容易用long来代替int,并且仍然可以使用验证和id的生成逻辑。但是更难扩展的以字符串为基础的版本。

通过优雅的接口实现

类应该被实现为一个简单的域类,有时是不可改变的值对象,它只包字符串并将某些语义附加到字符串中。

public class Name {

public Name(String name) {

...

}

public static Name name(String name) {

return new Name(name);

}

}

人们会疑问为什么这么麻烦。假定如下

new Customer(new FirstName("Stephan"), new Name("Schmidt"));

是不是比一个字符串语义更麻烦

new Customer("Stephan", "Schmidt");

尽管这样,第一个例子很容易理解, 通过本地静态方法可以改写成

new Customer(firstName("Stephan"), name("Schmidt"));

这也解决了许多争论,‘开发’商从阅读代码不理解每个参数的手段,尤其是在较长的(重构参数对象!)参数列表。这是另一种方法,以流利的界面建设者。

我的最后一篇文章是如何使用不可变对象的泛型,也可以用值对象代替原始的。

new Point(10,10);

new Point(x(10), y(10));

where x(10) and y(10) create Xpos and Ypos value objects.

面试 域对象与primitivs

一个我经常喜欢问的问题是关于价格搜索. 我通常给他们如下的样子

... searchByPrice(...)

并让面试者补全代码。一些人会这样写

Vector searchByPrice(double start, double end)

which is bad code from several points of view (using double for money, no domain objects, untyped Vector).

这个是很差的代码,从某些方面看。

Others with more domain based thinking write

其他基于领域考虑的面试者这样写

List searchByPriceRange(Price start, Price end)

or even us the Range pattern by Fowler:

List searchByPriceRange(PriceRange priceToSearch)

最后的解决方案很容易扩展和理解。这个问题,往往开始于接口设计的一个有趣的讨论,架构和领域建模。不管你对这个面试问题有什么想法,不要忘记一次,对所有的人:不要使用double定义money。

谢谢你们的倾听,不要使用String,int和long(或double定义money)。

Update:

If you find the usage of Classes instead of Strings repulsive, look at another example, zip code. Most people I’ve seen in lots of code use a primitive for zip code which creates a lot of problems when going i18n.

Customer {

String name;

String street;

String city;

String zip;

}

(some will have used int for the zip code but get faster into trouble than the String users)

instead of

Customer {

String name;

Address address;

}

Address {

ZipCode code;

}

Still think Strings are a good idea in your code or “the simplest thing that could possibly work”?

你所谓的简单,其实对别人来说不简单。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值