OneCompiler

Absract factor shap

50

class Circle implements Shape {
@Override
public void draw() {
System.out.println("Drawing a Circle");
}
}

class Rectangle implements Shape {
@Override
public void draw() {
System.out.println("Drawing a Rectangle");
}
}

abstract class AbstractFactory {
abstract Shape getShape(String shapeType);
}

class ShapeFactory extends AbstractFactory {
@Override
Shape getShape(String shapeType) {
if (shapeType == null)
return null;
if (shapeType.equalsIgnoreCase("CIRCLE"))
return new Circle();
else if (shapeType.equalsIgnoreCase("RECTANGLE"))
return new Rectangle();
return null;
}
}

class FactoryProducer {
static AbstractFactory getFactory(String choice) {
if (choice.equalsIgnoreCase("SHAPE")) {
return new ShapeFactory();
}
return null;
}
}

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

    AbstractFactory shapeFactory = FactoryProducer.getFactory("SHAPE");

    Shape shape1 = shapeFactory.getShape("CIRCLE");
    shape1.draw();

    Shape shape2 = shapeFactory.getShape("RECTANGLE");
    shape2.draw();
}

}