Abstract Factory Pattern

Abstract Factory patterns work around a super-factory which creates other factories. This factory is also called as factory of factories. This type of design pattern comes under creational pattern as this pattern provides one of the best ways to create an object.
In Abstract Factory pattern an interface is responsible for creating a factory of related objects without explicitly specifying their classes. Each generated factory can give the objects as per the Factory pattern.
The Abstract Factory pattern is a creational pattern which is related to the Factory Method pattern, but it adds another level of abstraction. What this means is that the pattern encapsulates a group of individual concrete factory classes (as opposed to concrete factory methods which are derived in
subclasses) which share common interfaces. The client software uses the Abstract Factory which provides an interface for creating families of related or dependent objects without specifying their concrete classes. This pattern separates the implementation details of a set of objects from its general usage.
Where to Use:
The pattern can be used where we need to create sets of objects that share a common theme and where the client only needs to know how to handle the abstract equivalence of these objects, i.e. the implementation is not important for the client. The Abstract Factory is often employed when there is a need to use different sets of objects and where the objects could be added or changed some time during the lifetime of an application.
We should use the Abstract Factory design pattern when:
1. the system needs to be independent from the way the products it works with are created.
2. the system is or should be configured to work with multiple families of products.
3. a family of products is designed to work only all together.
4. the creation of a library of products is needed, for which is relevant only the interface, not the implementation, too
Implementation:
We are going to create a Shape and Color interfaces and concrete classes implementing these interfaces. We create an abstract factory class AbstractFactory as next step. Factory classes ShapeFactory and ColorFactory are defined where each factory extends AbstractFactory. A factory creator/generator class FactoryProducer is created.
AbstractFactoryPatternDemo, our demo class uses FactoryProducer to get a AbstractFactory object. It will pass information (CIRCLE / RECTANGLE / SQUARE for Shape) to AbstractFactory to get the type of object it needs. It also passes information (RED / GREEN / BLUE for Color) to AbstractFactory to get the type of object it needs.
abstract factory pattern UML diagram
abstract factory pattern UML diagram
Java Code:
1. Create an interface for Shapes.
package com.learnjavaj2ee.abstractfactory;

public interface Shape {
void draw();
}
2. Create concrete classes implementing the same interface.
package com.learnjavaj2ee.abstractfactory;

public class Rectangle implements Shape {

@Override
public void draw() {
System.out.println("Inside Rectangle::draw() method.");
}
}
package com.learnjavaj2ee.abstractfactory;

public class Circle implements Shape {

@Override
public void draw() {
System.out.println("Inside Circle::draw() method.");
}
}
package com.learnjavaj2ee.abstractfactory;

public class Square implements Shape {

@Override
public void draw() {
System.out.println("Inside Square::draw() method.");
}
}
3. Create an interface for Colors.
package com.learnjavaj2ee.abstractfactory;

public interface Color {
void fill();
}
4. Create concrete classes implementing the same interface.
package com.learnjavaj2ee.abstractfactory;

public class Red implements Color {

@Override
public void fill() {
System.out.println("Inside Red::fill() method.");
}
}
package com.learnjavaj2ee.abstractfactory;

public class Blue implements Color {

@Override
public void fill() {
System.out.println("Inside Blue::fill() method.");
}
}
package com.learnjavaj2ee.abstractfactory;

public class Green implements Color {

@Override
public void fill() {
System.out.println("Inside Green::fill() method.");
}
}
5. Create an Abstract class to get factories for Color and Shape Objects.
package com.learnjavaj2ee.abstractfactory;

public abstract class AbstractFactory {
abstract Color getColor(String color);

abstract Shape getShape(String shape);
}
6. Create Factory classes extending AbstractFactory to generate object of concrete class based on given information.
package com.learnjavaj2ee.abstractfactory;

