简体   繁体   English

这是OO编程的不正确做法吗?

[英]Is this an incorrect practice of OO programming?

I'm working on re-writing a paint handler for my api. 我正在为我的API重新编写绘画处理程序。 Is this incorrect practice of OO for my class structure of the PaintManager or is it fine? 对于我的PaintManager的类结构而言,OO的这种错误做法是正确的吗? I feel like it's correct but I wanted some second opinions. 我觉得这是正确的,但我想再提出一些意见。

Ignore the height and positional values in the Background class. 忽略Background类中的高度和位置值。 :p :p

在此处输入图片说明

在此处输入图片说明

在此处输入图片说明

在此处输入图片说明

It's not an incorrect practice. 这不是不正确的做法。 Moreover, whatever you've implemented here is like a Strategy Design Pattern . 此外,无论您在此处实现的是什么,都像是Strategy Design Pattern

Strategy Pattern says 策略模式说
capture the abstraction in an interface, bury implementation details in derived classes. 在接口中捕获抽象,将实现细节埋在派生类中。

策略设计模式

source 资源

Since you have a PaintManager , I assume that you will have a lot of classes implementing Paintable . 由于您具有PaintManager ,因此我假设您将有很多实现Paintable的类。 Try to have your classes as general as possible, to avoid having to write too many classes. 尝试使您的类尽可能通用,以避免不得不编写太多的类。

If you created a class Square extends Paintable for example, the difference in the code would be minimal. 例如,如果您创建了一个class Square extends Paintable ,则代码中的差异将很小。

public class Square extends Paintable {
    // the x position
    private int x;

    // the y position
    private int y;

    // the width
    private int w;

    // the height
    private int h;

    // the color
    private Color color;

    public Square(int x, int y, int w, int h, Color color) {
        this.x = x;
        this.y = y;
        this.w = w;
        this.h = h;
        this.color = color;
    }

    // draw method
    public void draw(Graphics g) {
        g.setColor(color.getCOLOR());
        g.drawRect(x, y, w, h);
        g.fillRect(x, y, w, h);
    }
}

Background can be an object of a more general class. 背景可以是更一般类的对象。

Read Difference between an object and a class for more details 阅读对象与类之间的区别以获取更多详细信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM