首页 > 解决方案 > 面向对象设计 - 形状

问题描述

所以,经过多年的 OOP,我从我的一门大学课程中得到了一个非常简单的家庭作业,以实现一个简单的面向对象结构。

要求的设计:

以及以下功能:颜色变化,移动,面积,圆周,在里面,复制。

不需要有效性测试(不是在类中,也不是来自用户输入)。

我的设计

在此处输入图像描述

总体而言,这是一种非常简单的方法,shape_class / non-circular 是抽象的,并且 rectangle/square 合并为一个类,因为它们包含完全相同的参数并且不需要有效性测试(没有理由将它们分成两个)。

Shape 类- 实现一个静态 id(唯一 id)和一个处理颜色名称的 init 函数。

public abstract class shape_class {

    static int STATIC_ID;
    int id;
    String color_name;

    public shape_class(String color_name_input) {
        this.id = STATIC_ID;
        shape_class.STATIC_ID+=1;
        if (Arrays.asList(toycad_globals.ALLOWED_COLORS).contains(color_name_input))
        {
            this.color_name = color_name_input;
        }
    }

    public void change_color(String color_name_input) {
        if (Arrays.asList(toycad_globals.ALLOWED_COLORS).contains(color_name_input)) {
            this.color_name = color_name_input;
        }
    }

    public abstract shape_class return_copy();
    public abstract void move(double x, double y);
    public abstract double area();
    public abstract double circumference();
    public abstract boolean is_inside(double x, double y);
}

** 非圆形** - 接收点数组(定义对象)并实现几乎所有必需的功能。

public abstract class non_circullar extends shape_class {
    List<line> line_list = new ArrayList<line>();
    List<point> point_list = new ArrayList<point>();

    non_circullar(String color_name, point...input_point_list) {
        super(color_name);
        this.point_list = Arrays.asList(input_point_list);
        for (int current_index =0; current_index< (input_point_list.length); current_index++) {
            point current_first_point = input_point_list[current_index];
            point current_second_point = input_point_list[(current_index+1)%input_point_list.length];
            this.line_list.add(new line(current_first_point, current_second_point));
        }
    }

    public point[] get_point_list_copy() {
        int index = 0;
        point [] new_array = new point[this.point_list.size()];
        for (point current_point:this.point_list) {
            new_array[index] = current_point.return_copy();
            index+=1;
        }
        return new_array;
    }

    public double circumference() {
        double sum = 0;
        for (line current_line :this.line_list) {
            sum += current_line.get_length();
        }
        return sum;
    }

    public void move(double x, double y) {
        for (point current_point :this.point_list) {
            current_point.move(x, y);
        }
    }

    public boolean is_inside(double x, double y) {
        int i;
        int j;
        boolean result = false;
        for (i = 0, j = this.point_list.size() - 1; i < this.point_list.size(); j = i++) {
            if ((this.point_list.get(i).y > y) != (this.point_list.get(j).y > y) &&
                (x < (this.point_list.get(j).x - this.point_list.get(i).x) * (y - this.point_list.get(i).y) / 
                        (this.point_list.get(j).y-this.point_list.get(i).y) + this.point_list.get(i).x)) 
           {
              result = !result;
           }
        }
        return result;
    }

    int get_top_left_line_index() {
        int top_left_line_index = 0;
        int index = 0;
        point best_point = this.line_list.get(0).get_average_point();
        point current_point;
        for (line current_line :this.line_list) {
            current_point = current_line.get_average_point();

            if (current_point.x < best_point.x) {
                best_point = current_point;
                top_left_line_index = index;
            } else if (current_point.x == best_point.x && current_point.y > best_point.y) {
                best_point = current_point;
                top_left_line_index = index;
            }
            index +=1;
        }
        return top_left_line_index;
    }
}

问题:

对于此作业,设计问题减少了 40 分:

1)圆是一个椭圆,因此需要从它继承(即使它们不共享参数)。

2)矩形/正方形是两个不同的实体,即使在这个实现中它们是完全相同的(没有有效性测试)。

我很乐意从社区获得一些关于这个设计的意见,设计问题是否“合法”,还有什么可以做得更好?

编辑1:

椭圆表示为:两点和d(对于椭圆上的点,它与两点之间的距离必须等于d)。

圆表示为:圆心和半径。

我发现很难理解他们如何共享公共参数。

标签: javaoopinheritance

解决方案


我建议你遵循这个方案:

在此处输入图像描述

您需要先按边数对形状进行分类,然后再按共同特征对形状进行分类。那么你必须认识到以下事实:

  • circle只是一种特殊的类型ellipse
  • square只是一种特殊的类型rectangle
  • 两者rectangle都有parallelogram4 条边
  • 与 不同parallelogramrectangle所有角度均为 90°

这是根据您的需要的简化方案:

椭圆、圆形、正方形、矩形、三角形、平行四边形

编辑:请注意,也存在以下层次结构。两者都rectangle具有parallelogram相同长度的对边。最后,这取决于首选的解释以及更适合您情况的解释(感谢@Federico klez Culloca):

Quadrilateral <- Parallelogram <- Rectangle <- Square

使其可扩展:如果包含更复杂的基本几何形状,我可能会放在polygon下面shape,然后首先通过凸性和非凸性区分后代。


推荐阅读