A Contract Example 约定的范例

Interfaces are contracts. Interfaces do not contain any code, but simply define a set of methods that an object must implement. If an object implements an interface, we are guranteed that every method defined by the interface is valid and callable on that object. Since the contract gurantees the implementation of certain methods, type safety becomes more flexible via polymorphism.

接口就是约定。接口不包含任何代码实现,只是定义了一个对象应该实现的一系列方法。如果一个对象实现了一个接口,那么我们就能确信这个接口所定义的一系列方法都能在这个对象上使用。因为有约定保证了特定方法的实现标准,通过多态也能使类型安全的语言变得更灵活。

Polywhat? 多什么肽?

Polymorphism is a big word that essentially means an entity can have multiple forms. In the context of this book, we mean that an interface can have multiple implementations. For example, a UserRepositoryInterface could have a MySQL and a Redis implementation, and both implementations would qualify as a UserRepositoryInterface instance.

多态含义很广,其本质上是说一个实体拥有多种形式。在本书中,我们讲多态是一个接口有着多种实现。比如 UserRepositoryInterface 可以有 MySQL 和 Redis 两种实现,每一种实现都是 UserRepositoryInterface 的一个实例。

To illustrate the flexibility that interfaces introduce into strongly typed languages, let's write some simple code that books hotel rooms. Consider the following interface:

为了说明在强类型语言中接口的灵活性,咱们来写一个酒店客房预订的代码。考虑以下接口:

interface ProviderInterface{
    public function getLowestPrice($location);
    public function book($location);
}

When our user books a room, we'll want to log that in our system, so let's add a few methods to our User class:

当用户订房间时,我们需要将此事记录在系统里。所以在 User 类里面写点方法:

class User {
    public function bookLocation(ProviderInterface $provider, $location)
    {
        $amountCharged = $provider->book($location);
        $this->logBookedLocation($location, $amountCharged);
    }
}

Since we are type hinting the ProviderInterface, our User can safely assume that the book method will be available. This gives us the flexibility to re-use our bookLocation method regardless of the hotel provider the user prefers. Finally, let's write some code that harnesses this flexibility:

因为我们写出了 ProviderInterface 的类型提示,该 User 类的就可以放心大胆的认为 book 方法是可以调用的。这使得 bookLocation 方法有了重用性。当用户想要换一家酒店提供商时也就更灵活。最后咱们来写点代码来强化他的灵活性。

$location = 'Hilton, Dallas';

$cheapestProvider = $this->findCheapest($location, array(
    new PricelineProvider,
    new OrbitzProvider,
));

$user->bookLocation($cheapestProvider, $location);

Wonderful! No matter what provider is the cheapest, we are able to simply pass it along to our User instance for booking. Since our User is simply asking for an object instances that abides by the ProviderInterface contract, our code will continue to work even if we add new provider implementations.

太棒了!不管哪家是最便宜的,我们都能够将他传入 User 对象来预订房间了。由于 User 对象只需要要有一个符合 ProviderInterface 约定的实例就可以预订房间,所以未来有更多的酒店供应商我们的代码也可以很好的工作。

Forget The Details 忘掉细节

Remember, interfaces don't actually do anything, They simply define a set of methods that an implementing class must have.

记住,接口实际上不真正做任何事情。它只是简单的定义了类们必须实现的一系列方法。

results matching ""

    No results matching ""