java 行为参数化

The past isn’t here anymore, the future cannot be seen and the only thing permanent is the present moment and nothing else. Everything is the result of the change. There is nothing to question — change is the only constant in the life.

过去不再在这里,未来不可见,唯一的永恒是现在,而没有别的。 一切都是变化的结果。 毋庸置疑-变化是生活中唯一的常数。

A well-known and most irritating problem software industry is that no matter what we do, customer requirements will always be changed. For example, suppose you are developing an application to help your customer to understand his Employee. Your customer might want an API in your application to find all the employees who are older than 50 years. But the next day he might tell you, “Actually I also want to find all employees who are Manager.” Two days later, the same customer comes back and adds something new like “It would be really great if I could find all employees who is Manager and older than 50 years.” How can you manage these changing requirements for application?

一个众所周知且最令人困扰的问题软件行业是,无论我们做什么,客户需求都将不断变化。 例如,假设您正在开发一个应用程序来帮助您的客户了解其雇员。 您的客户可能希望应用程序中的API查找所有50岁以上的员工。 但是第二天他可能会告诉您:“实际上,我也想找到所有经理人员。” 两天后,同一位客户回来并添加了新内容,例如“如果我能找到所有经理和50岁以上的员工,那将是非常不错的。” 您如何管理这些不断变化的应用程序需求?

Behavior parameterization is a software development pattern that lets you how to handle frequent requirement changes. Behavior parameterization adds an ability to a method to receive multiple different behaviors as its parameter and use them internally to accomplish the task.

行为参数化是一种软件开发模式,可让您如何处理频繁的需求变更。 行为参数化为方法添加了一种功能,使其能够接收多种不同的行为作为其参数,并在内部使用它们来完成任务。

Let’s walk through an example and go through the Java code in the context of an EmployeeInfo application,

让我们来看一个示例,并在EmployeeInfo应用程序的上下文中浏览Java代码,

We have to implement an API to categorize employees who are from India.

我们必须实现一个API,对来自印度的员工进行分类。

  1. Naive and Simple Approach

    天真而简单的方法

