fluent 界面控件
The Fluent Interface Design Pattern
流利的界面设计模式
You can use the Fluent Interface design pattern to make your PHP code easier to read and maintain. "Fluent Interface" is an object-oriented design pattern that resembles message handling, where a message is passed from program to program, with each program adding a bit of value to the message. It was first given its name about a decade ago by Martin Fowler, and is now in wide use in the PHP world, considered a "best practice" by industry leaders. Fluent design allows us to do rapid application development with Laravel, and facilitates formalized automated application testing with PHPUnit. One of the defining principles of the design pattern is code that is "primarily designed to be readable and to flow." One of the most delightful effects of using a Fluent Interface design is the elegant simplicity of method chaining.
您可以使用Fluent Interface设计模式使您PHP代码更易于阅读和维护。 “ Fluent接口”是一种类似于消息处理的面向对象的设计模式,在该模式中,消息从一个程序传递到另一个程序,每个程序都为该消息添加一些值。 它大约是十年前由Martin Fowler命名的,现在在PHP世界中被广泛使用,被业界领导者视为“最佳实践”。 流利的设计使我们能够使用Laravel进行快速的应用程序开发,并借助PHPUnit促进形式化的自动化应用程序测试 。 设计模式的定义原则之一是“最初设计为易于阅读和流动的代码”。 使用Fluent接口设计最令人愉悦的效果之一就是方法链接的优雅简洁性。
Before we see a Fluent Interface in practice, let's look at some programming activities we have all encountered before, and the steps (and missteps) we have used to solve our problems. A good example is the Address Book or Contact Directory. When we start writing this application, we think, "Well, I need names and phone numbers." And that is fine for starters, but right after you get it all set up, the boss comes in. "Don't forget that you also need email addresses."
在实践中看到Fluent接口之前,让我们看一下之前所有遇到的编程活动,以及用于解决问题的步骤(和错误步骤)。 通讯录或联系人目录就是一个很好的例子。 当我们开始编写此应用程序时,我们会想到:“好,我需要姓名和电话号码。” 这对初学者来说很好,但是当您完成所有设置后,老板就会加入。“别忘了,您还需要电子邮件地址。”
OK, no problem, we can add that field to our data model.
好的,没问题,我们可以将该字段添加到我们的数据模型中。
Here's what an original data model might look like (in prototype):
这是原始数据模型的外观(在原型中):
<?php
$name = 'Ray';
$phone = '703.555.1212';
$email = 'ray@gmail.com';
Of course, we will need more than one name in our directory.
当然,我们的目录中将需要多个名称。