面向对象编程 - 有关设计和访问相关数据的问题
假设您在 Timer< 中有以下代码/代码>
: 它的主要目标是倒计时并在基于 Swing 的 GUI 上显示它。 计时器是游戏的一部分,用于通过首先找到解决方案的用户来决定谁是获胜者。
Action updateClockAction = new AbstractAction() {
public void actionPerformed(ActionEvent e) {
JLabel secLabel = m_GameApplet.GetJpanelStartNetGame().GetJlabelSeconds();
secLabel.setFont(new java.awt.Font("Lucida Handwriting", 1, 36));
secLabel.setForeground(Color.red);
secLabel.setText(Integer.toString(m_TimerSeconds));
if (m_TimerSeconds > 0) {
m_TimerSeconds--;
} else if (m_TimerSeconds == 0) {
m_Timer.stop();
m_GameApplet.GetJpanelStartNetGame().GetJlabelSeconds().setText("0");
m_GameApplet.GetJpanelStartNetGame().GetJbuttonFinish().setVisible(false);
//Checking whether time ended for both players and no solution was recieved
if (!m_WasGameDecisived) {
System.out.println("Tie - No one had a solution in the given time");
}
}
}
};
m_Timer = new Timer(1000, updateClockAction);
现在,我在客户端实现中有两个主要包 A.GUI - 持有所有摇摆 Jpanels 等。 B.LogicEngine
现在,在 LogicEngine 中,我有一个名为 GameManager 的类,它应该管理和存储有关游戏的信息。
我当前的实现一般是这样的: 我有一个位于 GUI 包上的 JpanelMainGame
JpanelMainGame
包含 JPanelGameBoard
,它引用(或换句话说包含)GameManger
的实例,该实例位于不同的包 - LogicEngine 上
包。
所以我的问题是:
上面的所有计时器定义代码应该放在哪里?
A.在
JpanelMainGame
中? (JpanelMainGame
应该有对它的引用)。B.在
GameManger
中作为游戏数据的一部分在您给出的每个解决方案中,我应该如何从匿名内部类中访问所有标签信息?因为我只能使用命令访问外部类的成员:
OuterClass.this.member
。有关当前设计的任何评论。
非常感谢
Consider you have the following code in a Timer
:
It's main goal is to count down the seconds and to show it on GUI, which is Swing based.
The Timer is part of the game and is used for a decision of who is the winner by taking the user who reached a solution first.
Action updateClockAction = new AbstractAction() {
public void actionPerformed(ActionEvent e) {
JLabel secLabel = m_GameApplet.GetJpanelStartNetGame().GetJlabelSeconds();
secLabel.setFont(new java.awt.Font("Lucida Handwriting", 1, 36));
secLabel.setForeground(Color.red);
secLabel.setText(Integer.toString(m_TimerSeconds));
if (m_TimerSeconds > 0) {
m_TimerSeconds--;
} else if (m_TimerSeconds == 0) {
m_Timer.stop();
m_GameApplet.GetJpanelStartNetGame().GetJlabelSeconds().setText("0");
m_GameApplet.GetJpanelStartNetGame().GetJbuttonFinish().setVisible(false);
//Checking whether time ended for both players and no solution was recieved
if (!m_WasGameDecisived) {
System.out.println("Tie - No one had a solution in the given time");
}
}
}
};
m_Timer = new Timer(1000, updateClockAction);
Now, I have two main packages in the client implementation
A.GUI - hold all the swing Jpanels etc.
B.LogicEngine
Now in LogicEngine I have a class that is called GameManager- that should manage and store information about the game.
My Current implementation in general is like this:
I have a JpanelMainGame
which is on the GUI PackageJpanelMainGame
contains JPanelGameBoard
which has reference(or in other word contains) an instance of GameManger
, which is on different package - The LogicEngine
package.
So my questions are these:
Where does all of the timer definition code above should be placed?
A. In the
JpanelMainGame
? (JpanelMainGame
should have a reference to it).B. In
GameManger
as a part of a data of the gameIn each solution you give, How should I access all the label info from within the anonymous inner class? As I can only access member from the outer class with command:
OuterClass.this.member
.Any comments about current design.
Thank you very much
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我重复我在另一个问题中给出的答案:
I repeat the answer I gave on another question:
好吧,不是用问题来回答问题,但是……
如果计时器是通用的,那么我会说它属于 LogicEngine 包树中的某个位置。但是,如果 Timer 只能在 GUI 元素中使用,那么它属于 GUI 包树。
作为一般经验法则(不要太用力地敲击敏捷鼓),您应该只编写现在有意义的代码,但不要害怕以后更改它。
示例:您正在代码中创建 AbstractAction() 类型的匿名内部类。如果简单地使用 updateClockAction 变量,这就很好(尽管我回避匿名类)。但是,一旦您的编码导致您需要跨越 updateClockAction 的边界(通过 OuterClass.this.member),那么我断言需要进行一些重构:提取这个内部类并使其成为一个完全限定的类。这样,您可以对 updateClockAction 对象的内部状态进行适当的控制(使用 cstr、getter 等)
编辑:添加了请求的示例
Well not to answer questions with questions, but...
If the Timer is general use, then I would say it belongs in the LogicEngine package tree somewhere. However, if Timer can only be used in a GUI element, then it belongs in the GUI package tree.
As a general rule of thumb (not to bang on the Agile drum too hard), you should only code what makes sense now but not be afraid to change it later.
Example: You are making an anonymous inner class of the type AbstractAction() in your code. This is just fine (although I shy away from anon classes) if the updateClockAction variable is used simply. But once your coding leads you to the need to cross the boundries of updateClockAction (via OuterClass.this.member) then I assert its time for a bit of refactoring: extract this inner class and make it a fully qualified class. This way, you can have the proper amount of control over the updateClockAction object's internal state (using the cstr, getters, etc)
EDIT: Added requested example