当前位置: 首页 > 编程笔记 >

Design patterns 使用Java 8功能接口实现Strategy模式

韩善
2023-03-14
本文向大家介绍Design patterns 使用Java 8功能接口实现Strategy模式,包括了Design patterns 使用Java 8功能接口实现Strategy模式的使用技巧和注意事项,需要的朋友参考一下

示例

本示例的目的是展示如何使用Java 8功能接口实现策略模式。我们将从经典Java中的简单用例代码开始,然后以Java 8方式对其进行重新编码。

我们使用的示例问题是一系列算法(策略),描述了远距离通信的不同方式。

经典Java版本

我们的算法系列的合同由以下接口定义:

public interface CommunicateInterface {
    public String communicate(String destination);
}

然后,我们可以实现许多算法,如下所示:

public class CommunicateViaPhone implements CommunicateInterface {
    @Override
    public String communicate(String destination) {
        return "communicating " + destination +" via Phone..";
    }
}

public class CommunicateViaEmail implements CommunicateInterface {
    @Override
    public String communicate(String destination) {
        return "communicating " + destination + " via Email..";
    }
}

public class CommunicateViaVideo implements CommunicateInterface {
    @Override
    public String communicate(String destination) {
        return "communicating " + destination + " via Video..";
    }
}

这些可以实例化如下:

CommunicateViaPhone communicateViaPhone = new CommunicateViaPhone();
CommunicateViaEmail communicateViaEmail = new CommunicateViaEmail();
CommunicateViaVideo communicateViaVideo = new CommunicateViaVideo();

接下来,我们实现使用该策略的服务:

public class CommunicationService {
    private CommunicateInterface communcationMeans;

    public void setCommuncationMeans(CommunicateInterface communcationMeans) {
       this.communcationMeans= communcationMeans;
    }

    public void communicate(String destination) {
        this.communcationMeans.communicate(destination);
    }
}

最后,我们可以使用以下不同策略:

CommunicationService communicationService = new CommunicationService();

// 通过电话
communicationService.setCommuncationMeans(communicateViaPhone);
communicationService.communicate("1234567");

// 通过电子邮件
communicationService.setCommuncationMeans(communicateViaEmail);
communicationService.communicate("hi@me.com");

使用Java 8功能接口

不同算法实现的协定不需要专用接口。相反,我们可以使用现有java.util.function.Function<T, R>接口对其进行描述。

组成不同的算法the family of algorithms可以表示为lambda表达式。这取代了战略类它们的实例。

Function<String, String> communicateViaEmail = 
        destination -> "communicating " + destination + " via Email..";
Function<String, String> communicateViaPhone = 
        destination -> "communicating " + destination + " via Phone..";
Function<String, String> communicateViaVideo = 
        destination -> "communicating " + destination + " via Video..";

接下来,我们可以对“服务”进行如下编码:

public class CommunicationService {
    private Function<String, String> communcationMeans;

    public void setCommuncationMeans(Function<String, String> communcationMeans) {
       this.communcationMeans= communcationMeans;
    }

    public void communicate(String destination) {
        this.communcationMeans.communicate(destination);
    }
}

最后,我们使用以下策略

CommunicationService communicationService = new CommunicationService();

// 通过电话
communicationService.setCommuncationMeans(communicateViaPhone);
communicationService.communicate("1234567");

// 通过电子邮件
communicationService.setCommuncationMeans(communicateViaEmail);
communicationService.communicate("hi@me.com");

甚至:

communicationService.setCommuncationMeans(
    destination -> "communicating " + destination + " via Smoke signals.." );
CommunicationService.communicate("anyone");
           

 类似资料:
  • 还是这些标准的功能接口(消费者、供应商、谓词、函数)是用来充当代码组织、可读性、结构、[其他]的模板?

  • 问题内容: 关于Java8 内置 功能接口,我遇到了许多问题,包括this,this和this。但是所有人都问“为什么只有一种方法?” 或“如果使用功能接口执行X,为什么会出现编译错误”等。我的问题是: 当我仍然可以在自己的接口中使用lambda时,这些新功能接口的存在目的 是 什么 ? 考虑一下oracle文档中的以下示例代码: 好的,很好,但这可以通过上面自己的示例来实现(带有单个方法的接口已

  • 更准确地说,函数接口定义为具有一个抽象方法的任何接口。 然后他继续介绍示例,其中一个是接口: 我能够测试是否可以使用lambda函数代替比较器参数,并且它能够工作(例如)。

  • 本文向大家介绍ThinkPHP实现支付宝接口功能实例,包括了ThinkPHP实现支付宝接口功能实例的使用技巧和注意事项,需要的朋友参考一下 本文实例讲述了ThinkPHP实现支付宝接口功能的方法。分享给大家供大家参考。具体分析如下: 最近做系统,需要实现在线支付功能,毫不犹豫,选择的是支付宝的接口支付功能,这里我用的是即时到帐的接口,具体实现的步骤如下: 一、下载支付宝接口包 下载地址:https

  • 本文向大家介绍php接口实现拖拽排序功能,包括了php接口实现拖拽排序功能的使用技巧和注意事项,需要的朋友参考一下 列表拖拽排序是一个很常见的功能,但是后端接口如何处理却是一个令人纠结的问题 如何实现才能达到效率最高呢? 先分析一个场景,假如有一个页面有十条数据,所谓的拖拽就是在这十条数据来来回回的拖,但是每次拖动都会影响到其他数据例如把最后一条拖到最前面,那么后面九条就自动往后移,反之也是,嗯~

  • 在Java8中,新的包是Java。util。函数包含很多函数接口。该软件包的文档(http://docs.oracle.com/javase/8/docs/api/java/util/function/package-summary.html)多次提到“函数形状”: 有几种基本函数形状,包括函数(从T到R的一元函数)、消费者(从T到void的一元函数)、谓词(从T到布尔的一元函数)和供应者(从T到