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.

|
Current Comments
0 comments so far (post your own) View All Comments Latest 10 Comments: