封装和吸气剂 [英] Encapsulation and Getters

查看:104
本文介绍了封装和吸气剂的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读文章关于为什么 getter setter 是邪恶的。文章并没有说永远使用它们,但是,它告诉你以某种方式思考限制这些方法的使用,或引用文章:

I was reading this article on why getter and setters are evil. The article doesn't say not to use them ever, but, it's telling you to think in a way that limits the use of those methods, or to quote the article:


不要求提供完成工作所需的信息;询问具有为您完成工作的信息的对象

Don't ask for the information you need to do the work; ask the object that has the information to do the work for you.

当您需要显示数据时会发生什么一个GUI,但没有getter方法?本文简要介绍了这一点,但并未完全涵盖。它提到将JComponent传递给类,但是如果你进行GUI更改,可能会导致很多工作需要修复。

what happens when you need to display data in a GUI, but don't have getter methods? The article covers this briefly, but not fully. It mentions passing a JComponent to the class, but if you're GUI changes, it could lead to a lot of work to fix.

例如,您有一个Book类(限制此示例以保持其可读性)。

Take for example, you have a Book class (making this example limited to keep it readable).

public final class Book {

    private String title;
    //Authors is class with the attributes authorFirstname, authorLastname
    private List<Author> listofAuthors;


    public Book(String title, List<Author> listofAuthors)
    {
        //initialization
    }

    //other methods that do work
}

如果我的GUI有 JTextField 显示书名和 JTable 以显示作者列表,我如何编写我的方法来执行为我工作并显示结果?这是否需要 getter

If I have a GUI that has a JTextField to display the book title and a JTable to display the list of authors, how would I write my method to "do the work" for me and display the result? Is this one of those times where a getter is necessary?

推荐答案

Allen Holub的文章(你提到的那篇文章)是完全正确的,你不应该要求数据,至少在你进行面向对象时。不,显示东西不是打开对象的有效借口。

Allen Holub's article (the one you mentioned) is completely right, you shouldn't ask for data, at least when you're doing Object-Orientation. And no, displaying things is not a valid excuse to open up an object.

如果你有一个 Book ,只需要 Book 来展示自己!无论是否使用 JTextField JTable 等等都无关紧要。根据您的要求,您可以这样做:

If you have a Book, just ask for the Book to display itself! It shouldn't matter whether that uses a JTextField or JTable or whatever. Depending on your requirements of course, you could do:

public final class Book {
    ...
    JComponent display() {
        ...
    }
}

面向对象的观点当然是你试图本地化变化(尽可能地限制到一个类)。唯一的方法是将依赖于相同内容的功能本地化到(最好)同一个类中。也称为增加凝聚力。

The point of Object-Orientation is of course, that you are trying to localize changes (restrict to one class as much as possible). The only way to do that is to localize the functionality that depends on the same things into (preferably) the same class. Also called increasing "cohesion".

所以现在,如果 Book 内部变化,所有的东西,包括如何显示 Book 本身在 Book 中,因此无需搜寻使用的代码预订

So now, if the Book internals change, all of the things, including how the Book is displayed is in the Book itself, so there is no need to "hunt" for code that uses the Book.

现在,对于这不是干净的答案,因为你正在混合演示代码与业务逻辑。值得注意的是,没有将表示与业务逻辑混合在一起的整个想法来自早期,当时我们仍然认为表示可能远离业务对象,其中业务对象可能是多个应用程序用于不同的事情。 IE浏览器。多层设计。 YAGNI 。大多数情况下,没有真正的理由在单个应用程序中具有人为的技术边界。如果 Book 知道它是GUI应用程序的一部分,并且有实际的好处(可维护性),那就没有坏处。

Now, for the answer that this is not "clean", because you are mixing presentation code with "business logic". It may be interesting to note, that the whole idea of not mixing presentation with "business logic" comes from earlier times, when we still thought that presentation might be "remote" to the "business objects", where "business objects" might be used by multiple applications for different things. Ie. multi-tier designs. YAGNI. Most of the time there is no real reason to have artificial technical boundaries inside a single application. There is no harm done if the Book knows it's part of a GUI application, and there are real benefits (maintainability) to have.

编辑:这是`display()方法的详细外观,显示标题和作者(Swing的伪代码):

this is how the `display() method could look like in detail, with displaying the title and authors (pseudocode for Swing):

public final class Book {
    private final String title;
    private final List<Author> authors;
    ...
    public JComponent display() {
        JPanel bookPanel = new JPanel();
        bookPanel.add(new JLabel(title));
        JList authorsList = new JList(); // Or similar
        for (Author author: authors) {
            authorsList.add(author.display());
        }
        bookPanel.add(authorsList);
        return bookPanel;
    }
}

然后你可以简单地将()该组件添加到要显示该书的任何摇摆容器中。

And then you can simply add() that component to whatever swing container you want to display the book in.

这篇关于封装和吸气剂的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