public class ShapeFactory extends AbstractFactory {

@Override
public Shape getShape(String shapeType) {

if (shapeType == null) {
return null;
}

if (shapeType.equalsIgnoreCase("CIRCLE")) {
return new Circle();

} else if (shapeType.equalsIgnoreCase("RECTANGLE")) {
return new Rectangle();

} else if (shapeType.equalsIgnoreCase("SQUARE")) {
return new Square();
}

return null;
}

@Override
Color getColor(String color) {
return null;
}
}
package com.learnjavaj2ee.abstractfactory;

public class ColorFactory extends AbstractFactory {

@Override
public Shape getShape(String shapeType) {
return null;
}

@Override
Color getColor(String color) {

if (color == null) {
return null;
}

if (color.equalsIgnoreCase("RED")) {
return new Red();

} else if (color.equalsIgnoreCase("GREEN")) {
return new Green();

} else if (color.equalsIgnoreCase("BLUE")) {
return new Blue();
}

return null;
}
}
7. Create a Factory generator/producer class to get factories by passing an information such as Shape or Color.
package com.learnjavaj2ee.abstractfactory;

public class FactoryProducer {
public static AbstractFactory getFactory(String choice) {

if (choice.equalsIgnoreCase("SHAPE")) {
return new ShapeFactory();

} else if (choice.equalsIgnoreCase("COLOR")) {
return new ColorFactory();
}

return null;
}
}
8. Use the FactoryProducer to get AbstractFactory in order to get factories of concrete classes by passing an information such as type.
package com.learnjavaj2ee.abstractfactory;

public class AbstractFactoryPatternDemo {
public static void main(String[] args) {

// get shape factory
AbstractFactory shapeFactory = FactoryProducer.getFactory("SHAPE");

// get an object of Shape Circle
Shape shape1 = shapeFactory.getShape("CIRCLE");

// call draw method of Shape Circle
shape1.draw();

// get an object of Shape Rectangle
Shape shape2 = shapeFactory.getShape("RECTANGLE");

// call draw method of Shape Rectangle
shape2.draw();

// get an object of Shape Square
Shape shape3 = shapeFactory.getShape("SQUARE");

// call draw method of Shape Square
shape3.draw();

// get color factory
AbstractFactory colorFactory = FactoryProducer.getFactory("COLOR");

// get an object of Color Red
Color color1 = colorFactory.getColor("RED");

// call fill method of Red
color1.fill();

// get an object of Color Green
Color color2 = colorFactory.getColor("Green");

// call fill method of Green
color2.fill();

// get an object of Color Blue
Color color3 = colorFactory.getColor("BLUE");

// call fill method of Color Blue
color3.fill();
}
}
Output:
Inside Circle::draw() method.
Inside Rectangle::draw() method.
Inside Square::draw() method.
Inside Red::fill() method.
Inside Green::fill() method.
Inside Blue::fill() method.

Benefits:
Use of this pattern makes it possible to interchange concrete classes without changing the code that uses them, even at runtime.
Factories as singletons:
An application usually needs only one instance of the ConcreteFactory class per family product. This means that it is best to implement it as a Singleton.
Drawbacks/consequences:
As with similar design patterns, one of the main drawbacks is the possibility of unnecessary complexity and extra work in the initial writing of the code.
Specific problems and implementation:
The Abstract Factory pattern has both benefits and flaws. On one hand it isolates the creation of objects from the client that needs them, giving the client only the possibility of accessing them through an interface, which makes the manipulation easier. The exchanging of product families is easier, as the class of a concrete factory appears in the code only where it is instantiated. Also if the products of a family are meant to work together, the Abstract Factory makes it easy to use the objects from only one family at a time. On the other hand, adding new products to the existing factories is difficult, because the Abstract Factory interface uses a fixed set of products that can be created. That is why adding a new product would mean extending the factory interface, which involves changes in the AbstractFactory class and all its subclasses. This section will discuss ways of implementing the pattern in order to avoid the problems that may appear.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment