如何实现猜词游戏的MVC模式? [英] how to implement MVC pattern for word guessing game?

查看:172
本文介绍了如何实现猜词游戏的MVC模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有猜词游戏的一些工作代码。但我担心它不会限制设计规则,尤其是 MVC 模式。附件图片目前是我的 GUI 。我把物品从一个类扔到另一个类,我听说这是一种糟糕的风格。虽然我同意这一点,但我无法想出一个好的 MVC 模式方法,用于单词猜谜游戏或通常称为刽子手。
主要应用程序将包含以下内容:

I have some working code for the word guessing game. But I fear it does not confine the design rules especially the MVC pattern. The attached image is my GUI currently. I am throwing around objects from one class to another and I hear that it is a bad style. while I agree with that, I am not able to come up with good MVC pattern approach for the word guessing game or the Hangman commonly called. The main application will have some like this:

public class Application {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                runApp();
            }

        });
    }

    public static void runApp() {
        Model model = new Model();
        View view = new View(model); //not sure if this correct, some suggest it is valid and some not

        Controller controller = new Controller(view, model);
    }

}

我将如何处理此问题?
如附图所示, GUI 将是 View 类。这包括所有 JButtons,Textfield,border,labels 等。将 actionlisteners 附加到 JButtons 查看

how would I approach this? The GUI as seen in the attached picture would be the View Class. This includes all JButtons, Textfield, borders, labels etc. Attach actionlisteners to JButtons in the View class

控制器事件传递给模型。例如,如果点击一些字母按钮,它会传递该字母A被点击到 model 并且 model 将向 controller 发送指令以更新 view 或它会直接更新查看。根据我对 MVC 模式的理解,模型类必须与分开实施和测试查看控制器。我不明白我怎么能在这里实现这个目标。我有完整的代码。我需要重构以限制MVC模式。请传递你的建议。

The controller will pass the events to the model. for example, if some letter buttons are clicked, it would pass that letter "A" is clicked to model and the model will either send instructions to controller to update view or it will update view directly. from my understanding of the MVC pattern, the model class must be implemented and tested separately from view and controller. I do not understand how I can achieve this here. I have complete code available. I need to refactor to confine to MVC pattern. kindly pass on your suggestions.

推荐答案

我认为你感到困惑的一个领域是责任。什么是每个组件负责什么以及它实际上可以做什么。

I think one of the areas you are getting confused over is "responsibility". What is each component responsible for and what can it actually do.

问题不是你在程序周围传递对象,而是你传递的对象更多暴露您的应用程序的一部分,收件人没有业务知道或应该被允许操纵。

The problem isn't that you are passing Objects around you program, but more that the objects you are passing are exposing parts of your application that the recipient has no business knowing about or should be allowed to manipulate.

我的意思是,如果你要通过按钮面板到猜测面板,因为您希望能够允许猜测面板检测到单击按钮时,您已将按钮面板暴露给应用程序中没有权利的区域实际看到它。

What I mean by this is, if you were to pass the "buttons" panel to the "guess" panel, because you wanted to have the ability to allow the "guess" panel detect when a button was clicked, you've exposed the "buttons" panel to an area of your application that has no right to actually see it.

什么阻止猜测面板移除组件?没有...

What's stopping the "guess" panel from removing components? Nothing...

相反,我们应该使用接口来确定应用程序的每个部分可以和可以'这是你可以获得的信息。

Instead, we should use interfaces which determine what each part of the application can and can't do and what information is available to it.

这是你进入模型的地方。模型决定了可用的信息,访问方式以及可能的事件。触发通知相关方该模型已更改。

This is where you model comes in. The model determines what information is available, how it can be accessed and what events might be triggered to notify interested parties that the model has changed.

例如。您的按钮面板会告诉模型用户进行了另一次猜测(响应用户按下按钮)。然后,该模型将引发一个事件,该事件将通知猜测面板发生了更改。然后猜测面板会相应地更新它的状态,向模型询问它所需的信息,以便表示模型的当前状态(就其负责而言)。

For example. Your "buttons" panel would tell the model that the user has made another guess (in response to the user pressing the button). The model would then raise an event, which would notify the "guess" panel that a change has occurred. The "guess" panel then would update it's state accordingly, asking the model for the information it needed in order to represent the current state of the model (as far as it was responsible for).

你可以看看

  • Code to Interface, Access by name and Instance Data
  • Program to an interface

现在,使用MCV模式,视图必须能够看到模型,控制器必须能够看到视图和模型以及该模型并不关心。

Now, with the MCV pattern, the view must be able to see the model, the controller must be able to see the view and model and the model doesn't care.

控制器正在监听视图的更改(即用户交互),并将其传递给模型。该模型会触发有关其状态更改的通知,并且视图会根据需要自行更新来响应这些更改。

The controller is listening for changes to the view (ie user interactions), which it passes to the model. The model fires notifications about changes to it's state and the view responds to those changes by updating itself as required.

例如,使用单击按钮上的按钮面板。 按钮面板的控制器检测到此事件(可能通过 ActionListener ),它处理此操作并更新模型。

For example, the use clicks a button on the "button" panel. The "button" panel's controller detects this event (probably via an ActionListener), it process this action and updates the model.

模型更新它的内部状态并触发某种事件。

The model updates it's internal state and fires some kind of event.

猜测面板检测到模型中的这种变化(通过某种监听器)和相应地更新它的视图(根据模型更新猜测和图像)。

The "guess" panel detects this change in the model (via some kind of listener) and updates it's view accordingly (update the guess's and the image as dictated by the model).

现在,请记住,Swing不使用纯MCV模式,它是控件(即按钮)既是控制器又是视图,所以在玩这些游戏时要小心......

Now, remember, Swing doesn't use a pure MCV pattern, it's controls (ie buttons) are both the controller and the view, so just be careful when playing around with these...

我会先从 HangManModel开始 interface ,它定义了您要公开的所有属性,例如猜测,秘密字以及可能的错误猜测的数量和例如,游戏的状态(输赢)。

I would start with a HangManModel interface which defines all the properties you want to expose, such as the guesses, the "secret" word and perhaps the number of incorrect guesses made and the state of the game (win or lose) for example.

我还会定义可能注册到模型的侦听器,它描述了此模型可以生成的事件。您可以使用 PropertyChangeListener 或甚至 ChangeListener 或根据您自己的需要定义您自己的,例如.. 。

I would also define the listeners that might be registered to the model, which describes the events that this model can generate. You could use a PropertyChangeListener or even a ChangeListener or define your own, based on your own needs, for example...

public interface HangManModel {

    public void addGuess(char guess);

    public char[] getGuesses();
    public String getSecretWord();
    public int getState(); // running, win or lose

    public void addChangeListener(ChangeListener listener);
    public void removeChangeListener(ChangeListener listener);

}

现在这只是一个示例,就我个人而言,我可能会习惯于隐藏秘密词并揭露它的属性(比如它的长度)。您也可能想要为密码字提供一个setter,因此可以重置模型...

Now this is just an example, personally, I might be tempered to hide the secret word and expose properties about it (like it's length for example). You could also be tempted to provide a setter for the secret word, so the model could be reset...

这将代表您应用程序的核心,这样,你就可以构建你的视图和控制器了。

This would represent the "heart" of your application, around this, you would build your views and controllers.

这篇关于如何实现猜词游戏的MVC模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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