We are moved to new domain
Click -> www.ehowtonow.com
Saturday, 22 July 2017

Design Patterns - Factory Pattern

Factory Pattern

Definition

The Factory pattern provides a way to use an instance as a object factory. The factory can return an instance of one of several possible classes (in a subclass hierarchy), depending on the data provided to it.

Where to use

  1. When a class can't anticipate which kind of class of object it must create.
  2. You want to localize the knowledge of which class gets created.
  3. When you have classes that is derived from the same subclasses, or they may in fact be unrelated classes that just share the same interface. Either way, the methods in these class instances are the same and can be used interchangeably.
  4. When you want to insulate the client from the actual type that is being instantiated.

Benefits

  1. The client does not need to know every subclass of objects it must create. It
  2. only need one reference to the abstract class/interface and the factory object.
  3. The factory encapsulate the creation of objects. This can be useful if the
  4. creation process is very complex.

Drawbacks/consequences

There is no way to change an implementing class without a recompile.

Class Diagram


Factory Pattern Example

This example shows how two different concrete Products are created using the ProductFactory. ProductA uses the superclass writeName method. ProductB implements writeName that reverses the name.

public abstract class Product {
public void writeName(String name) {
System.out.println("My name is "+name);
}
}
public class ProductA extends Product { }
public class ProductB extends Product {
public void writeName(String name) {
StringBuilder tempName = new StringBuilder().append(name);
System.out.println("My reversed name is" +
tempName.reverse());
}
}
public class ProductFactory {
Product createProduct(String type) {
if(type.equals("B"))
return new ProductB();
else
return new ProductA();
}
}

public class TestClientFactory {
public static void main(String[] args) {
ProductFactory pf = new ProductFactory();
Product prod;
prod = pf.createProduct("A");
prod.writeName("John Doe");
prod = pf.createProduct("B");
prod.writeName("John Doe");
}
}

When TestClientFactory is executed the result is:
c:> My name is John Doe
c:> My reversed name is eoD nhoJ

Usage example

The Connection object in the java package sql is a factory. Depending on the database driver you use you get the database vendors implementation of the Statement interface. In the following example we
actually get an OracleStatement object from the package oracle.jdbc.driver when calling createStatement.


import java.sql.*;
public class TestClientFactoryUsage {
static Connection con;
static Statement stmt;
public static void main(String[] args) {
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
con = DriverManager.getConnection("myServer", "user",
"password");
stmt = con.createStatement();
} catch(Exception e) {}
}
}

Shop and help us

Flipkart Offer Snapdeal offer Amazon.in offer Amazon.com offer
  • Blogger Comments
  • Facebook Comments
  • Disqus Comments

0 comments:

Post a Comment

Item Reviewed: Design Patterns - Factory Pattern Rating: 5 Reviewed By: eHowToNow