Factory Pattern.
public interface Shape{
public void draw();
}
public class circle implements Shape {
@Override
public void draw(){
}
}
public class rectangle implements Shape {
@Override
public void draw(){
}
}
public class square implements Shape {
@Override
public void draw() {
}
}
public class Factory {
public Shape getShape(String shapeType){
if(shapeType.equals("circle")){
return new circle();
} else if(shapeType.equals("rectangle")){
return new rectangle();
} else if(shapeType.equals("square")){
return new square();
}
return null;
}
}
public class FacotryDemo{
public static void main(String[] args){
FactoryDemo factorydemo = new FactoryDemo();
Shape shape1 = factorydemo.getShape("circle");
shape1.draw();
Shape shape2 = factorydemo.getShape("rectangle");
shape2.draw();
Shape shape3 = factorydemo.getShape("square");
shape3.draw();
}
}