DP101: polymorphism and Factory Method
Given the below pt 1. (polymorphism) can be replaced by pt 2. a Factory Method pattern.
Question: Whenever we have a polymorphism situation like pt 1, should we 'automatically' use FM
or another way is in general what is the number of concrete subclasses that would trigger
the use of FM? at least 2? 1M Thanks.
1. polymorphism
publicinterface IDeveloper
{
publicvoid Code();
}
class JavaDeveloperimplements IDeveloper
{
publicvoid Code()
{
}
}
class CSharpDeveloperimplements IDeveloper
{
publicvoid Code()
{
}
}
IDeveloper developer =new JavaDeveloper();
or
IDeveloper developer =new CSharpDeveloper();
developer.Code();
2. Factory Method pattern
class DeveloperFactory
{
publicstatic IDeveloper getDeveloper(String devType)
{
IDeveloper developer=null;
if (devType.equals("JAVA"))
developer=new JavaDeveloper();
if (devType.equals("CSHARP"))
developer=new CSharpDeveloper();
return developer;
}
}
IDeveloper anotherdeveloper = DeveloperFactory.getDeveloper("JAVA");
anotherdeveloper.Code();
[2445 byte] By [
PaulPhuoca] at [2007-9-29 21:07:56]

> Given the below pt 1. (polymorphism) can be replaced
> by pt 2. a Factory Method pattern.
>
Factory Method emphasizing to create an object return to user
You can also use polymorphism in the created object as well
which one you are going to use is depending on what you are doing
if there are a lot of subtypes that you are not prepared to implement youself
polymorphism is better than factory that without polymorphism
if you only allow user to get the objects without a lot of implementation, Factory is a good choice
i believe you know that polymorphism is not only be used in interface
actually, i don't thing which one is better than the other
because i can't see any contradiction between them
maybe cursory relevance, http://forum.java.sun.com/thread.jsp?forum=4&thread=228069
Hi,
To answer pt 2, it is good to have factory if there is customization need to be done when creating the product object or if you want to restrict / put limitation how the product object is created (eg: object is pooled, object is singleton, etc)
.....
if ( "JAVA".equals(arg)) return new JavaDeveloper(); // default constructor is used
if ( "JAVA2".equals(arg)) return new JavaDeveloper( 2 ); // using different constructor
if ( "CSHARP".equals(arg)) return CSharpDeveloper.getInstance(); // singleton is used
throw new IllegalArgumentException("DeveloperFactory is not configured for "+arg);
....
Whereas, without Factory it is hard to figure out how each class need to be instantiated.
Alex
richardwat,
::i believe you know that polymorphism is not only be used in interface
public abstract class Developer
{
public abstract void Code();
}
public class JavaDeveloper extends Developer
{
public void Code()
{
}
}
public class CSharpDeveloper extends Developer
{
public void Code()
{
}
}
Developer developer = new JavaDeveloper();
developer.Code();
Am I missing anything else?
-
Alex,
From the book GoF, Factory Method's intent:
Define an interface for creating an object, but let subclasses
decide which class to instantiate...
Question: Implementation-wise, the word interface can be implemented
either by keywords: interface or abstract. Is that correct?
public interface IDeveloper
{
public void Code();
}
OR
public abstract class Developer
{
public abstract void Code();
}
An Interface has no implemention of the methods. So unless your base class is going to have imiplemented methods, make it an interface.
>Question: Implementation-wise, the word interface can be implemented
either by keywords: interface or abstract. Is that correct?
In my opinion, interface is used to share a common platform, function or idea. For example, java.io.Serializable is to mark that any class/interfaces which implements this interface are serializable, java.awt.event.MouseListener is an interface for classes/interface which interested on mouse movement.
In other hand, abstract class is used as base class to provide a structure/framework for sub classes to follow.
For example:
public interface Action{
public Object perform(Map param) throws SecurityException;
}
and Abstract class gives us a base framework of Action:
public abstract class SecureAction implements Action {
public final void securityCheck(Map param) {
if (param!=null &&
"admin".equals(param.get("username")) &&
"abc123".equals(param.get("password"))) return;
throw new SecurityException();
}
public final Object perform(Map param) throws SecurityException{
securityCheck(param);
performImpl(param);
}
public abstract performImpl(Map param);
}
Here, SecureAction provide a framework to enforce the classes which inherit from it to implement performImpl() which only be called after authentication is succesful.
rgds,
Alex
