Java8的蛋糕模式可能吗? [英] Cake pattern with Java8 possible?

查看:140
本文介绍了Java8的蛋糕模式可能吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想知道:使用Java 8,并且可以在接口中添加实现(有点像Scala特性),是否可以实现蛋糕模式,就像我们在Scala中可以做的那样?

I just wonder: with Java 8, and the possibility to add implementation in interfaces (a bit like Scala traits), will it be possible to implement the cake pattern, like we can do in Scala?

如果是,有人可以提供代码片段吗?

If it is, can someone provide a code snippet?

推荐答案

在其他答案的启发下,我提出了以下(粗略)类层次结构,类似于Scala中的蛋糕模式:

With inspiration from other answers I came up with the following (rough) class hierarchy that is similar to the cake pattern in Scala:

interface UserRepository {
    String authenticate(String username, String password);
}

interface UserRepositoryComponent {
    UserRepository getUserRepository();
}

interface UserServiceComponent extends UserRepositoryComponent {
    default UserService getUserService() {
        return new UserService(getUserRepository());
    }
}

class UserService {
    private final UserRepository repository;

    UserService(UserRepository repository) {
        this.repository = repository;
    }

    String authenticate(String username, String password) {
        return repository.authenticate(username, password);
    }
}

interface LocalUserRepositoryComponent extends UserRepositoryComponent {
    default UserRepository getUserRepository() {
        return new UserRepository() {
            public String authenticate(String username, String password) {
                return "LocalAuthed";
            }
        };
    }
}

interface MongoUserRepositoryComponent extends UserRepositoryComponent {
    default UserRepository getUserRepository() {
        return new UserRepository() {
            public String authenticate(String username, String password) {
                return "MongoAuthed";
            }
        };
    }
}

class LocalApp implements UserServiceComponent, LocalUserRepositoryComponent {}
class MongoApp implements UserServiceComponent, MongoUserRepositoryComponent {}

以上编译自Java 8截至2013年1月9日。

The above compiles on Java 8 as of Jan.9 2013.

那么,Java 8可以做一个像一样的模式吗? 是。

So, can Java 8 do a cake-like pattern? Yes.

它是否与Scala一样简洁,还是与Java中的其他模式一样有效(即依赖注入)?
可能不是,上面的草图需要大量的文件,并不像Scala那样简洁。

Is it as terse as Scala, or as effective as other patterns in Java (i.e. dependency injection)? Probably not, the above sketch required a whole lot of files and is not as terse as Scala.

总结:


  • 可以通过扩展我们期望的基本接口来模拟自我类型(根据蛋糕模式的需要)。

  • 接口不能有内部类(由@Owen指出),所以我们可以使用匿名类。

  • val var 可以通过使用静态hashmap(和延迟初始化)来模拟,或者由类的客户端简单地存储值(就像UserService那样)。

  • 我们可以在默认界面方法中使用 this.getClass()来发现我们的类型。

  • 正如@Owen所说,路径依赖类型使用接口是不可能的,因此完整的蛋糕模式本质上是不可能的。但是,上面显示可以使用它进行依赖注入。

  • Self-types (as needed for the cake pattern) can be emulated by extending the base interface we expect.
  • Interfaces cannot have inner classes (as noted by @Owen), so instead we can use anonymous classes.
  • val and var can be emulated by using a static hashmap (and lazy initialization), or by the client of the class simply storing the value on their side (like UserService does).
  • We can discover our type by using this.getClass() in a default interface method.
  • As @Owen notes, path dependent types are impossible using interfaces, so a full cake pattern is inherently impossible. The above shows, however, that one could use it for dependency injection.

这篇关于Java8的蛋糕模式可能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