Bill Zhang
Bill Zhang

Reputation: 1

Dependency between two class

class Foo {}
class Bar {
  Foo foo;
}

Since Foo is a type of field of Bar, can we safely claim that Bar depends on Bar? What if foo == None? Does that mean Bar no longer depends on Bar?

Upvotes: 0

Views: 112

Answers (1)

Superchamp
Superchamp

Reputation: 146

This comes down to the difference between composition and aggregation. In the example you have

class Foo {}
class Bar {
  Foo foo;
}

Here, Bar can exist independent of Foo because foo can be null - a weak dependence.

However, if you had the same relationship defined this way:

class Foo {}

class Bar {
  Foo foo;
  Bar(Foo foo){
     this.foo = foo;
  }
}

then, it is a composition relationship, where Bar can not exist without Foo.

Upvotes: 1

Related Questions