Sorry, I typed a wrong Factory class. It should be:
class Factory{
public static Interface1 getInterface(int type){
if(type == 1){
return new Imple1();
}else if(type ==2){
return new Imple2();
}
return null;
}
}
In main method:
public static void main(String []args){
Interface1 inter1 = Factory.getInterface(1);
}
On Aug 9, 9:07 pm, "[email protected]" <[email protected]>
wrote:
> Hey there,
>
> Generally you wouldn't use a factory in this way - the idea is that
> the caller just expects any implementation of the interface returned
> therefore the caller specifying the implementation to use doesn't make
> sense.
>
> Out of interest, how are you using this particular piece of code?
>
> Cheers
>
> Mark
>
> On Aug 9, 3:04 pm, zhao yi <[email protected]> wrote:
>
> > This is my code based on Factory pattern. I want to use guice and how
> > can I convert it to use guice?
>
> > interface Interface1{
> > public void sayHello();
>
> > }
>
> > class Imple1 implements Interface1{
> > @Override
> > public void sayHello() {
> > System.out.println("imple 1");
> > }
>
> > }
>
> > class Imple2 implements Interface1{
> > @Override
> > public void sayHello() {
> > System.out.println("imple 2");
> > }
>
> > }
>
> > class Factory{
> > public Interface1 getInterface(int type){
> > if(type == 1){
> > return new Imple1();
> > }else if(type ==2){
> > return new Imple2();
> > }
> > return null;
> > }
>
> > }
>
> > thanks.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"google-guice" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/google-guice?hl=en
-~----------~----~----~----~------~----~------~--~---