0% found this document useful (0 votes)
4 views3 pages

Java Abstract Factory Pattern Example

code of abstract factory pattren

Uploaded by

aroobhamid123
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views3 pages

Java Abstract Factory Pattern Example

code of abstract factory pattren

Uploaded by

aroobhamid123
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

Create an interface for Shapes.

[Link]

public interface Shape {


void draw();
}
Step 2
Create concrete classes implementing the same interface.

[Link]

public class RoundedRectangle implements Shape {


@Override
public void draw() {
[Link]("Inside RoundedRectangle::draw() method.");
}
}
[Link]

public class RoundedSquare implements Shape {


@Override
public void draw() {
[Link]("Inside RoundedSquare::draw() method.");
}
}
[Link]

public class Rectangle implements Shape {


@Override
public void draw() {
[Link]("Inside Rectangle::draw() method.");
}
}
Step 3
Create an Abstract class to get factories for Normal and Rounded Shape Objects.

[Link]

public abstract class AbstractFactory {


abstract Shape getShape(String shapeType) ;
}
Step 4
Create Factory classes extending AbstractFactory to generate object of concrete
class based on given information.

[Link]

public class ShapeFactory extends AbstractFactory {


@Override
public Shape getShape(String shapeType){
if([Link]("RECTANGLE")){
return new Rectangle();
}else if([Link]("SQUARE")){
return new Square();
}
return null;
}
}
[Link]

public class RoundedShapeFactory extends AbstractFactory {


@Override
public Shape getShape(String shapeType){
if([Link]("RECTANGLE")){
return new RoundedRectangle();
}else if([Link]("SQUARE")){
return new RoundedSquare();
}
return null;
}
}
Step 5
Create a Factory generator/producer class to get factories by passing an
information such as Shape

[Link]

public class FactoryProducer {


public static AbstractFactory getFactory(boolean rounded){
if(rounded){
return new RoundedShapeFactory();
}else{
return new ShapeFactory();
}
}
}
Step 6
Use the FactoryProducer to get AbstractFactory in order to get factories of
concrete classes by passing an information such as type.

[Link]

public class AbstractFactoryPatternDemo {


public static void main(String[] args) {
//get shape factory
AbstractFactory shapeFactory = [Link](false);
//get an object of Shape Rectangle
Shape shape1 = [Link]("RECTANGLE");
//call draw method of Shape Rectangle
[Link]();
//get an object of Shape Square
Shape shape2 = [Link]("SQUARE");
//call draw method of Shape Square
[Link]();
//get shape factory
AbstractFactory shapeFactory1 = [Link](true);
//get an object of Shape Rectangle
Shape shape3 = [Link]("RECTANGLE");
//call draw method of Shape Rectangle
[Link]();
//get an object of Shape Square
Shape shape4 = [Link]("SQUARE");
//call draw method of Shape Square
[Link]();

}
}
Reference:
[Link]
Design pattern[Link]

You might also like