There is Spring framework for IoC, I know.
There is Guice for IoC, I know.
There is Seasar2, which is popular in Japan, for IoC, I know.
But none of these is not what I want.
Spring is too comlicated to my brain.
(My brain accepts only easiest one:)
Guice, it sounds better to use, but i see Guice way is not the way i always recommend to use.
(If you are higher skill programmer, recommend to use.)
Seasar2, i don't say about it right now, maybe tommorow.
So I am creating a new one, called Lucy.
Here is a simple sample.
//just a interface
public interface Greeting {
String greet();
}
//Impl class. @Component must set to notify it is component.
//Also @Sinlgeton is singleton notification.(Scopes are singleton, prototype, but you can extend easily)
@Component
@Singleton
public class GreetingImpl implements Greeting {
private String name = "lucy";
public void setName(String name) {
this.name = name;
}
public String greet() {
return "Hello " + name;
}
}
//a client to use
@Component
@Prototype
public class Client {
protected Greeting greeting;
public void execute() {
System.out.println(greeting.greet());
}
//you must specify inject point.
@Inject
public void setGreeting(Greeting greeting) {
this.greeting = greeting;
}
}
//main class
public class Main2 {
public static void main(String[] args) {
Lucy.init();
final Registry registry = Lucy.getRegistry();
try {
registry.register(GreetingImpl.class);
registry.register(Client.class);
Client client = registry.get(Client.class);
client.execute();
} finally {
Lucy.destroy();
}
}
}
Verrrrrrrrrrry simple solution by annotation, as we see.
Our policy is
-small is beautiful
Small code is beautiful.
I know there are situations users want to add new feature.
But I will try to be small as much as I can beacuse small code is always good for developers.
If code is small, you can understand quickly and extend it quickly.
And maintainability is very high compared with large-footprint frameworks.
-no magic
There are no magic. IoC is a just technique, it is not magic.
So I realize auto injection is evil. You must specify injection point for maintainability.
See this code.
public class Hoge {
private Foo foo;
public void setFoo(Foo foo) {
this.foo = foo;
}
}
Ok, guess what Foo is injected to Hoge?
No body knows. There is no other information to know whether inject or not.
Let's see other example.
public class Hoge {
private Foo foo;
@Inject
public void setFoo(Foo foo) {
this.foo = foo;
}
}
Now you know the way:)
You see all the time whether Foo is injected or not.
-less is more
Less is more is always true to me.
Try to be less is so difficult, especially OSS.
But i try.Maybe i will write more detail why i love to pursue "less is more".
No comments:
Post a Comment