Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
Abstract Factory Pattern 
 

This pattern is one level of abstraction higher than factory pattern. This means that the abstract factory returns the factory of classes.

 

Abstract Factory Pattern

                         

II Abstract Factory Pattern :

 
This pattern is one level of abstraction higher than factory pattern. This means that the abstract factory returns the factory of classes. Like Factory pattern returned one of the several sub-classes, this returns such factory which later will return one of the sub-classes.
  It provides a way to encapsulate a group of several related factories. This method is used when to return one of several related classes of objects and each of which have the capability of returning several objects of different types on request. This pattern provides separation from the implementation details of a set of objects from its general usage. This pattern hides the concrete subclass from the client and should be used when the system is independent of how the components are organized.

This pattern allows to interchange the concrete classes without changing the code that they uses even at runtime. However this pattern incurs the risk of unnecessary complexity.

Benefits: The client  does no need to specify the type of the concrete class because the abstract factory creates the actual concrete objects by reading the type of the concrete object from the configuration and returns the abstract pointer of those objects. The client can only access to these objects through their abstract interfaces. It defines a class library of products that provides expose to interface and creates the families of related objects as Kit. It tries to enforce the constraints and includes the related patterns. It provides independency required by the system from how its products are created, composed and support for a system or a family of systems to be extensible.

Usage: It is used to construct the complex objects that are independent of how to make up the objects and how the parts are assembled. The construction process must allow the constructed object to represent differently. It is used to make the concrete classes isolate from their super classes. The client code have no need to add the header files, class declarations and also no need to know about the concrete class. The abstract factory class creates the objects of the concrete class these objects are accessed by the client's code.

Let’s take an example to clearly understand this pattern. Suppose we need the configuration of a Computer.  RAM, Hard disk and Processor are the different parts of computer and workstation Server and PC are the different types of computers.

Therefore we are taking the Computer as the abstract base class.

package creational.abstractfactory;

public abstract class Computer {
public abstract Parts getHarddisk();
public abstract Parts getRAM();
public abstract Parts getProcessor();
}

package creational.abstractfactory;

public class Parts {
public String configuration;
public Parts(String configuration) {
this.configuration = configuration;
}
public String getConfiguration() {
return configuration;
}

package creational.abstractfactory;

public class PC extends Computer {
public Parts getRAM() {
return new Parts("256 MB");
}
public Parts getProcessor() {
return new Parts("Pentium3");
}
public Parts getHarddisk() {
return new Parts("40GB");
}
}

package creational.abstractfactory;

public class Workstation extends Computer {
public Parts getRAM() {
return new Parts("1 GB");
}
public Parts getProcessor() {
return new Parts("Pentium4");
}
public Parts getHarddisk() {
return new Parts("80GB");
}
}

package creational.abstractfactory;

public class Server extends Computer{
public Parts getRAM() {
return new Parts("2 GB");
}
public Parts getProcessor() {
return new Parts("DualCore");
}
public Parts getHarddisk() {
return new Parts("160GB");
}
}

package creational.abstractfactory;

public class CatagoryType {
private Computer comp;
public static void main(String[] args) {
CatagoryType type = new CatagoryrType();
Computer computer = type.getComputer("Server");
System.out.println("Harddisk: "+computer.getHarddisk().getConfiguration());
System.out.println("RAM: "+computer.getRAM().getConfiguration());
System.out.println("Processor: "+computer.getProcessor().getConfiguration());
}
public Computer getComputer(String catagoryType) {
if (catagoryType.equals("PC"))
comp = new PC();
else if(catogoryType.equals("Workstation"))
comp = new Workstation();
else if(catagoryType.equals("Server"))
comp = new Server();
return comp;
}
}

The above class gives the output like this:

Harddisk: 160GB
RAM: 2 GB
Processor: DualCore.

                         

» View all related tutorials
Related Tags: c class conversion interface io interfaces client get version ip pattern cli int this tab ie cte work expect to

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.