• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Returning an interface from a Factory

 
Barry Brashear
Ranch Hand
Posts: 303
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it possible to return an instantiated interface from a Factory? If
so could someone include an example of this?

Thanks.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65216
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since you cannot create an instance of an interface, no. But you can easily create and return an object that implements that interface.

You can also do so in an anonymous manner. For example:



Now some might say that you are instantiating the interface with this code, but really you are creating an instance of an anonymous class that implements the interface.
[ November 09, 2007: Message edited by: Bear Bibeault ]
 
Jon Parise
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By definition you cannot instantiate an interface directly, you can only instantiate it's subclasses.

The closest you can come is to instantiate one of it's subclasses. This is great for things like the factory method.

So said I had an interface called Interface1 and then I had 2 classes that implemented it called Class1 and Class2.


In my factory I could have a Create method that appears to return an instance of the interface, but truly returns an instance of it's child which is through polymorphism also an instance of Interface;


public interface Interface1{

}

public Class1 implements Interface1{
public Class1(){}
}

public Class2 implements Interface2{
public Class2(){}
}



public class Factory{

public Factory(){}

public Interface create(int option){
if(option ==1){
return new Class1();
}
else{
return new Class2();
}
}


}


You can play with instanceof to see which Class is returned by the factory. This adds a level of information hiding to your design. Based on conditions, the factory returns the class that is best suited. The user only sees the interface though and doesn't need to know if they have a class 1 or class2.

This also adds some extensibility, since with this method if a new condition arises requiring Class3, you can simply add it to the factory and leave everything else intact.

I hope that helped,

Jon
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65216
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jon Parise:
By definition you cannot instantiate an interface directly, you can only instantiate it's subclasses.

Interfaces do not have sub-classes, they have implementers. Sometimes, semantics are important.
 
Jon Parise
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good point, bad terminology use on my part.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic