Java工厂模式的应用
画一个图形,有一个图形的工厂,根据你需要的图形,然后创建出一个图形
public interface Shape {
void draw();
}
public class Rectangle implements Shape {
@Override
public void draw() {
System.out.println('Inside Rectangle::draw() method.');
}
}
public class Circle implements Shape {
@Override
public void draw() {
System.out.println('Inside Circle::draw() method.');
}
}
public class ShapeFactory {
//use getShape method to get object of type shape
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;
}
}
public interface Shape {
void draw();
}
public class Square implements Shape {
@Override
public void draw() {
System.out.println('Inside Square::draw() method.');
}
}
public class Circle implements Shape {
@Override
public void draw() {
System.out.println('Inside Circle::draw() method.');
}
}
public class ShapeFactory {
//use getShape method to get object of type shape
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;
}
}
赞0
踩0