Definition
Provides an abstraction or an interface and lets subclass or implementing classes decide which class or method should be instantiated or called, based on the conditions or parameters given.
Where to use & benifits?
- Connect parallel class hierarchies.
- A class wants its subclasses to specify the object.
- A class cannot anticipate its subclasses, which must be created.
- A family of objects needs to be separated by using shared interface.
- The code needs to deal with interface, not implemented classes.
- Hide concrete classes from the client.
- Factory methods can be parameterized.
- The returned object may be either abstract or concrete object.
- Providing hooks for subclasses is more flexible than creating objects directly.
- Follow naming conventions to help other developers to recognize the code structure.
- Related patterns include
- Abstract Factory , which is a layer higher than a factory method.
- Template method, which defines a skeleton of an algorithm to defer some steps to subclasses or avoid subclasses
- Prototype, which creates a new object by copying an instance, so it reduces subclasses.
- Singleton, which makes a returned factory method unique
Example:
Following example shows and connection factory which which based on constant passed returns HTTP,FTP or UDP connection object.Connection.java
package com.meritmix.blog.designPattern.factory;
import java.io.IOException;
/**
* @author Ravi Yadav
* This is copyright content of MeritMix.com
* blog address is blog.meritmix.com
*/
public interface Connection {
/* connect to the URL
*/
public void connect() throws IOException;
/* tell if its using proxy
*/
public boolean usingProxy();
/* disconnect from the URL
*/
public void disconnect();
}
HttpConnection.java
package com.meritmix.blog.designPattern.factory;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
/**
* @author Ravi Yadav
* This is copyright content of MeritMix.com
* blog address is blog.meritmix.com
*/
public class HttpConnection implements Connection {
URL url;
public HttpConnection(String url) {
try {
this.url = new URL(url);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public HttpConnection(URL url) {
this.url = url;
}
@Override
public void connect() throws IOException {
// write code to connect
}
@Override
public boolean usingProxy() {
// tell if its using proxy
return false;
}
@Override
public void disconnect() {
// write code to disconnect
}
}
FTPConnection.java
package com.meritmix.blog.designPattern.factory;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
/**
* @author Ravi Yadav
* This is copyright content of MeritMix.com
* blog address is blog.meritmix.com
*/
public class FTPConnection implements Connection {
URL url;
public FTPConnection(String url) {
try {
this.url = new URL(url);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public FTPConnection(URL url) {
this.url = url;
}
@Override
public void connect() throws IOException {
// write code to connect
}
@Override
public boolean usingProxy() {
// tell if its using proxy
return false;
}
@Override
public void disconnect() {
// write code to disconnect
}
}
UDPConnection.java
package com.meritmix.blog.designPattern.factory;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
/**
* @author Ravi Yadav
* This is copyright content of MeritMix.com
* blog address is blog.meritmix.com
*/
public class UDPConnection implements Connection {
URL url;
public UDPConnection(String url) {
try {
this.url = new URL(url);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public UDPConnection(URL url) {
this.url = url;
}
@Override
public void connect() throws IOException {
// write code to connect
}
@Override
public boolean usingProxy() {
// tell if its using proxy
return false;
}
@Override
public void disconnect() {
// write code to disconnect
}
}
ConnectionFactory.java
package com.meritmix.blog.designPattern.factory;
import java.net.URL;
/**
* @author Ravi Yadav
* This is copyright content of MeritMix.com
* blog address is blog.meritmix.com
*/
public class ConnectionFactory {
public static enum CTYPE{HTTP,FTP,UDP}
public static Connection getConnection(CTYPE type,URL url) {
switch (type) {
case HTTP:
new HttpConnection(url);
break;
case FTP:
new FTPConnection(url);
break;
case UDP:
new UDPConnection(url);
break;
default:
break;
}
return null;
}
}
Related patterns:
- Abstract Factory, which is a layer higher than a factory method.
- Template method, which defines a skeleton of an algorithm to defer some steps to subclasses or avoid subclasses
- Prototype, which creates a new object by copying an instance, so it reduces subclasses.
- Singleton, which makes a returned factory method unique

No comments:
Post a Comment