题目描述
利用接口和接口回调,实现简单工厂模式,当输入不同的字符,代表相应图形时,利用工厂类获得图形对象,再计算以该图形为底的柱体体积。
源代码
Shape 接口
package com.tomotoes.probleam.nine;
public interface Shape {
double getArea();
}
工厂类
package com.tomotoes.probleam.nine;
import com.tomotoes.probleam.nine.shapes.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.function.Supplier;
public class Factory {
private static Scanner scanner = new Scanner(System.in);
private static double input() {
return scanner.nextDouble();
}
private static Map> factory = new HashMap<>();
static {
factory.put("rect", () -> new Rect(input(), input()));
factory.put("circle", () -> new Circle(input()));
factory.put("square", () -> new Square(input()));
factory.put("trapezoid", () -> new Trapezoid(input(), input(), input()));
factory.put("triangle", () -> new Triangle(input(), input(), input()));
}
public static Shape getInstance(String type) {
return factory.getOrDefault(type, () -> null).get();
}
}
主类
package com.tomotoes.probleam.nine;
import com.tomotoes.probleam.nine.shapes.Column;
import java.util.Objects;
import java.util.Scanner;
public class App {
private static Scanner scanner = new Scanner(System.in);
public static void init() {
System.out.println("矩形类: 输入 rect -> Rect(宽,长)");
System.out.println("圆形类: 输入 circle -> Circle(半径)");
System.out.println("三角形类: 输入 triangle -> Triangle(底边,高,斜边)");
System.out.println("正方形类: 输入 square -> Square(边长)");
System.out.println("梯形类: 输入 trapezoid -> Trapezoid(顶边,底边,高)");
System.out.printf("输入其他值, 程序退出.\n\n");
}
public static void main(String[] args) {
init();
while (true) {
System.out.println("请输入类型参数: ");
final String input = scanner.next().trim().toLowerCase();
System.out.println("请输入类所需的构造参数: ");
Shape shape = Factory.getInstance(input);
if (Objects.isNull(shape)) {
System.out.println("程序退出.");
return;
}
System.out.println("请输入柱体的高: ");
final double height = scanner.nextDouble();
Column column = new Column(height, shape);
System.out.printf("柱体的体积为: " + column.getVolume() + "\n\n");
}
}
}
柱体类
package com.tomotoes.probleam.nine.shapes;
import com.tomotoes.probleam.nine.Shape;
public class Column {
double height;
Shape shape;
public Column(double height, Shape shape) {
this.height = height;
this.shape = shape;
}
public double getVolume() {
return height * shape.getArea();
}
}
矩形类
package com.tomotoes.probleam.nine.shapes;
import com.tomotoes.probleam.nine.Shape;
public class Rect implements Shape {
double width;
double length;
public Rect(double width, double length) {
this.width = width;
this.length = length;
}
@Override
public double getArea() {
return width * length;
}
}
圆形类
package com.tomotoes.probleam.nine.shapes;
import com.tomotoes.probleam.nine.Shape;
public class Circle implements Shape {
double radius;
public Circle(double radius) {
this.radius = radius;
}
@Override
public double getArea() {
return Math.pow(radius, 2) * Math.PI;
}
}
正方形类
package com.tomotoes.probleam.nine.shapes;
import com.tomotoes.probleam.nine.Shape;
public class Square implements Shape {
double length;
public Square(double length) {
this.length = length;
}
@Override
public double getArea() {
return length * length;
}
}
梯形类
package com.tomotoes.probleam.nine.shapes;
import com.tomotoes.probleam.nine.Shape;
public class Trapezoid implements Shape {
double top;
double base;
double height;
public Trapezoid(double top, double base, double height) {
this.top = top;
this.base = base;
this.height = height;
}
@Override
public double getArea() {
return (top + base) * height * 2;
}
}
三角形类
package com.tomotoes.probleam.nine.shapes;
import com.tomotoes.probleam.nine.Shape;
public class Triangle implements Shape {
double base;
double height;
double oblique;
public Triangle(double base, double height, double oblique) {
this.base = base;
this.height = height;
this.oblique = oblique;
}
@Override
public double getArea() {
return base * height / 2;
}
}