需要帮助理解PEP 227和Python语言参考中的以下句子
如果在封闭范围中引用变量,则删除名称是错误的.编译器将为'del name'引发一个SyntaxError.
缺少示例导致我无法在编译时重现错误,因此非常需要使用示例进行解释.
我有一个webapp,它使用GIN在入口点注入依赖项.
private InjectorService injector = GWT.create(InjectorService.class);
Run Code Online (Sandbox Code Playgroud)
@GinModules({PlaceContollerInject.class, RootViewInject.class})
public interface InjectorService extends Ginjector {
RootView getRootView();
PlaceController getPlaceConroller();
}
Run Code Online (Sandbox Code Playgroud)
public class RootViewInject extends AbstractGinModule {
@Override
protected void configure() {
bind(RootView.class).to(RootViewImpl.class);
}
}
Run Code Online (Sandbox Code Playgroud)
我需要一个使用不同RootView实现的移动版本.依赖关系在以下模块中描述
public class RootViewMobileInject extends AbstractGinModule {
@Override
protected void configure() {
bind(RootView.class).to(RootViewMobileImpl.class);
}
}
Run Code Online (Sandbox Code Playgroud)
问题是如何有条件地选择所需的依赖性是否需要移动版或默认版.我见过GWT-GIN多重实现,但还没有找到解决方案,因为Provider打破了依赖关系链,而Factory Pattern破坏了可测试性.在这里的"Big Modular Java with Guice"视频中(12分钟) Guice的带模块的注射器被用作工厂的替代品.所以我的问题是我应该为我的应用程序的移动和默认版本(如MobileFactory和DefaultFactory)创建不同的Ginjector,否则这将是不好的做法,我应该配置一个Ginjector实例,其中包含所有需要的版本.例如,使用这样的注释绑定.
public class RootViewMobileInject extends AbstractGinModule {
@Override
protected void configure() {
bind(RootView.class).annotatedWith(Mobile.class).to(RootViewMobileImpl.class);
}
}
Run Code Online (Sandbox Code Playgroud)
并在GWT入口点使用@Mobile注释绑定
@Inject
private void setMobileRootView(@Mobile RootView rw) { …Run Code Online (Sandbox Code Playgroud) 如果我们使用工厂方法,我们将不得不返回创建的实现作为已实现接口的类型。
public class Factory {
public Product getProduct() {
return new ProductA();
}
}
Run Code Online (Sandbox Code Playgroud)
public interface Product {
}
Run Code Online (Sandbox Code Playgroud)
class ProductA implements Product {
}
Run Code Online (Sandbox Code Playgroud)
为了避免客户将返回的 Product 强制转换为 Product{A, B, C... etc.} 的具体实现,我们必须:
com.example.client和com.example.factory) package com.example.client;
...
public class Client {
public static void main(String[] args) {
Product i = new Factory().getProduct();
ProductA a = (ProductA) i; // the type of ProductA isn't visible.
}
}
Run Code Online (Sandbox Code Playgroud)
例如我们需要使用隐藏方法的同一个工厂
public class Factory {
public Product …Run Code Online (Sandbox Code Playgroud)