- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
在此上下文 ( Google Mock ) 中,依赖注入(inject)(控制反转)是什么意思:
Let's look at an example. Suppose you are developing a graphics program that relies on a LOGO-like API for drawing. How would you test that it does the right thing? Well, you can run it and compare the screen with a golden screen snapshot, but let's admit it: tests like this are expensive to run and fragile (What if you just upgraded to a shiny new graphics card that has better anti-aliasing? Suddenly you have to update all your golden images.). It would be too painful if all your tests are like this. Fortunately, you learned about Dependency Injection and know the right thing to do: instead of having your application talk to the drawing API directly, wrap the API in an interface (say, Turtle) and code to that interface:
class Turtle {
...
virtual ~Turtle() {}
virtual void PenUp() = 0;
virtual void PenDown() = 0;
virtual void Forward(int distance) = 0;
virtual void Turn(int degrees) = 0;
virtual void GoTo(int x, int y) = 0;
virtual int GetX() const = 0;
virtual int GetY() const = 0;
};
通过类在应用程序代码和绘图API之间添加一层与DI有什么关系?在许多有关依赖注入(inject)的Java示例中,通常不应在类内部具体创建对象。相反,它应该在别处创建以分离两个对象之间的实现耦合。例如(来源 codeproject ):
解决方法:
当我在 Stackoverflow 上搜索有关 DI 的答案时,通常会在 Java 的上下文中询问。一些示例使用了 Java GUI。通常这些例子是如此简单和明显,以至于我看不出它的意义,除了更好的设计和更少的耦合。然而,我要学习的是它背后的意义。如 wiki 中所定义, 控制反转 (IoC) 意味着您反转代码的控制流。那么,它如何适用于 Google 案例?与程序风格相比,实际流程如何倒转?我以为代码是从上到下逐行顺序执行的,而不是从下到上?
最佳答案
维基百科对这些有很好的定义:
依赖注入(inject)只是类通过接口(interface)(图形 API)与另一个类交互的一种奇特方式,它提供了一种改变接口(interface)指向的方式(即注入(inject)对另一个类的依赖)。
对于控制反转,维基百科提到了工厂模式之类的东西。
它还提到了 setter 注入(inject)(使用 setter 函数更改接口(interface)实现)、构造注入(inject)(从构造函数设置接口(interface)实现)或接口(interface)注入(inject)(从另一个接口(interface)请求接口(interface)实现)并指出这些是依赖类型注入(inject)。
这就是这里发生的事情——程序可以使用 setter 方法(控制反转)更改海龟程序的绘图 API(依赖注入(inject))。
这允许你有一个像这样的测试类:
struct DrawingTester : public DrawingInterface
{
void move_to(long x, long y) { printf("moveto %d %d\n", x, y); }
void line_to(long x, long y) { printf("lineto %d %d\n", x, y); }
};
并通过测试程序驱动它:
int main(int argc, char **argv) {
DrawingTester drawing;
Turtle t;
t.setDrawingApi(&drawing);
t.runProgramFromFile(argv[0]);
return 0;
}
然后您可以使用 DrawingTester 的预期输出来测试 turtle/logo 测试程序。例如:
# test1.logo
MOVE 5, 7
# test1.calls
moveto 5 7
并通过测试套件(例如 https://github.com/rhdunn/cainteoir-engine/blob/master/tests/harness.py 和 https://github.com/rhdunn/cainteoir-engine/blob/master/tests/metadata.py)驱动它。
关于c++ - C++ Google Mock 指南提到的依赖注入(inject),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8678017/
这里的这个问题对 updating Google Sheets charts linked to Google slides 有一个简洁的解决方案. function onOpen() { var
我正在尝试将 Google 表单添加到 Google 类作业中,但似乎不可能。 首先,它在这里 ( https://developers.google.com/classroom/reference/
出于某种原因,无论我做什么以及我如何尝试,这个日期格式化程序都不起作用。工具提示仍然显示错误的格式。你可以试试代码here . 在代码中我必须注释掉 formatter.format(dataTabl
我目前正在使用访问 token 和刷新 token 从 Google Analytics Reporting API (v4) 中提取数据。当我致力于自动从 Google Analytics 中提取数
我已在 Google 云端硬盘中创建了一个文件夹,例如测试一下,放入3个文件 a.jpg, b.jpg, c.jpg 我希望在同一帐户下的 Google 电子表格中访问文件,例如生成图像文件的链接,可
电子表格 A 是欢迎新移民来到我们小镇的团队的主数据源。它里面有大量非常敏感的数据,不能公开,哪怕是一点点。 (我们谈论的是 child 的姓名和出生日期以及他们在哪里上学……保证电子表格 A 的安全
有没有办法在 Google 文档中编写 Google Apps 脚本以从 Google 表格中检索仅限于非空白行的范围并将这些行显示为表格? 我正在寻找一个脚本,用于使用 Google Apps 脚本
有没有办法在 Google 文档中编写 Google Apps 脚本以从 Google 表格中检索仅限于非空白行的范围并将这些行显示为表格? 我正在寻找一个脚本,用于使用 Google Apps 脚本
尝试检索存储在 google firebase 中名为条目的节点下的表单条目,并使用谷歌工作表中的脚本编辑器附加到谷歌工作表。 我已将 FirebaseApp 库添加到谷歌表脚本编辑器。然后我的代码看
是否可以将我的 Web 应用程序的登录限制为仅限 google 组中的帐户? 我不希望每个人都可以使用他们的私有(private) gmail 登录,而只能使用我的 google 组中的用户。 最佳答
我们想使用 Google 自定义搜索实现 Google 附加链接搜索框。在谷歌 documentation , 我发现我们需要包含以下代码来启用附加链接搜索框 { "@context"
我想将特定搜索词的 Google 趋势图表添加到我的 Google Data Studio 报告中,但趋势不是数据源列表中的选项。我也找不到嵌入 JavaScript 的选项。是否可以将趋势图表添加到
是否可以将文件从 Google Drive 复制到 Google Cloud Storage?我想它会非常快,因为两者都在类似的存储系统上。 我还没有看到有关无缝执行此操作的任何方法的任何信息,而无需
之间有什么区别 ga('send', 'pageview', { 'dimension1': 'data goes here' }); 和 ga('set', 'dimension1', 'da
我正在尝试记录每个博客站点作者的点击率。 ga('send', 'pageview'); (in the header with the ga code to track each page) ga(
我设置了 Google Tag Manager 和 2 个数据层变量:一个用于跟踪用户 ID,传递给 Google Analytics 以同步用户 session ,另一个用于跟踪访问者类型。 在使用
我在我们的网站上遇到多个职位发布的问题。 我们在加拿大多个地点提供工作机会。所有职位页面都包含一个“LD+JSON ”职位发布的结构化数据,基于 Google 的职位发布文档: https://dev
公司未使用 Google 套件,使用个人(消费者)帐户(甚至是 Google 帐户)违反公司政策。 需要访问 Google Analytics - 没有 Google 帐户是否可能? 谢谢 最佳答案
我想分析人们使用哪些搜索关键字在 Play 商店中找到我的应用。 那可能吗?我怎么能这样做? 最佳答案 自 2013 年 10 月起,您可以关联您的 Google Analytics(分析)和 Goo
Google Now 和 Google Keep 中基于时间和位置的提醒与 Google Calendar 事件提醒不同。是否有公共(public) API 可以访问 Now 和 Keep 中的这些事
我是一名优秀的程序员,十分优秀!