使用Fluent Interface:Fluent Interface可以使代码更短,更容易编写。
原先创建和赋值是这么写:
The most common way I see this kind of thing built up is like this:
private void makeNormal(Customer customer) {
Order o1 = new Order();
customer.addOrder(o1);
OrderLine line1 = new OrderLine(6, Product.find(“TAL”));
o1.addLine(line1);
OrderLine line2 = new OrderLine(5, Product.find(“HPK”));
o1.addLine(line2);
OrderLine line3 = new OrderLine(3, Product.find(“LGV”));
o1.addLine(line3);
line2.setSkippable(true);
o1.setRush(true);
以后可以这么写:
private void makeFluent(Customer customer) {
customer.newOrder()
.with(6, “TAL”)
.with(5, “HPK”).skippable()
.with(3, “LGV”)
.priorityRush();