use*_*267 6 java design-patterns scala
我正在将我的系统从java迁移到Scala.我在我的java代码中使用了注册表模式来从字符串中获取实现.scala有什么类似的事情吗?我是scala的新手,有人可以指点我正确的参考吗?
我的java代码:
public class ItemRegistry {
private final Map<String, ItemFactory> factoryRegistry;
public ItemRegistry() {
this.factoryRegistry = new HashMap<>();
}
public ItemRegistry(List<ItemFactory> factories) {
factoryRegistry = new HashMap<>();
for (ItemFactory factory : factories) {
registerFactory(factory);
}
}
public void registerFactory(ItemFactory factory) {
Set<String> aliases = factory.getRegisteredItems();
for (String alias : aliases) {
factoryRegistry.put(alias, factory);
}
}
public Item newInstance(String itemName) throws ItemException {
ItemFactory factory = factoryRegistry.get(itemName);
if (factory == null) {
throw new ItemException("Unable to find factory containing alias " + itemName);
}
return factory.getItem(itemName);
}
public Set<String> getRegisteredAliases() {
return factoryRegistry.keySet();
}
}
Run Code Online (Sandbox Code Playgroud)
我的项目界面:
public interface Item {
void apply(Order Order) throws ItemException;
String getItemName();
}
Run Code Online (Sandbox Code Playgroud)
我将字符串映射为:
public interface ItemFactory {
Item getItem(String itemName) throws ItemException;
Set<String> getRegisteredItems();
}
public abstract class AbstractItemFactory implements ItemFactory {
protected final Map<String, Supplier<Item>> factory = Maps.newHashMap();
@Override
public Item getItem(String alias) throws ItemException {
try {
final Supplier<Item> supplier = factory.get(alias);
return supplier.get();
} catch (Exception e) {
throw new ItemException("Unable to create instance of " + alias, e);
}
}
protected Supplier<Item> defaultSupplier(Class<? extends Item> itemClass) {
return () -> {
try {
return itemClass.newInstance();
} catch (InstantiationException | IllegalAccessException e) {
throw new RuntimeException("Unable to create instance of " + itemClass, e);
}
};
}
@Override
public Set<String> getRegisteredItems() {
return factory.keySet();
}
}
public class GenericItemFactory extends AbstractItemFactory {
public GenericItemFactory() {
factory.put("reducedPriceItem", () -> new Discount(reducedPriceItem));
factory.put("salePriceItem", () -> new Sale(reducedPriceItem));
}
}
Run Code Online (Sandbox Code Playgroud)
销售和折扣是物品的实施.我使用ItemRegistry中的newInstance方法根据名称获取类.有人可以建议我任何类似的东西可以让我在scala中做同样的事情吗?
其他答案给出了以下选项:
这个答案提供了一种与“注册表模式”不同的方法,它使用编译器而不是字符串或 Spring 来解析实现。在 Scala 中,我们可以使用语言构造来注入蛋糕模式的依赖项。下面是使用类的简化版本的示例:
case class Order(id: Int)
trait Item {
// renamed to applyOrder to disambiguate it from apply(), which has special use in Scala
def applyOrder(order: Order): Unit
def name: String
}
trait Sale extends Item {
override def applyOrder(order: Order): Unit = println(s"sale on order[${order.id}]")
override def name: String = "sale"
}
trait Discount extends Item {
override def applyOrder(order: Order): Unit = println(s"discount on order[${order.id}]")
override def name: String = "discount"
}
Run Code Online (Sandbox Code Playgroud)
让我们定义一个Shopping依赖于 的类Item。我们可以将这种依赖关系表达为self 类型:
class Shopping { this: Item =>
def shop(order: Order): Unit = {
println(s"shopping with $name")
applyOrder(order)
}
}
Run Code Online (Sandbox Code Playgroud)
Shopping有一个方法 ,shop它调用其 上的applyOrder和name方法Item。让我们创建 的两个实例Shopping:一个有一个Sale项目,另一个有一个Discount项目...
val sale = new Shopping with Sale
val discount = new Shopping with Discount
Run Code Online (Sandbox Code Playgroud)
...并调用它们各自的shop方法:
val order1 = new Order(123)
sale.shop(order1)
// prints:
// shopping with sale
// sale on order[123]
val order2 = new Order(456)
discount.shop(order2)
// prints:
// shopping with discount
// discount on order[456]
Run Code Online (Sandbox Code Playgroud)
Item编译器要求我们在创建实例时混合实现Shopping。通过这种模式,我们可以在编译时强制执行依赖项,并且不需要第三方库。
| 归档时间: |
|
| 查看次数: |
1274 次 |
| 最近记录: |