nodediggity / dependency-diagram

Visualizing the dependency graph

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

dependency-diagram

Visualizing the dependency graph

Writing/Reading Dependency Diagrams

There are different types of dependency you can represent in a dependency diagram using different annotations, lines, and arrows.

Here are the main ones:

1. Solid line, empty head = "inherits from" / "is a".

0_0_solid_line_empty_head

A solid line with an empty head denotes that a class inherits from another class.

For example:

class MyViewController: UIViewController { ... }

The MyViewController class inherits from the UIViewController class, or the MyViewController "is a" subtype of UIViewController. 0_1_inheritance

2. Dashed line, empty head = "conforms to" or "implements"

1_0_dashed_line_empty_head

A dashed line with an empty head denotes that a component conforms/implements a protocol/abstract interface.

For example:

protocol HTTPClient { ... }

class URLSessionHTTPClient: HTTPClient { ... }

1_1_conformance

The URLSessionHTTPClient conforms to the HTTPClient protocol.

3. Solid line, filled head = "depends on" / "has a" (strong dependency)

2_0_solid_line_filled_head

A solid line with a filled head denotes a strong dependency.

When a type instance depends on another type instance to exist, it's considered a stronger dependency, such as Association, Aggregation, and Composition.

For example:

class RemoteFeedLoader {
  private let client: HTTPClient

  init(client: HTTPClient) {
    self.client = client
  }
}

2_1_strong_dependency

The RemoteFeedLoader has an HTTPClient.

You cannot instantiate a RemoteFeedLoader without an HTTPClient instance. The code wouldn't even compile. So that's a strong dependency.

The RemoteFeedLoader depends on an HTTPClient to exist.

4. Dashed line, filled head = "depends on" (weak dependency)

3_0_dashed_line_filled_head

A dashed line with a filled head denotes a weak dependency.

It's important to note that a type can depend on and use another but still work without one.

For example:

class RemoteFeedLoader {
  func load(with client: HTTPClient) {
    client.doSomething()
  }
}

3_1_weak_dependency

The RemoteFeedLoader has a source code dependency to the HTTPClient because it references and uses it. But it doesn't require an HTTPClient instance to exist.

You can create a RemoteFeedLoader without an HTTPClient.

That's considered a weaker dependency, but still a dependency!

The RemoteFeedLoader uses an HTTPClient dependency in the load method. But it doesn't have one. It must be provided as a parameter.

About

Visualizing the dependency graph