手头的任务是创建我的 Java Web 应用程序的一部分,这将使我能够以组合方式轻松执行小块代码。手头的任务是允许用户以任何顺序编写“ Action ”。我遇到的困难是将参数传递给我的操作。
一切都从 Action 界面开始:
public interface Action {
void resolve(Context context);
}
当 Action 被解析时,它的代码被执行。代码可以是任何东西:调用 Java 中的方法,执行一些 Javascript...
在这里,“上下文”对我来说是个问题。每个 Action 都在特定上下文中执行。这个想法是创建 Action 的用户可以指定从概念中检索哪个对象,例如正在解析当前 Action 的用户,或在 Action 的特定界面中指定的其他对象。
例如,让我们看一下这个 Action :
public final class ActionScript implements Action {
private final Parameters parameters;
private final String methodName;
private final ScriptsLibrary library;
public ActionScript(ScriptsLibrary library, String methodName, Parameters parameters) {
this.parameters = parameters;
this.library = library;
this.methodName = methodName;
}
@Override
public void resolve(Context context) {
try {
((Invocable) library.getEngine()).invokeFunction(methodName, context);
} catch (ScriptException | NoSuchMethodException ex) {
throw new RuntimeException(ex);
}
}
}
这是一个使用 Nashorn 在 Javascript 中调用 Action 的简单包装器。参数可以是任何东西:数据库中具有特定 id 的对象,用户配置的 int/String/boolean 值...
一个 Action 的代码可以是这样的:
public class DummyAction implements Action {
@Override
public void resolve(Context context) {
User userExecutingTheAction = context.get("ExecutingUser");
System.out.println("User " + userExecutingTheAction);
}
}
所以 Action 可以检索运行时参数(例如:执行 Action 的用户...)和静态参数(在加载 Action 时创建 - 例如从配置文件),以及所有来自概念的参数。此外,用户可以在运行时注入(inject)的参数中指定对对象的引用。
Action 也可以嵌套/修饰以实现完整的组合性。例如:
public class DummyWrapperAction implements Action {
private final Action wrappedAction;
public DummyWrapperAction(Action wrappedAction) {
this.wrappedAction = wrappedAction;
}
@Override
public void resolve(Context context) {
System.out.println("Before");
wrappedAction.resolve(context);
System.out.println("After");
}
}
这样可以轻松创建操作:
// executes specific action 1 or specific action 2 based on a condition
Action myAction = new LoggingAction(new ConditionalAction(new Condition(3), new SpecificAction1(), new SpecificAction2()));
知道了这一切,设计上下文类最干净的是什么?它应该分成几个元素吗?难点在于在运行时将所有需要的东西注入(inject)到类中,并确保不与潜在的包装操作发生冲突。
Context 基本实现负责:
我觉得它做得太多了。设计受到拥有很多责任的概念类的影响,它们应该是碎片化的(现在应用程序的每个部分都与概念相关联)。但是怎么办? 这是我在干净编码中尝试实现的目标:
以真正的面向对象和面向方法的方式,如何解决这个特定的设计问题?
编辑:删除接口(interface)方法中的公共(public)声明符
编辑 2:我有很多有趣的解决方案,但对我来说更有意义的是每个 Action 都使用特定类型的上下文进行参数化的解决方案。 我这样重组了事情:
最佳答案
我已将 Action 接口(interface)的 Context 设为通用:
public interface Action<C extends Context> {
void resolve(C context); // no need to use 'public' modifier here
// interface methods are always public
}
然后,Context 可以是标记接口(interface)、强制执行契约的接口(interface)或具有默认方法实现的抽象类:
public interface Context {
User get(String user);
// other default methods here
}
然后,你可以这样做:
public class LogUserContext implements Context {
@Override
public User get(String user) {
// materialize user here
}
}
让用户登录的Action可以是:
public class LogUserAction implements Action<LogUserContext> {
@Override
public void resolve(LogUserContext context) {
User user = context.get("theUser");
// log the user in
}
}
对于包装的Action,我会使用WrapperContext 上下文:
public interface WrapperContext extends Context {
Context getWrappedContext();
}
实现:
public class DummyWrappedContext implements WrapperContext {
private final Context wrappedContext;
public DummyWrapperContext(Context wrappedContext) {
this.wrappedContext = wrappedContext;
}
@Override
public Context getWrappedContext() {
return this.wrappedContext;
}
// TODO other methods from Context, etc.
}
现在你的 DummyWrapperAction 可以如下所示:
public class DummyWrapperAction implements Action<WrapperContext> {
private final Action wrappedAction;
public DummyWrapperAction(Action wrappedAction) {
this.wrappedAction = wrappedAction;
}
@Override
public void resolve(WrapperContext context) {
System.out.println("Before");
Context wrappedContext = context.getWrappedContext();
wrappedAction.resolve(wrappedContext);
System.out.println("After");
}
}
这个想法也是包装上下文。您可以通过允许链接或修饰上下文以及使用负责所有上下文通用任务的抽象类来增强此设计。
关于java - 如何在保持干净的编程实践的同时设计一个通用的 Action 类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32948198/
很好奇,就使用rubyonrails自动化单元测试而言,你们正在做什么?您是否创建了一个脚本来在cron中运行rake作业并将结果邮寄给您?git中的预提交Hook?只是手动调用?我完全理解测试,但想知道在错误发生之前捕获错误的最佳实践是什么。让我们理所当然地认为测试本身是完美无缺的,并且可以正常工作。下一步是什么以确保他们在正确的时间将可能有害的结果传达给您? 最佳答案 不确定您到底想听什么,但是有几个级别的自动代码库控制:在处理某项功能时,您可以使用类似autotest的内容获得关于哪些有效,哪些无效的即时反馈。要确保您的提
出于纯粹的兴趣,我很好奇如何按顺序创建PI,而不是在过程结果之后生成数字,而是让数字在过程本身生成时显示。如果是这种情况,那么数字可以自行产生,我可以对以前看到的数字实现垃圾收集,从而创建一个无限系列。结果只是在Pi系列之后每秒生成一个数字。这是我通过互联网筛选的结果:这是流行的计算机友好算法,类机器算法:defarccot(x,unity)xpow=unity/xn=1sign=1sum=0loopdoterm=xpow/nbreakifterm==0sum+=sign*(xpow/n)xpow/=x*xn+=2sign=-signendsumenddefcalc_pi(digits
如何在buildr项目中使用Ruby?我在很多不同的项目中使用过Ruby、JRuby、Java和Clojure。我目前正在使用我的标准Ruby开发一个模拟应用程序,我想尝试使用Clojure后端(我确实喜欢功能代码)以及JRubygui和测试套件。我还可以看到在未来的不同项目中使用Scala作为后端。我想我要为我的项目尝试一下buildr(http://buildr.apache.org/),但我注意到buildr似乎没有设置为在项目中使用JRuby代码本身!这看起来有点傻,因为该工具旨在统一通用的JVM语言并且是在ruby中构建的。除了将输出的jar包含在一个独特的、仅限ruby
我正在使用的第三方API的文档状态:"[O]urAPIonlyacceptspaddedBase64encodedstrings."什么是“填充的Base64编码字符串”以及如何在Ruby中生成它们。下面的代码是我第一次尝试创建转换为Base64的JSON格式数据。xa=Base64.encode64(a.to_json) 最佳答案 他们说的padding其实就是Base64本身的一部分。它是末尾的“=”和“==”。Base64将3个字节的数据包编码为4个编码字符。所以如果你的输入数据有长度n和n%3=1=>"=="末尾用于填充n%
使用带有Rails插件的vim,您可以创建一个迁移文件,然后一次性打开该文件吗?textmate也可以这样吗? 最佳答案 你可以使用rails.vim然后做类似的事情::Rgeneratemigratonadd_foo_to_bar插件将打开迁移生成的文件,这正是您想要的。我不能代表textmate。 关于ruby-使用VimRails,您可以创建一个新的迁移文件并一次性打开它吗?,我们在StackOverflow上找到一个类似的问题: https://sta
我需要从一个View访问多个模型。以前,我的links_controller仅用于提供以不同方式排序的链接资源。现在我想包括一个部分(我假设)显示按分数排序的顶级用户(@users=User.all.sort_by(&:score))我知道我可以将此代码插入每个链接操作并从View访问它,但这似乎不是“ruby方式”,我将需要在不久的将来访问更多模型。这可能会变得很脏,是否有针对这种情况的任何技术?注意事项:我认为我的应用程序正朝着单一格式和动态页面内容的方向发展,本质上是一个典型的网络应用程序。我知道before_filter但考虑到我希望应用程序进入的方向,这似乎很麻烦。最终从任何
我想要做的是有2个不同的Controller,client和test_client。客户端Controller已经构建,我想创建一个test_clientController,我可以使用它来玩弄客户端的UI并根据需要进行调整。我主要是想绕过我在客户端中内置的验证及其对加载数据的管理Controller的依赖。所以我希望test_clientController加载示例数据集,然后呈现客户端Controller的索引View,以便我可以调整客户端UI。就是这样。我在test_clients索引方法中试过这个:classTestClientdefindexrender:template=>
exe应该在我打开页面时运行。异步进程需要运行。有什么方法可以在ruby中使用两个参数异步运行exe吗?我已经尝试过ruby命令-system()、exec()但它正在等待过程完成。我需要用参数启动exe,无需等待进程完成是否有任何rubygems会支持我的问题? 最佳答案 您可以使用Process.spawn和Process.wait2:pid=Process.spawn'your.exe','--option'#Later...pid,status=Process.wait2pid您的程序将作为解释器的子进程执行。除
鉴于我有以下迁移:Sequel.migrationdoupdoalter_table:usersdoadd_column:is_admin,:default=>falseend#SequelrunsaDESCRIBEtablestatement,whenthemodelisloaded.#Atthispoint,itdoesnotknowthatusershaveais_adminflag.#Soitfails.@user=User.find(:email=>"admin@fancy-startup.example")@user.is_admin=true@user.save!ende
我知道您通常应该在Rails中使用新建/创建和编辑/更新之间的链接,但我有一个情况需要其他东西。无论如何我可以实现同样的连接吗?我有一个模型表单,我希望它发布数据(类似于新View如何发布到创建操作)。这是我的表格prohibitedthisjobfrombeingsaved: 最佳答案 使用:url选项。=form_for@job,:url=>company_path,:html=>{:method=>:post/:put} 关于ruby-on-rails-rails:Howtomak