行为链模式
我们研究了创意设计模式和结构设计模式。 在此博客上,我们将重点关注行为设计模式。
来自维基百科
行为设计模式是识别对象之间常见的通信模式并实现这些模式的设计模式。 通过这样做,这些模式增加了执行此通信的灵活性。
创新的设计模式实现了对象的创建。 结构设计模式提供了实现与对象之间关系的方法。 行为设计模式实现了对象之间的通信。
责任链模式与包含逻辑的处理对象所处理并传递给其他对象的命令对象有关。
我想到了许多例子。 其中之一是道路基础设施问题,需要由城镇,直辖市或州解决。
城镇应负责其道路问题,市政当局应处理与城镇,小村庄和村庄有关的基础设施,州政府应处理所有其余部分,例如城市道路,桥梁等。
有各种各样的道路事故坑洞,事故,您命名的桥梁倒塌。 这使我们进入了道路事故模型。
package com.gkatzioura.design.behavioural.cor;
public class RoadIncident {
private final double latitude;
private final double longtitude;
private final String report;
public RoadIncident(double latitude, double longtitude, String report) {
this.latitude = latitude;
this.longtitude = longtitude;
this.report = report;
}
public double getLatitude() {
return latitude;
}
public double getLongtitude() {
return longtitude;
}
public String getReport() {
return report;
}
}
纬度和经度是坐标。 根据坐标,每个地理区域应继续进行事件或将事件传递到下一个区域。
下一步是实现事件处理程序接口。
package com.gkatzioura.design.behavioural.cor;
public interface RoadIncidentHandler {
void handle(RoadIncident roadIncident);
boolean withinBounds(double lat, double lng);
}
应确定每个地理区域的事件是否在其范围之内,并应由其处理。 如果不是这样,则该事件应转发给其后继者。
我们应该从州道事件处理程序开始。
package com.gkatzioura.design.behavioural.cor;
public class StateRoadRoadIncidentHandler implements RoadIncidentHandler {
@Override
public void handle(RoadIncident roadIncident) {
if(!withinBounds(roadIncident.getLatitude(),roadIncident.getLatitude())) {
throw new IllegalArgumentException("Incident cannot be handled by state. No successor available");
}
/**
* Handle the incident
*/
}
@Override
public boolean withinBounds(double lat, double lng) {
/**
* Reverse geolocation, look up by name, radius based distance etc.
*/
return true;
}
}
因此国家没有继任者。 它是最高级别,因此,如果事件超出范围,那么它将抛出非法的参数异常。
然后,我们去市政当局。 市政事务处理人员确实有后继者。 可能是状态,也可能是我们将来可能出现的其他情况。
package com.gkatzioura.design.behavioural.cor;
public class MunicipalityRoadRoadIncidentHandler implements RoadIncidentHandler {
private final RoadIncidentHandler successor;
public MunicipalityRoadRoadIncidentHandler(final RoadIncidentHandler successor) {
this.successor = successor;
}
@Override
public void handle(RoadIncident roadIncident) {
if(!withinBounds(roadIncident.getLatitude(),roadIncident.getLongtitude())) {
successor.handle(roadIncident);
} else {
/**
* Handle the incident
*/
}
}
@Override
public boolean withinBounds(double lat, double lng) {
/**
* Reverse geolocation, look up by name, radius based distance etc.
*/
return false;
}
}
如果市政事件处理程序无法处理事件,则它将转发给其后继者。
最后一个将是城镇道路事件处理程序。
package com.gkatzioura.design.behavioural.cor;
public class TownRoadRoadIncidentHandler implements RoadIncidentHandler {
private final RoadIncidentHandler successor;
public TownRoadRoadIncidentHandler(final RoadIncidentHandler successor) {
this.successor = successor;
}
@Override
public void handle(RoadIncident roadIncident) {
if(!withinBounds(roadIncident.getLatitude(),roadIncident.getLongtitude())) {
successor.handle(roadIncident);
} else {
/**
* Handle the incident
*/
}
}
@Override
public boolean withinBounds(double lat, double lng) {
/**
* Reverse geolocation, look up by name, radius based distance etc.
*/
return false;
}
}
适用于市政当局的相同规则也适用于镇务人员。 如果事件不在其范围之内,它将转发给它的后继者。
让我们把它们放在一起。
package com.gkatzioura.design.behavioural.cor;
public class ChainOfResponsibility {
public static void main(String[] args) {
StateRoadRoadIncidentHandler state = new StateRoadRoadIncidentHandler();
MunicipalityRoadRoadIncidentHandler municipality = new MunicipalityRoadRoadIncidentHandler(state);
TownRoadRoadIncidentHandler firstTown = new TownRoadRoadIncidentHandler(municipality);
TownRoadRoadIncidentHandler secondTown = new TownRoadRoadIncidentHandler(state);
RoadIncident roadIncident = new RoadIncident(0d,0d,"Something happened");
firstTown.handle(roadIncident);
secondTown.handle(roadIncident);
}
}
因此,直辖市的继任者是国家。 该州也是第二镇的继任者。 第一个镇由市政府接任。
对于第一个镇,如果事件只能由州处理,则将事件从第一个镇转发到直辖市,然后再从直辖市转发到州。 关于第二镇,如果事件无法处理,它将转发给州。
您可以在github上找到源代码。
翻译自: https://www.javacodegeeks.com/2018/10/behavioural-patterns-chain-responsibility.html
行为链模式