关于面向对象编程的接口隔离原则,你都会了吗?

接口理解为 OOP 中的接口概念

我们可以把接口理解为OOP中的接口概念,比如Java中的interface

假设我们的项目中用到了三个外部系统:RedisMySQLKafka。每个系统都对应一系列配置信息,地址、端口、访问超时时间等。

为了在内存中存储这些配置信息,供项目中的其他模块来使用,我们分别设计实现了三个 Configuration 类:RedisConfig、MysqlConfig、KafkaConfig。具体的代码实现如下所示。注意,这里我只给出了 RedisConfig 的代码实现,另外两个都是类似。

public class RedisConfig {

    private ConfigSource configSource; // 配置中心(比如 zookeeper)

    private String address;

    private int timeout;

    private int maxTotal;

    // 省略其他配置: maxWaitMillis,maxIdle,minIdle...

    public RedisConfig(ConfigSource configSource) {

        this.configSource = configSource;

    }

    public String getAddress() {

        return this.address;

    }

    //... 省略其他 get ()、init () 方法...

    public void update() {

      // 从 configSource 加载配置到 address/timeout/maxTotal...

    }

}

public class KafkaConfig { //... 省略... }

public class MysqlConfig { //... 省略... }

现在,我们有一个新的功能需求,希望支持 Redis 和 Kafka 配置信息的热更新。所谓 “热更新(hot update)” 就是,如果在配置中心中更改了配置信息,我们希望在不用重启系统的情况下,能将最新的配置信息加载到内存中(也就是 RedisConfig、KafkaConfig 类中)。但是,因为某些原因,我们并不希望对 MySQL 的配置信息进行热更新。

为了实现这样一个功能需求,我们设计实现了一个 ScheduledUpdater 类,以固定时间频率(periodInSeconds)来调用 RedisConfig、KafkaConfig 的 update () 方法更新配置信息。具体的代码实现如下所示:

public interface Updater {

  void update();

}

public class RedisConfig implemets Updater {

  //... 省略其他属性和方法...

  @Override

  public void update() { //... }

}

public class KafkaConfig implements Updater {

  //... 省略其他属性和方法...

  @Override

  public void update() { //... }

}

public class MysqlConfig { //... 省略其他属性和方法... }

public class ScheduledUpdater {

    private final ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();;

    private long initialDelayInSeconds;

    private long periodInSeconds;

    private Updater updater;

    public ScheduleUpdater(Updater updater, long initialDelayInSeconds, long periodInSeconds) {

        this.updater = updater;

        this.initialDelayInSeconds = initialDelayInSeconds;

        this.periodInSeconds = periodInSeconds;

    }

    public void run() {

        executor.scheduleAtFixedRate(new Runnable() {

            @Override

            public void run() {

                updater.update();

            }

        }, this.initialDelayInSeconds, this.periodInSeconds, TimeUnit.SECONDS);

    }

}

public class Application {

  ConfigSource configSource = new ZookeeperConfigSource(/* 省略参数 */);

  public static final RedisConfig redisConfig = new RedisConfig(configSource);

  public static final KafkaConfig kafkaConfig = new KakfaConfig(configSource);

  public static final MySqlConfig mysqlConfig = new MysqlConfig(configSource);

  public static void main(String[] args) {

    ScheduledUpdater redisConfigUpdater = new ScheduledUpdater(redisConfig, 300, 300);

    redisConfigUpdater.run();

    ScheduledUpdater kafkaConfigUpdater = new ScheduledUpdater(kafkaConfig, 60, 60);

    redisConfigUpdater.run();

  }

}

刚刚的热更新的需求我们已经搞定了。现在,我们又有了一个新的监控功能需求。通过命令行来查看 Zookeeper 中的配置信息是比较麻烦的。所以,我们希望能有一种更加方便的配置信息查看方式。

我们可以在项目中开发一个内嵌的 SimpleHttpServer,输出项目的配置信息到一个固定的 HTTP 地址,比如:http://127.0.0.1:2389/config 。我们只需要在浏览器中输入这个地址,就可以显示出系统的配置信息。不过,出于某些原因,我们只想暴露 MySQL 和 Redis 的配置信息,不想暴露 Kafka 的配置信息。

为了实现这样一个功能,我们还需要对上面的代码做进一步改造。改造之后的代码如下所示:

public interface Updater {

  void update();

}

public interface Viewer {

  String outputInPlainText();

  Map<String, String> output();

}

public class RedisConfig implemets Updater, Viewer {

  //... 省略其他属性和方法...

  @Override

