So far, we’ve explored 3 types of relationships: composition, aggregation, and association. We’ve saved the simplest one for last: dependencies.
到目前为止我已经学了三种类型的关系。现在我们要学习最后最简单的一种关系:依赖。
In casual conversation, we use the term dependency to indicate that an object is reliant upon another object for a given task. For example, if you break your foot, you are dependent on crutches to get around (but not otherwise). Flowers are dependent upon bees to pollinate them, in order to grow fruit or propagate (but not otherwise).
在日常会话中,我们经常用依赖这个词语来指示一个对象是依赖于另一个对象的,对于一个特定的过程而言。举个例子,如果你的脚受伤了,你就得依赖于拐杖行走。
A dependency occurs when one object invokes another object’s functionality in order to accomplish some specific task. This is a weaker relationship than an association, but still, any change to object being depended upon may break functionality in the (dependent) caller. A dependency is always a unidirectional relationship.
依赖关系发生于一个对象需要引入另一个对象的功能才能完成某件特定的事情的时候。但是同样的,对于被依赖的对象的功能的改变,也会导致依赖者的功能发生变化。一个依赖总是一个单向的关系。
A good example of a dependency that you’ve already seen many times is std::cout (of type std::ostream). Our classes that use std::cout use it in order to accomplish the task of printing something to the console, but not otherwise.
关于依赖的一个很好的例子是你经常看见std::cout调用。我们的类经常调用cout这个对象来完成打印的任务,而这个关系反过来不成立。
For example:
#include <iostream>
class Point
{
private:
double m_x, m_y, m_z;
public:
Point(double x=0.0, double y=0.0, double z=0.0): m_x(x), m_y(y), m_z(z)
{
}
friend std::ostream& operator<< (std::ostream &out, const Point &point);
};
std::ostream& operator<< (std::ostream &out, const Point &point)
{
// Since operator<< is a friend of the Point class, we can access Point's members directly.
out << "Point(" << point.m_x << ", " << point.m_y << ", " << point.m_z << ")";
return out;
}
int main()
{
Point point1(2.0, 3.0, 4.0);
std::cout << point1;
return 0;
}
In the above code, Point isn’t directly related to std::cout, but it has a dependency on std::cout since operator<< uses std::cout to print the Point to the console.
在上面的例子中,Point不是直接依赖于cout的,但是它对cout有一个依赖,因为运算符重载中用到了。
Dependencies vs Association in C++
There’s typically some confusion about what differentiates a dependency from an association.
总有人会对依赖和结合的关系感到困惑
In C++, associations are a relationship between two classes at the class level. That is, one class keeps a direct or indirect “link” to the associated class as a member. For example, a Doctor class has an array of pointers to its Patients as a member. You can always ask the Doctor who its patients are. The Driver class holds the id of the Car the driver object owns as an integer member. The Driver always knows what Car is associated with it.
在C++中,结合是在类层次上的两个类的关系。也就是说,一个类保留了一个直接或间接的方式链接到另一个类的对象。举个例子,医生有一堆指向病人的指针。你总是可以问医生你的病人有些什么人。司机也总是知道自己有什么车。
Dependencies typically are not represented at the class level -- that is, the object being depended on is not linked as a member. Rather, the object being depended on is typically instantiated as needed (like opening a file to write data to), or passed into a function as a parameter (like std::ostream in the overloaded operator<< above).
依赖不是在类层面表现的,被依赖的对象不是作为成员而连接的。被连接的对象通常是在需要的时候才进行实例化,或者通过函数参数传入(比如输出运算符重载)。