与Java的公共领域有什么关系? [英] What's the deal with Java's public fields?

查看:95
本文介绍了与Java的公共领域有什么关系?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在阅读两篇文章(1) < javaworld.com上关于所有类字段应如何私有的href =http://www.javaworld.com/javaworld/jw-01-2004/jw-0102-toolbox.html>(2)和getter / setter方法一样糟糕。一个对象应该对它拥有的数据起作用,而不是允许访问它。

I've been reading two articles (1)(2) on javaworld.com about how all class fields should be private and getter/setter methods are just as bad. An object should act on the data it has rather than allowing access to it.

我正在为四连接。在设计程序时,玩游戏的代理商需要访问董事会的状态(因此他们可以决定要移动的内容)。他们还需要将这一举动传递给游戏,以便将其作为合法举措进行验证。在决定移动内容时,将各个部分分组为具有起点和终点的威胁。

I'm currently working on a University assignment for Connect Four. In designing the program the Agents playing the Game need access to the Board's state (so they can decide what to move). They also need to pass this move to the Game so it can validate it as a legal move. And during deciding what to move pieces are grouped into Threats with a start and end Points.

Board,Threat和Point对象实际上没有做任何事情。它们只是用于存储可以人类可读方式访问的相关数据。

Board, Threat and Point objects don't really do anything. They are just there to store related data that can be accessed in a human readable way.

在设计之初,我将板上的点表示为两个元素的int数组但是,在创建点或引用它们的组件时会很烦人。

At the start of design I was representing Points on the board as two element int arrays, however that got annoying when creating points or referencing components of them.

所以,类:

public class Point {
    public int x;
    public int y;
    public Point(int x, int y){
        this.x = x;
        this.y = y;
    }
}

我能想到的各方面都很完美。除了它打破了我学到的每一条规则。我犯了罪吗?

Perfect in every way I can think of. Except it breaks every rule I've learned. Have I sinned?

推荐答案

公共字段将对象的表示暴露给其调用者,即如果表示必须改变,那么做来电者。

Public fields expose the representation of an object to its callers, i.e. if the representation has to change, so do the callers.

通过封装表示,您可以强制调用者与其交互,并且可以更改该表示,而无需修改调用者,前提是公共API不变。在任何非平凡的程序中,封装都是实现合理可维护性所必需的。但是,当您需要胶囊时,它们的适当粒度可能比单个类大。例如,从它运行的 Collection 的内部表示中封装 Iterator 是没有意义的。

By encapsulating the representation, you can enforce how callers interact with it, and can change that representation without having to modify the callers provided the public api is unchanged. In any non-trivial program, encapsulation is necessary to achieve reasonable maintainability. However, while you need capsules, their proper granularity may be larger than a single class. For instance, it makes little sense to encapsulate an Iterator from the internal representation of the Collection it operates on.

有了这个,让我们来看看你的例子:

With that out of the way, let's look at your example:

public class Point {
    public int x;
    public int y;
    public Point(int x, int y){
        this.x = x;
        this.y = y;
    }
}

该类的内部表示极不可能改变因此,通过将字段设为私有来隐藏表示的结构没有任何好处。但是,我会阻止调用者在构造之后修改 Point

The internal representation of that class is exceedingly unlikely to change, so hiding the structure of the representation by making the fields private has no benefit. However, I'd prevent callers from modifying a Point once it has been constructed:

public class Point {
    public final int x;
    public final int y;
    public Point(int x, int y){
        this.x = x;
        this.y = y;
    }
}

这样一个实际上希望封装其状态的类可以在没有 Point =noreferrer>泄漏其内部表示,并在其表示中使用给定的点,而不使用捕获它。这也非常适合一个点的数学概念,它没有身份或改变状态。

so that a class that actually wishes to encapsulate its state can return its Point without leaking its internal representation, and use a given Point in its representation without capturing it. This also fits nicely with the mathematical notion of a point, which has no identity or changing state.


在设计程序时,代理人在玩游戏需要访问董事会的状态(因此他们可以决定要移动什么)。他们还需要将这一举动传递给游戏,以便将其作为合法举措进行验证。在决定移动内容时,将各个部分分组为具有起点和终点的威胁。

In designing the program the Agents playing the Game need access to the Board's state (so they can decide what to move). They also need to pass this move to the Game so it can validate it as a legal move. And during deciding what to move pieces are grouped into Threats with a start and end Points.

Board,Threat和Point对象实际上没有做任何事情。他们只是存储相关数据,可以用人类可读的方式访问。

Board, Threat and Point objects don't really do anything. They are just there to store related data that can be accessed in a human readable way.

现在这听起来像是一个浪费的封装机会:不应允许代理商任意修改董事会,但仅限于合法行动。当更新状态位于类 Board 游戏的责任>?如果 Board 要验证移动本身,则没有来电者,特别是没有代理人,可能违反游戏规则:

Now this sounds like a wasted opportunity for encapsulation: The agents should really not be permitted to arbitrarily modify the board, but be restricted to legal moves. Why is it the responsibility of class Game to decide what a legal move is, when the state being updated resides in class Board? If the Board were to validate the moves itself, no caller, and in particular no agent, could violate the rules of the game:

public class Board {
    // private fields with state

    // public methods to query state

    public void perform(Move move) throws IllegalMoveException;
}

这篇关于与Java的公共领域有什么关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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