我有一个java程序,它允许在单击按钮时制作新的绘图,假设每个按钮都是一个带有数字的圆圈。
我想要做的是,当我创建 3 个圆圈时,我创建的圆圈内的数字是 1-3,但要做到这一点,我需要能够跟踪用户已经创建的圆圈对象的数量。
我曾考虑过使用 InstanceOf,但这似乎是一种粗略的实现方式,
任何想法都值得赞赏。
谢谢
克里斯
I have a java program that allows new drawings to be made when a button is clicked , say each button is a circle with a number in it.
What i want to be able to do is that when I create 3 circles the number inside the circles I create is 1-3 but to do this I need to be able to track the amount of circle objects the user has already created.
I have thought about using InstanceOf but this seems like a Crude way of implementing it
Any Ideas Appreciated.
Thanks
Chris
发布评论
评论(6)
如果我理解正确的话,这更多的是一个设计问题,而不是一个实现问题。设计对于各种应用程序都非常重要,因为它们可以帮助您以更好的方式组织代码。通常,在编写第一行代码之前进行几个小时的思考将帮助您在以后节省更多时间。我希望我的回答能够解答您的问题。
尝试将其视为一个 MVC 应用程序,其中您的组件是视图层,模型将是一个存储实例数量(或对它们的引用)的 bean,而控制器将是由来自“事件”的代码触发的代码。视图层和模型层。
正如上面的答案中提到的,您将需要一个中心点来访问您的数据,因此您需要确保 UI 状态只有一个 bean 表示。这可以通过使用您建议的单例模式、使用静态字段或使用服务提供者外观来访问对象来实现,其中任何一个都可以完成这项工作。当然,您需要选择最适合您的项目的方法,过度设计也是一件坏事;)
您可能想要学习 PureMVC 框架,它是一个相当简单且轻量级的。我建议至少完成教程并阅读文档,以便您能够理解这个想法。您不需要使用它,但它对于教育目的很有用。
If I understood you correctly this is more of a design question rather than an implementation problem. Design is pretty important for all kinds of applications cause they help you to organise your code in a better way. Usually few hours of thinking before you write the first line of code will help you to save much more time later. I hope that my answer will answer your question.
Try thinking about it as a MVC application, where your components are the view layer, model would be a bean that stores the number of instances (or the references to them) and the controller would be a code triggered by the "events" from the view and model layers.
As mentioned in the answers above you will need a central point to access your data so you will need to make sure that there is only one bean representation of the UI state. This could be achieved either using singleton pattern as you've suggested, using static fields or by using a service provider facade to access the object, either of those will do the job. Of course you need to choose the method that suits your project best, over design is a bad thing as well ;)
You may want to do the tutorial for the PureMVC framework, it's a quite easy and lightweight. I would recommend at least doing the tutorial and going through the documentation so that you can understand the idea. You don't need to use it but it'll be good for the educational purposes.
我不知道你的系统是怎么设计的。但我想要的是一个中心位置,我的所有形状都存储在其中并且可以添加/删除。然后,该存储还可以负责计算它创建/删除的对象以及它们的命名方式。
我认为如果您不打算扩展程序/没有太大的要求,静态变量是一个简单的解决方案。
I don't know how you designed your system. But what I would have is a central place where all my shapes are stored and can be added/removed. This store could then also be responsible for counting which objects it has created/removed and the way they are named.
I think the static variable is an easy solution if you don't plan to extend the program/don't have big requirements.
另一种方法是将 Circle 对象保存在 ArrayList 中,这样您就可以通过 .size() 方法检查有多少个圆。另一个优点是您可以轻松更改/删除特定圈子。
编辑:
额外的好处是,您想要在圆圈中表示的数字是圆圈的索引 + 1。
Another way to do it, is to keep you Circle objects in an ArrayList, that way you can check how many circles there are by means of the .size() method. Another advantage is that you can easily alter/delete specific circles.
EDIT:
Added bonus, the numbers you want represented in the circles are the indices of the circle + 1.
如果我正确理解你的问题,你所需要的只是一个全局(例如静态)变量,每次创建 Circle 对象时(例如从构造函数创建),该变量都会递增。
Provided I am understanding your question correctly, all you need is a global (e.g. static) variable that gets incremented every time a Circle object gets created, e.g. from the constructor.
只需使用静态
AtomicInteger
即可跟踪创建的 Circles 的实例计数。如果您喜欢冒险,您还可以使用 基于软引用的静态缓存。Just use a static
AtomicInteger
which would keep track of your instance count for the Circles created. If you are feeling rather adventurous, you can also keep track of individual instances based on their ID by using a soft reference based static cache.如果圆圈的数量只能增加(圆圈无法删除),那么您需要使用 静态变量 用于跟踪该类已实例化的次数。
即:
或者,如果您要添加和删除圆圈,您可能希望将它们存储在列表中,并在构造函数中传入数字。
IE:
If the number of circles can only increase (circles can't be removed) then you want to use a static variable in the Circle class to keep track of the number of times that the class has been instantiated.
i.e.:
Alternatively, if you are going to be adding and removing circles, you probably want to store them in a list, and pass in the number in the constructor.
i.e.: