我有一个使用java的固体原理练习,特别是关于开/闭原理,练习要求我制作一个程序来计算不同几何形状的总面积,我有两种方法来制作这个程序,我想问哪一个更好地实现了开/闭原则 第一种方法:
abstract class Shape {
public abstract double CalcArea();
}
class Circle extends Shape {
private double radius;
public Circle(double radius) {
this.radius = radius;
}
public double getRadius() {
return radius;
}
@Override
public double CalcArea(){
return Math.PI * Math.pow(radius, 2);
}
}
class Rectangle extends Shape {
private double width;
private double height;
public Rectangle(double width, double height) {
this.width = width;
this.height = height;
}
public double getWidth() {
return width;
}
public double getHeight() {
return height;
}
@Override
public double CalcArea(){
return width*height;
}
}
public class ShapeCalculator {
public double calculateTotalArea(List<Shape> shapes) {
double totalArea = 0;
for (Shape shape : shapes) {
if (shape instanceof Circle) {
Circle circle = (Circle) shape;
totalArea += circle.CalcArea();
} else if (shape instanceof Rectangle) {
Rectangle rectangle = (Rectangle) shape;
totalArea += rectangle.CalcArea();
}
// Add more conditions for other shapes (e.g., Triangle)
}
return totalArea;
}
}
public static void main (String[] args) {
Rectangle abcd = new Rectangle(5.5 , 8);
Circle circle1 = new Circle(4);
List<Shape> geoshape = new ArrayList();
geoshape.add(abcd);
geoshape.add(circle1);
ShapeCalculator calculate = new ShapeCalculator();
calculate.calculateTotalArea(geoshape);
}
}
第二种方式:
public interface Shapes {
public abstract double CalcArea();
}
public class Rectangle implements Shapes {
public double length , width;
public Rectangle(double width , double length){
this.length = length;
this.width = width;
}
@Override
public double CalcArea(){
return width*length;
}
}
public class Circle implements Shapes {
double radius;
public Circle(double radius){
this.radius = radius;
}
@Override
public double CalcArea(){
return radius*radius*3.14;
}
}
public class CalculateTotalArea {
public double CalculateTotalArea(ArrayList<Double> Areas){
double totalArea = 0;
for (Double area : Areas){
totalArea += area;
}
return totalArea;
}
}
public static void main(String[] args) {
Rectangle rectangle1 = new Rectangle(8 , 2);
Circle circle1 = new Circle(7);
ArrayList<Double> AreaList = new ArrayList();
AreaList.add(rectangle1.CalcArea());
AreaList.add(circle1.CalcArea());
CalculateTotalArea result = new CalculateTotalArea();
System.out.println("The area of the Circle : "+circle1.CalcArea());
System.out.println("The area of the rectangle : "+rectangle1.CalcArea());
System.out.println("The Total Area is : "+result.CalculateTotalArea(AreaList));
}
}
我想知道他们中的哪一个更好地实现了开放/封闭原则是否有更好的方法来实现
多态性的关键方面是您可以(稍微简化)通过名称调用方法,并且每个具有此方法的类都会以自己的方式做出反应。
通过使用
Shape
方法引入抽象父类 CalcArea()
并在 Shape
子类 Circle
和 Rectangle
中实现不同的实现,您已经完成了一大半。
这允许您简化
第一种方式中的
ShapeCalculator
类,因为根本不需要执行任何条件:
public class ShapeCalculator {
public double calculateTotalArea(List<Shape> shapes) {
double totalArea = 0;
for (Shape shape : shapes) {
totalArea += shape.CalcArea();
}
return totalArea;
}
}
通过在
CalcArea()
类中声明 Shape
方法,编译器就知道 Shape
的任何实例都会有这个方法,你可以直接调用它。如果形状是 Circle
,则 Java 运行时将分派到 Circle
的实现;如果是 Rectangle
,则 Java 运行时将分派到 Rectangle
的实现。
这样您的代码就可以开放以引入新的形状类,而无需修改任何使用形状的代码。