Skip to content

Instantly share code, notes, and snippets.

@gadieichhorn
Created April 10, 2016 07:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gadieichhorn/5033617b4ad44ae5cfe0434a88ef1898 to your computer and use it in GitHub Desktop.
Save gadieichhorn/5033617b4ad44ae5cfe0434a88ef1898 to your computer and use it in GitHub Desktop.
Class Service {
// call some external API
// the implementation can very without effecting the consumers
// it can be mocked for testing
string getData() { return string }
}
Class Consumer
// 1. service is injected (I) not created internally or maintained by this class
// 2. consumer is dependent on service to operate (D)
// 3. service can be changed without letting the consumer know
Consumer(Service service) { // you should abstract an interface for service
// you can save the service and use it .
}
}
// when testing you can mock a test service
class MockService {
string getData { return "A"} // expected results are returned without a need to call the real service
}
// test the consumer with the mock service by injecting it instead of the real thing
new consumer( new mockService())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment