Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

依赖注入 #8

Open
wants to merge 4 commits into
base: gh-pages
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions _posts/06-01-01-Dependency-Injection.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
---
title: Dependency Injection
title: 依赖注入
---

# Dependency Injection {#dependency_injection_title}
# 依赖注入 {#dependency_injection_title}

From [Wikipedia](http://en.wikipedia.org/wiki/Dependency_injection):
引用 [维基百科](http://en.wikipedia.org/wiki/Dependency_injection):

> Dependency injection is a software design pattern that allows the removal of hard-coded dependencies and makes it
> possible to change them, whether at run-time or compile-time.
> 依赖注入是软件设计模式的一种。它可以移除高耦合的代码,并且无论在编译时还是运行时可以改变它。

This quote makes the concept sound much more complicated than it actually is. Dependency Injection is providing a component
with it's dependencies either through constructor injection, method calls or the setting of properties. It is that simple.
引用的这个概念听起来要比实际上复杂。依赖注入只是一个组件,它所依赖的是通过构造函数注入、方法调用或属性设置的。它是简单的!。(译者:一个对象依赖与在new这个类的时候传递给构造函数的参数。)
22 changes: 13 additions & 9 deletions _posts/06-02-01-Basic-Concept.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
---
title: 日期和时间
anchorid: basic_concept_title
isChild: true
---

## Basic Concept {#basic_concept_title}
<h2 id="basic_concept_title">基本概念</h2>

We can demonstrate the concept with a simple, yet naive example.
我们可以用一个简单而又天真的例子,来证明这个概念。

Here we have a `Database` class that requires an adapter to speak to the database. We instantiate the
adapter in the constructor and create a hard dependency. This makes testing difficult and means the `Database` class is
very tightly coupled to the adapter.
我们有一个 `Database` 的类,这个类要求适配数据库。我们在类的构造函数里实例化了这个适配器,并且创建了一个高耦合的依赖。 这才测试的时候会很困难,并且这就意味着 `Database` 类和适配器是高耦合关系。

{% highlight php %}

<?php

namespace Database;

class Database
Expand All @@ -25,12 +27,15 @@ class Database
}

class MysqlAdapter {}

{% endhighlight %}

This code can be refactored to use Dependency Injection and therefore loosen the dependency.
上面这个代码可以使用依赖来解耦合。

{% highlight php %}

<?php

namespace Database;

class Database
Expand All @@ -44,8 +49,7 @@ class Database
}

class MysqlAdapter {}

{% endhighlight %}

Now we are giving the `Database` class its dependency rather than it creating it itself. We could even create a method
that would accept an argument of the dependency and set it that way, or if the `$adapter` property was `public` we could
set it directly.
现在我们可以创建 `Database` 类的依赖了,而不用创建它本身。我们甚至可以创建一个方法,接受依赖当作参数,然后设置它,或者如果`$adapter`这个属性是`public`的,我们可以直接去设置它。
23 changes: 10 additions & 13 deletions _posts/06-03-01-Complex-Problem.md
Original file line number Diff line number Diff line change
@@ -1,26 +1,23 @@
---
title: 复杂问题
anchorid: complex_problem_title
isChild: true
---

## Complex Problem {#complex_problem_title}
<h2 id="complex_problem_title">复杂问题</h2>

If you have ever read about Dependency Injection then you have probably seen the terms *"Inversion of Control"* or *"Dependency Inversion Principle"*.
These are the complex problems that Dependency Injection solves.
如果你涉猎了依赖注入的有关内容,那么你很可能看到过 *"控制反转"* or *"依赖反转原则"* 这两个条目。这是依赖注入所能解决的复杂问题。

### Inversion of Control
### 控制反转

Inversion of Control is as it says, "inverting the control" of a system by keeping organisational control entirely separate from our objects.
In terms of Dependency Injection, this means loosening our dependencies by controlling and instantiating them elsewhere in the system.
控制反转是说,一个系统的控制反转是从我们的对象中通过保持组织控制的完全独立。
依赖反转的作用是在系统的其他地方去控制和实例化它们,以便降低耦合度。

For years, PHP frameworks have been achieving Inversion of Control, however, the question became, which part of control
are you inverting, and where to? For example, MVC frameworks would generally provide a super object or base controller that other
controllers must extend to gain access to its dependencies. This **is** Inversion of Control, however, instead of loosening
dependencies, this method simply moved them.
多年来,PHP框架已经实现了控制反转,但是,随之而来的问题是,你反转了哪一部分的控制器,而且它又在哪里?例如,MVC框架通常提供了一个超级对象,或者基础的控制器,其他的控制器必须继承和依赖它。这就是依赖反转,是简单的移动他们而不是降低耦合。

Dependency Injection allows us to more elegantly solve this problem by only injecting the dependencies we need, when we need them,
without the need for any hard coded dependencies at all.
依赖注入允许我们需要他们的时候,只有注入我们需要依赖,才能更优雅的解决这个问题,没有任何强代码的依赖性。

### Dependency Inversion Principle
### 依赖反转原则

Dependency Inversion Principle is the "D" in the S.O.L.I.D set of object oriented design principles that states one should
*"Depend on Abstractions. Do not depend on concretions."*. Put simply, this means our dependencies should be interfaces/contracts or abstract
Expand Down