Skip to content

Commit

Permalink
docs: Add more detail to code example
Browse files Browse the repository at this point in the history
  • Loading branch information
mrwilson committed Oct 15, 2020
1 parent 3937afc commit b7a705d
Showing 1 changed file with 24 additions and 15 deletions.
39 changes: 24 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,34 @@ implementation "uk.co.probablyfine:femto:${version}"
## Usage

```java
// Initialise a new injector
var injector = new FemtoInjector();

// Bind to a class (searches for a valid constructor)
injector.bind(One.class);

// Bind a class to an instance
injector.bind(Two.class, new TwoImplementation());

// Depends on One, Two
injector.bind(Three.class);

// Create a new object
Three three = injector.get(Three.class);
class Example {

public static void main(String... args) {
// Initialise a new injector
var injector = new FemtoInjector();

// Bind a class
injector.bind(One.class);

// Bind a class to an implementing class
injector.bind(Two.class, TwoImplementation.class);

// Bind a class to an instance
injector.bindInstance(Three.class, new ThreeImplementation());

// Depends on One, Two, Three
injector.bind(Four.class);

// Create a new object
Four four = injector.get(Four.class);
}
}
```

## Design Decisions

- **Singleton by default**: The first call to `FemtoInjector#get` caches the result for each class and returns the same value afterwards
- **Lazy by default**: `FemtoInjector` does not initialise any classes until `.get()` is called.
- **No annotation "magic"**: Clients register components directly with the injector rather than using JSR 330 (`@Inject`) annotations.
- **Prefer standard Java over dependencies**: Keeps the library small, and the source code easy to read.
- **Prefer standard Java over dependencies**: Keeps the library small, and the source code easy to read.
- **No named bindings**: Usd the type system to extend and name classes instead.

0 comments on commit b7a705d

Please sign in to comment.