反射——确定调用的方法&来自数据库值的参数

发布于 2024-12-14 10:26:07 字数 828 浏览 1 评论 0原文

我开发了一个简单的 POS 系统,可以获取产品列表和产品列表。来自数据库的按钮。包含所有按钮的表格提供了我们绘制按钮所需的所有信息(即:大小、位置、标签、颜色以及与哪个 SKU 相关)。

在原始版本中,所有“功能”按钮都已硬编码(即:数字 1-9、支付、更改价格水平、结束班次等),但我想转向更可定制的设计,以便功能按钮也可以被存储在数据库中。

我正在考虑用基于反射的东西来做这件事。我有一个类充当接口,其中包含将出现在数据库中的所有方法,然后它将调用系统内的任何必要方法。但我不确定如何将其映射到数据库。

我非常确定每个方法都会采用 0、1 或 2 个参数,这些参数混合有 boolean、int、double 和 char。

我已经为数据库提出了一个符号,我可以解析它来确定调用该方法所需的所有信息,例如为了支付一笔销售,我会

paySale[]

在数据库中放入类似的内容,但如果客户给了我一个50 美元,我想使用“快速现金”按钮,那么方法调用将是

paySale[d 50]

或更复杂一些,例如使用支票付款

paySale[i 3, d 50]

,其中 int 是付款方式类型(在本例中为支票),而 double 是总数。

我的问题是,当我尝试使用 getMethod() 获取方法时,我无法弄清楚如何定义要查找的参数,例如 double.class 和 int.class

需要有几种不同的方法打电话,处理价格水平变化、轮班结束、向帐户/标签付款等事情,所以如果有更简单的方法来做到这一点,那么我愿意!

再次感谢大家!

I've developed a simple POS system that gets the list of products & buttons from the DB. The table holding all the buttons provides all the information we need to draw the button (i.e.: size, position, label, colour and which SKU it relates to).

In the original version, all the 'function' buttons have been hard coded (i.e.: numbers 1-9, pay, change price levels, end shift etc) but I want to move to a more customisable design, so that function buttons can also be stored in the database.

I was looking at doing this with something reflection based. I have a class to act as an interface, containing all the methods that will appear in the DB, which will then call on any of the necessary methods within the system. But I'm not sure how to map this to the DB.

I'm pretty certain that every method will take 0, 1 or 2 parameters which have a mixture of boolean, int, double and char.

I've come up with a notation for the DB that I can parse to determine all the information I need to call the method, for example to pay off a sale I would put something like

paySale[]

in the database, but if the customer gave me a $50 and I wanted to use a 'quick cash' button, then the method call would be

paySale[d 50]

or something a little more complex, like paying with a cheque

paySale[i 3, d 50]

where the int is the payment method type (in this case would be cheque) and double is the total.

My issue is that when I try to fetch the method using getMethod() I can't figure out how to define which parameters to look for, such as double.class and int.class

There will be several different methods that will need to be called, to handle things like price level changes, end of shift, paying to accounts / tabs etc so if there is an easier way to do this, then I'm open to it!

Thanks again guys!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ゞ花落谁相伴 2024-12-21 10:26:07

使用诸如

class.getMethod(int.class, double.class, String.class) 等。如果您的方法不是公共的,请改用 getDeclaredMethod()

但老实说,你的解决方案听起来很奇怪。据我了解,您实际上保存在数据库类型的脚本中,然后运行它。如果是这样,请使用现成的脚本引擎。例如,java 1.6 及更高版本具有内置 JavaScript 解释器(请参阅 ScriptEngineManager)。您可以生成 JavaScript,然后运行它们。这比自己创建非常有限的脚本解释器要容易得多。

Use something like

class.getMethod(int.class, double.class, String.class) etc. If your method is not public use getDeclaredMethod() instead.

But honestly your solution sounds strange. As far as I understand you actually save in DB kind of script and then run it. If so use ready-to-use scripting engine. For example java 1.6 and higher has built-in JavaScript interpreter (see ScriptEngineManager). You can generate javascripts and then run them. It is much easier than create very limited script interpreter yourself.

赤濁 2024-12-21 10:26:07

在这一点上我和 AlexR 是一致的。

虽然您正在做的事情绝对是可能的,但您最好在数据库中存储可执行脚本,并在应用程序的上下文中执行该脚本。

也就是说,我质疑系统是否需要如此灵活:您的系统可能只需要一定数量的功能(命令)。看起来这些命令可以正常执行,并分配给特定的键/等。通过正常的配置机制。

I'm with AlexR on this one.

While what you're doing is definitely possible, you'd be better off storing an executable script in the database, and executing that script in the context of your app.

That said, I question the need for having the system be this flexible: there are only a certainly number of functions (commands) your system is likely to need. It seems like those commands could be implemented normally, and assigned to specific keys/etc. via a normal configuration mechanism.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文