private static List<Employee> getIndiaEmp(List<Employee> empList) {List<Employee> result = new ArrayList<>();//An Accumulator List for Employeefor(Employee emp : empList){if(emp.getCountry().equals("IN")){        // Filter, Select only Older employeeresult.add(emp);}}return result;}

But now the Customer changes his mind and wants to also get employees from other countries like the US, Germany, What can we do? The easiest solution would be to duplicate the above method getIndiaEmp, rename it as getUsemp(), and getGermanEmp(), But this approach doesn’t look good. Might be your customer has some other filter on the age.

但是现在,客户改变了主意,还希望从美国,德国等其他国家/地区招募员工,我们该怎么办? 最简单的解决方案是复制上述方法getIndiaEmp,将其重命名为getUsemp()和getGermanEmp(),但是这种方法看起来不太好。 可能是您的客户在年龄上有其他过滤条件。

2. Parameterizing the Address(Country)

2.参数化地址(国家)

What we can do is add a country as a parameter to the method to parameterize and be more flexible to such changes.

我们可以做的是将国家/地区作为参数添加到方法中,以进行参数设置并更灵活地应对此类更改。

private static List<Employee> getEmpByCountry(List<Employee> empList, String country) {List<Employee> result = new ArrayList<>();for(Employee emp : empList){if(emp.getCountry().equals(country)){result.add(emp);}}return result;}

We can now make the customer happy and call our method as belowList<Employee> usEmp = getEmpByCountry(empList,”US”);List<Employee> germanEmp = getEmpByCountry(empList,”GER”);List<Employee> indiaEmp = getEmpByCountry(empList,”IN”);

现在,我们可以使客户满意并调用以下方法:List <Employee> usEmp = getEmpByCountry(empList,“ US”); List <Employee> germanEmp = getEmpByCountry(empList,“ GER”); List <Employee> indiaEmp = getEmpByCountry( empList,“ IN”);

Suppose the same customer comes back says, We need to differentiate employees by age as older(age>40) and younger(age<40).A naive and simple solution is we can rename the above method getEmpByCountry() as getEmpByAge() with parameter employee list and age getEmpByAge(List<Employee>, age) but here most of codes are duplicate.

假设同一位客户回来说,我们需要按年龄(年龄大于40岁)和年龄小于40岁(年龄小于40岁)的不同来区分员工。天真而简单的解决方案是,可以将上述方法getEmpByCountry()重命名为getEmpByAge(),参数员工列表和年龄getEmpByAge(List <Employee>,age),但是此处大多数代码重复。

Now to eliminate the above problem like continuous requirement changes, a lot of duplicate codes, etc. We need to introduce a better level of abstraction.

现在,要消除上述问题,例如连续的需求变更,大量重复的代码等,我们需要引入更好的抽象级别。

3. Behavior parameterization using Different Strategies (Interface)

3.使用不同策略的行为参数化(接口)

We can create an interface

我们可以创建一个界面

interface EmployeePredicate{public boolean test(Employee employee);
}

We can create different type of predicates like

我们可以创建不同类型的谓词,例如

class USEmpPredicate implements EmployeePredicate{@Overridepublic boolean test(Employee employee) {return employee.getCountry().equals("US");}
}
class OlderEmpPredicate implements EmployeePredicate{@Overridepublic boolean test(Employee employee) {return employee.getAge()>50;}
}

Now we can create a new method filterEmp(List<Employee>, EmployeePredicate) as below

现在,我们可以如下创建一个新方法filterEmp(List <Employee>,EmployeePredicate)

private static List<Employee> filterEmp(List<Employee> empList, EmployeePredicate p){List<Employee> result  = new ArrayList<>();for(Employee emp : empList){if(p.test(emp)){result.add(emp);}}return result;}

We can use this method like this

我们可以这样使用

List<Employee> usEmp = filterEmp(empList, new USEmpPredicate());

List <Employee> usEmp = filterEmp(empList,new USEmpPredicate());

[Employee{name=’Kumar’, add=’US’, designation=’Manager’, age=45}, Employee{name=’Kavi’, add=’US’, designation=’Manager’, age=25}]

[Employee {name ='Kumar',add ='US',designation ='Manager',age = 45},Employee {name ='Kavi',add ='US',designation ='Manager',age = 25} ]

We can create a new predicate for an older employee who is from India as below.

我们可以为来自印度的年长雇员创建一个新的谓词,如下所示。

class OlderIndianEmpPredicate implements EmployeePredicate{@Overridepublic boolean test(Employee employee) {return employee.getAge()>50 &&employee.getCountry().equals("IN");}
}

We can use as below

我们可以如下使用

List<Employee> olderIndianEmp = filterEmp(empList, new OlderIndianEmpPredicate());[Employee{name=’Ram’, add=’IN’, designation=’Developer’, age=58}]

List <Employee> oldIndianEmp = filterEmp (empList,新的OlderIndianEmpPredicate()); [Employee {name ='Ram',add ='IN',designation ='Developer',age = 58}]

In this approach, We will have to write a lot of codes that introduces a lot of verbosities. To eliminate these verbosities we can use the java anonymous class.

在这种方法中,我们将不得不编写很多代码,引入很多详细信息。 为了消除这些冗长的细节,我们可以使用java匿名类。

4. Behavior parameterization using Anonymous classes

4.使用匿名类进行行为参数化

The following code shows how to rewrite the filtering example by creating an object that implements EmployeePredicate using an anonymous class.

以下代码显示如何通过创建使用匿名类实现EmployeePredicate的对象来重写过滤示例。

List<Employee> olderIndianEmp = filterEmp(empList, new EmployeePredicate() {@Overridepublic boolean test(Employee employee) {return employee.getAge()>50 && employee.getCountry().equals("IN");}});System.out.println(olderIndianEmp);

5. Behavior parameterization using Lambda expression

5.使用Lambda表达式进行行为参数化

How can we write the above code using the lambda expression.

我们如何使用lambda表达式编写上面的代码。

List<Employee> olderIndianEmp = filterEmp(empList, new EmployeePredicate() {@Overridepublic boolean test(Employee employee) {return employee.getAge()>50 && employee.getCountry().equals("IN");}});System.out.println(olderIndianEmp);//Lambda expression//emp.getAge()>50 && emp.getCountry().equals("IN") behaviour as a parameterList<Employee> olderIndianEmp_lambdaExp = filterEmp(empList, (Employee emp)-> emp.getAge()>50 && emp.getCountry().equals("IN"));System.out.println(olderIndianEmp_lambdaExp);
List<Employee> olderIndianEmp_lambdaExp = filterEmp(empList, (Employee emp)-> emp.getAge()>50 && emp.getCountry().equals("IN"));

Now We can see that behavior parameterization is a useful pattern to easily adapt to changing requirements. This pattern lets you encapsulate a behavior (a piece of code) and parameterize the behavior of methods bypassing and using these behaviors you create.

现在,我们可以看到行为参数化是一种易于适应不断变化的需求的有用模式。 通过这种模式,您可以封装行为(一段代码)并参数化绕过和使用您创建的这些行为的方法的行为。

Behavior parameterization is the ability of a method to take multiple different behaviors as parameters and use them internally to accomplish different behaviors. Behavior parameterization lets you make your code more adaptive to changing requirements and saves on engineering efforts in the future.

行为参数化是一种方法,可以将多个不同的行为作为参数,并在内部使用它们来完成不同的行为。 行为参数化使您可以使代码更适应不断变化的需求,并节省将来的工程工作。

This is all about Behavior parameterization in java. I know this is not complete. I have just given introductory information and the implementation of Behavior parameterization. If you like this information please go through that and please share your knowledge and understanding through comment, suggestion.

这一切都与java中的Behavior参数化有关。 我知道这还不完整。 我刚刚给出了介绍性信息和行为参数化的实现。 如果您喜欢此信息,请仔细阅读并通过评论,建议分享您的知识和理解。

翻译自: https://medium.com/javarevisited/behavior-parameterization-in-java-741591461622

java 行为参数化


http://www.taodudu.cc/news/show-6499855.html

相关文章:

  • 【学习】MybatisPlus + ShardingSphere 分表对象使用updateById方法自动补齐分表属性
  • MyBatis入门系列(18) -MyBatis四大组件之ParameterHandler源码及流程解析
  • 【MyBatis】MyBatis 核心配置综述之 ParameterHandler
  • 手机刷入recovery的方法
  • Android 线刷入Recovery.img
  • Windows使用cmd刷入recovery.img
  • fastboot 模式下刷入Recovery
  • 华为G700升级鸿蒙,华为G700刷入recovery的教程(移动版)
  • C# 控件属性一览表
  • 设置adobe reader pro的文本框字体属性
  • 查询校园网是否支持IPv6绕过校园网
  • 找不到认证服务器 是否网卡选择错误,锐捷上网认证常见问题
  • 免认证连接CQUST校园网
  • python蓝桥杯训练营一 等差素数列
  • 质数筛的应用——等差素数列
  • 慕课MOOC课程已结束或未开始解决方法
  • [中国大学Mooc]Web前端测试题
  • 三极管,MOS管,IGBT
  • 硬币排成线-LintCode
  • A字分拣机快速自动拆零拣选方案
  • 282、排列硬币
  • 平均需要扔多少次硬币才能够得到连续2个正面 [# 12]
  • 电商物流一分四分拣机的开发和源码
  • 我的世界服务器物品整理机,我的世界快速整理箱子 自动分拣机
  • 硬币问题(随机生成假硬币 找出它的位置)
  • 什么是组合,作用是什么?
  • 所有部门之间的比赛组合
  • 什么是组合
  • 什么是组合?有什么作用?
  • 组合之和

java 行为参数化_Java中的行为参数化相关推荐

  1. java list对象_JAVA中list

    Java 查找 List 中的最大值.最小值 Java 查找 List 中的最大值.最小值 java> List list = new ArrayList(); java.util.List l ...

  2. java mod %区别_Java中 % 与Math.floorMod() 区别详解

    %为取余(rem),Math.floorMod()为取模(mod) 取余取模有什么区别呢? 对于整型数a,b来说,取模运算或者取余运算的方法都是: 1.求 整数商: c = a/b; 2.计算模或者余 ...

  3. java show过时_Java中show() 方法被那个方法代替了? java编程 显示类中信

    你说的show是swing里的吧,在老版本中Component这个超类确实有show这个方法,而且这个方法也相当有用,使一个窗口可见,并放到最前面.在jdk5.0中阻止了这个方法,普遍用setVisi ...

  4. java判断类型_Java中类型判断的几种方式 - 码农小胖哥 - 博客园

    1. 前言 在Java这种强类型语言中类型转换.类型判断是经常遇到的.今天就细数一下Java中类型判断的方法方式. 2. instanceof instanceof是Java的一个运算符,用来判断一个 ...

  5. java 序列化实例_Java中的序列化与反序列化实例

    创建的字节流与平台无关.因此,在一个平台上序列化的对象可以在另一个平台上反序列化. 为了使Java对象可序列化,我们实现java.io.Serializable可序列化接口. ObjectOutput ...

  6. java 数据类型分为_JAVA中分为基本数据类型及引用数据类型

    byte:Java中最小的数据类型,在内存中占8位(bit),即1个字节,取值范围-128~127,默认值0 short:短整型,在内存中占16位,即2个字节,取值范围-32768~32717,默认值 ...

  7. java 代码锁_Java中的Lock锁

    Lock锁介绍: 在java中可以使用 synchronized 来实现多线程下对象的同步访问,为了获得更加灵活使用场景.高效的性能,java还提供了Lock接口及其实现类ReentrantLock和 ...

  8. java wait 参数_java中wait()和join()方法的区别是什么

    java中wait()和join()方法的区别是:存在不同的java包中:wait()方法用于线程间通信,它所施加的等待状态的线程可以被启动:join()方法用于在多个线程之间添加排序,它所施加的等待 ...

  9. java thread join()_Java中Thread.join()的使用方法

    概要 本文分三个部分对thread.join()进行分析: 1. join() 的示例和作用 2. join() 源码分析 3. 对网上其他分析 join() 的文章提出疑问 1. join() 的示 ...

最新文章

  1. Caffe、TensorFlow、MXnet三库对比
  2. 微信公众号接入图灵机器人实现自动回复消息
  3. 透過 OpenNI 建立 Kinect 3D Point Cloud
  4. linux的特殊权限SUID、SGID和SBIT
  5. React Native 蓝牙4.0 BLE开发
  6. Foundry feats. MultiverseStudio
  7. C#json数据的序列化和反序列化(将数据转换为对象或对象集合)
  8. keycloak mysql_keycloak搭配mysql
  9. 软件测试面试题(每日一刷)
  10. UltraEdit for Mac
  11. 微信公众号开发:消息处理
  12. 【持续更新】Java序列化对象释疑
  13. 【无标题】win7系统怎么配置adb环境变量
  14. 互联网架构师必备技能(使用markdown编写)
  15. xxx学校/学院/大学信息管理系统
  16. mmdetection踩坑记录
  17. 计算机net是什么意思翻译,net是什么意思_net翻译_读音_用法_翻译
  18. 《系统相关》Intel VT-x 处于禁用状态开启
  19. Windows Server 2016远程桌面服务配置方法
  20. Python版冈萨雷斯 V1.0 (二)

热门文章

  1. 计算机语言的发展历,计算机语言的发展历程
  2. 平面广告设计基本的要素有哪些
  3. 简单、彻底的解决 iOS 5.0.1 完美越狱 iBooks 秒退问题
  4. 【毕业设计】stm32单片机酒精浓度酒驾检测系统 - 物联网 嵌入式
  5. Android Studio值得推荐的主题背景
  6. 在 Ubuntu Lucid 下请回 Sun Java6 Jre, 赶走 OpenJDK(转)
  7. OSI参考模型与TCP/IP协议
  8. idea修改代码背景色
  9. 证金公司与转融通业务
  10. 身份证丢了怎么买高铁票?身份证丢了怎么坐车?