https://github.com/techjacker/abstract-factory
Super simple dependency injection
https://github.com/techjacker/abstract-factory
Last synced: 7 months ago
JSON representation
Super simple dependency injection
- Host: GitHub
- URL: https://github.com/techjacker/abstract-factory
- Owner: techjacker
- License: mit
- Created: 2013-06-12T17:43:24.000Z (over 12 years ago)
- Default Branch: master
- Last Pushed: 2013-07-29T10:49:54.000Z (over 12 years ago)
- Last Synced: 2025-06-08T22:11:28.275Z (8 months ago)
- Language: JavaScript
- Homepage:
- Size: 180 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# abstract-factory
[](http://travis-ci.org/techjacker/abstract-factory)
## Description
Interface for abstracting classes in your modules; IoC container.
### How does using it help you?
- Make your tests easier
- Cheap dependency injection; no monolothic libs needed
## Install
```Shell
npm install abstract-factory
```
## Full Example
```JavaScript
var AbstractFactory = require('abstract-factory');
var assert = require('assert');
// var AbstractFactory = require('abstract-factory');
var originalApi = {
returnData: function () {
return 'lots of data';
}
},
cheaperApi = {
returnData: function () {
return 'less data';
}
};
var App = function (AbFactory) {
this.myInterface = AbFactory;
};
App.prototype.getData = function () {
// instead of referencing api directly, eg originalApi.returnData();
return this.myInterface.getFactory('api').returnData();
};
// you've just received funding for your great new idea
// from YCombinator, and you launch your first version, fantastic!
var MyAppAlpha = new App(new AbstractFactory({
api: originalApi
}));
// oh dear your investors cut the funding suddenly...
// ...that's okay, all you have to do is swap out a single dependency
// when instantiating your app; no need to go hunting for methods
// buried deep in your lib dir
var MyAppBeta = new App(new AbstractFactory({
api: cheaperApi
}));
assert.equal(MyAppAlpha.getData(), 'lots of data', 'alpha app returns correct data from api');
assert.equal(MyAppBeta.getData(), 'less data', 'beta app returns correct data from api');
```