与界面交互? [英] interact with an interface?

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

问题描述

从我读到的内容来看,似乎可以与某个界面进行交互?

from what i've read it seems that one can interact with an interface?

例如可以说我有一个带有空方法"eat()"的接口

eg. lets say that i've got an interface with an empty method "eat()"

然后有2个子类实现此接口.

then 2 subclasses are implementing this interface.

我的控制器可以只与接口交互并使用eat()方法吗?

can my controller interact with only the interface and use it's eat() method?

在此链接中查看图片策略

推荐答案

除了访问接口中定义的常量或将其用于TypeHints之外,您无法与该接口进行交互.接口没有方法主体.它们只是为了定义实现类必须遵守的合同.

You cannot interact with an interface except for accessing any defined constants in it or using it for TypeHints. Interfaces do not have method bodys. They are solely meant to define a contract implementing classes must obey.

interface Logger
{
    const FOO = 1;
    public function log($msg);
}

echo Logger::FOO;  // 1
Logger::log($msg); // Fatal error: Cannot call abstract method Logger::log()
new Logger;        // Fatal error: Cannot instantiate interface Logger

请参见 http://php.net/manual/en/language.oop5 .interfaces.php

针对接口编码 与接口交互时,通常只不过是调用在方法中定义的方法而已. 实现它们的类中的接口.您调用实现,而不是定义.该定义仅指定对于实现该接口的每个Class,都必须有一个带有指定参数的特定方法.

What is generally meant when coding against an interface or interacting with an interface is basically nothing more than calling methods defined in an interface in the classes implementing them. You call the implementation, not the definition. The definition just specifies that for every Class implementing the interface, there has to be a specific method with the specified arguments.

请考虑以下课程:

Class DbLog implements Logger 
{
    public function log($msg) { /* log $msg to database */ }
}

Class FileLog implements Logger
{
    public function log($msg) { /* log $msg to file */ }
}

两个类都实现了Logger,因此必须具有方法log($msg).您基本上是在说:嘿,如果您想成为Logger,请确保我可以在您身上调用log()." .现在,在代码中的某个地方,您可能有一个需要记录器的类,例如

Both classes implement Logger and therefor have to have a method log($msg). You're basically saying: "hey class, if you want be a Logger, make sure I can call log() on you.". Now somewhere in your code you might have a class that needs a logger, like

class Foo
{
    protected $logger;
    public function __construct(Logger $logger)
    {
        $this->logger = $logger;
        $this->logger->log('I can haz logger! Yay!');
    }
}

Foo不在乎是否获取FileLogDbLog或任何其他 concrete Logger .它只是在乎它是否可以调用log()任何记录器. Foo对log()的功能甚至不感兴趣. Foo关心的只是能够调用log().但是,您没有在界面中调用log().您是在传递给Foo的具体类中调用它的,但是在UML图中,您将像在链接的页面中显示的那样表示它,因为您只是针对接口进行了编码.

Foo doesn't care if it gets FileLog, DbLog or any other concrete Logger. It just cares that it gets any Logger it can call log() on. Foo isn't even interested in what log() does. All Foo cares about is being able to call log(). You're not calling log() in the interface though. You are calling it in the conrete class that was passed to Foo, but in an UML diagram you'd represent this like it's shown in the page you linked, because you just coded against an interface.

这样做的主要优点是您的类之间的耦合要少得多.您可以更轻松地换出依赖关系,例如在单元测试中使用Mocks时,您的代码将更易于维护.

The main advantage of this is that your classes are much less coupled. You can more easily swap out dependencies, for instance when using Mocks in unit-testing, and your code will be more maintainable.

基本上,将接口视为概念上的标准化.例如,当您购买新的DVD播放器时,您希望它具有一个按钮,该按钮会以某种方式(您不在乎如何)使播放器播放DVD.当您按下该按钮时,您并没有按下表示DVD播放器必须具有播放按钮的通用抽象DVD接口规范,而是单击了该品牌播放器上的播放按钮的具体实现.

Basically, think of an interface as a conceptual standardization. For instance, when you buy a new DVD Player, you expect it to have a button that somehow (you don't care how, just that) makes the player play the DVD. When you press that button, you're not pressing the general abstract DVD interface specification that says a DVD player must have a play button, but you clicked the concrete implementation of a play button on this brand of player.

这篇关于与界面交互?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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