  public void update() { //... }

  @Override

  public String outputInPlainText() { //... }

  @Override

  public Map<String, String> output() { //...}

}

public class KafkaConfig implements Updater {

  //... 省略其他属性和方法...

  @Override

  public void update() { //... }

}

public class MysqlConfig implements Viewer {

  //... 省略其他属性和方法...

  @Override

  public String outputInPlainText() { //... }

  @Override

  public Map<String, String> output() { //...}

}

public class SimpleHttpServer {

  private String host;

  private int port;

  private Map<String, List<Viewer>> viewers = new HashMap<>();

  public SimpleHttpServer(String host, int port) {//...}

  public void addViewers(String urlDirectory, Viewer viewer) {

    if (!viewers.containsKey(urlDirectory)) {

      viewers.put(urlDirectory, new ArrayList<Viewer>());

    }

    this.viewers.get(urlDirectory).add(viewer);

  }

  public void run() { //... }

}

public class Application {

    ConfigSource configSource = new ZookeeperConfigSource();

    public static final RedisConfig redisConfig = new RedisConfig(configSource);

    public static final KafkaConfig kafkaConfig = new KakfaConfig(configSource);

    public static final MySqlConfig mysqlConfig = new MySqlConfig(configSource);

    public static void main(String[] args) {

        ScheduledUpdater redisConfigUpdater =

            new ScheduledUpdater(redisConfig, 300, 300);

        redisConfigUpdater.run();

        ScheduledUpdater kafkaConfigUpdater =

            new ScheduledUpdater(kafkaConfig, 60, 60);

        redisConfigUpdater.run();

        SimpleHttpServer simpleHttpServer = new SimpleHttpServer(“127.0.0.1”, 2389);

        simpleHttpServer.addViewer("/config", redisConfig);

        simpleHttpServer.addViewer("/config", mysqlConfig);

        simpleHttpServer.run();

    }

}

至此,热更新和监控的需求我们就都实现了。我们来回顾一下这个例子的设计思想。

我们设计了两个功能非常单一的接口:Updater 和 Viewer。ScheduledUpdater 只依赖 Updater 这个跟热更新相关的接口,不需要被强迫去依赖不需要的 Viewer 接口,满足接口隔离原则。同理,SimpleHttpServer 只依赖跟查看信息相关的 Viewer 接口,不依赖不需要的 Updater 接口,也满足接口隔离原则。

你可能会说,如果我们不遵守接口隔离原则,不设计 Updater 和 Viewer 两个小接口,而是设计一个大而全的 Config 接口,让 RedisConfig、KafkaConfig、MysqlConfig 都实现这个 Config 接口,并且将原来传递给 ScheduledUpdater 的 Updater 和传递给 SimpleHttpServer 的 Viewer,都替换为 Config,那会有什么问题呢?我们先来看一下,按照这个思路来实现的代码是什么样的。


public interface Config {

  void update();

  String outputInPlainText();

  Map<String, String> output();

}

public class RedisConfig implements Config {

  //... 需要实现 Config 的三个接口 update/outputIn.../output

}

public class KafkaConfig implements Config {

  //... 需要实现 Config 的三个接口 update/outputIn.../output

}

public class MysqlConfig implements Config {

  //... 需要实现 Config 的三个接口 update/outputIn.../output

}

public class ScheduledUpdater {

  //... 省略其他属性和方法..

  private Config config;

  public ScheduleUpdater(Config config, long initialDelayInSeconds, long periodInSeconds) {

      this.config = config;

      //...

  }

  //...

}

public class SimpleHttpServer {

  private String host;

  private int port;

  private Map<String, List<Config>> viewers = new HashMap<>();

  public SimpleHttpServer(String host, int port) {//...}

  public void addViewer(String urlDirectory, Config config) {

    if (!viewers.containsKey(urlDirectory)) {

      viewers.put(urlDirectory, new ArrayList<Config>());

    }

    viewers.get(urlDirectory).add(config);

  }

  public void run() { //... }

}

这样的设计思路也是能工作的,但是对比前后两个设计思路,在同样的代码量、实现复杂度、同等可读性的情况下,第一种设计思路显然要比第二种好很多。为什么这么说呢?主要有两点原因。

首先,第一种设计思路更加灵活、易扩展、易复用。

因为 Updater、Viewer 职责更加单一,单一就意味了通用、复用性好。比如,我们现在又有一个新的需求,开发一个 Metrics 性能统计模块,并且希望将 Metrics 也通过 SimpleHttpServer 显示在网页上,以方便查看。尽管 Metrics 跟 RedisConfig 等没有任何关系,但我们仍然可以让 Metrics 类实现非常通用的 Viewer 接口,复用 SimpleHttpServer 的代码实现。具体的代码如下所示:


public class ApiMetrics implements Viewer {//...}

public class DbMetrics implements Viewer {//...}

public class Application {

    ConfigSource configSource = new ZookeeperConfigSource();

    public static final RedisConfig redisConfig = new RedisConfig(configSource);

    public static final KafkaConfig kafkaConfig = new KakfaConfig(configSource);

    public static final MySqlConfig mySqlConfig = new MySqlConfig(configSource);

    public static final ApiMetrics apiMetrics = new ApiMetrics();

    public static final DbMetrics dbMetrics = new DbMetrics();

    public static void main(String[] args) {

        SimpleHttpServer simpleHttpServer = new SimpleHttpServer(“127.0.0.1”, 2389);

        simpleHttpServer.addViewer("/config", redisConfig);

        simpleHttpServer.addViewer("/config", mySqlConfig);

        simpleHttpServer.addViewer("/metrics", apiMetrics);

        simpleHttpServer.addViewer("/metrics", dbMetrics);

        simpleHttpServer.run();

    }

}

其次,第二种设计思路在代码实现上做了一些无用功。

因为 Config 接口中包含两类不相关的接口,一类是 update (),一类是 output () 和 outputInPlainText ()。理论上,KafkaConfig 只需要实现 update () 接口,并不需要实现 output () 相关的接口。

同理,MysqlConfig 只需要实现 output () 相关接口,并需要实现 update () 接口。但第二种设计思路要求 RedisConfig、KafkaConfig、MySqlConfig 必须同时实现 Config 的所有接口函数(update、output、outputInPlainText)。

除此之外,如果我们要往 Config 中继续添加一个新的接口,那所有的实现类都要改动。相反,如果我们的接口粒度比较小,那涉及改动的类就比较少。

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注

关注我们