spring增强访问者模式-改造前(early binding)
This commit is contained in:
1
.idea/compiler.xml
generated
1
.idea/compiler.xml
generated
@@ -2,6 +2,7 @@
|
|||||||
<project version="4">
|
<project version="4">
|
||||||
<component name="CompilerConfiguration">
|
<component name="CompilerConfiguration">
|
||||||
<annotationProcessing>
|
<annotationProcessing>
|
||||||
|
<profile default="true" name="Default" enabled="true" />
|
||||||
<profile name="Maven default annotation processors profile" enabled="true">
|
<profile name="Maven default annotation processors profile" enabled="true">
|
||||||
<sourceOutputDir name="target/generated-sources/annotations" />
|
<sourceOutputDir name="target/generated-sources/annotations" />
|
||||||
<sourceTestOutputDir name="target/generated-test-sources/test-annotations" />
|
<sourceTestOutputDir name="target/generated-test-sources/test-annotations" />
|
||||||
|
|||||||
@@ -0,0 +1,33 @@
|
|||||||
|
package top.fjy8018.designpattern.pattern.behavior.visitor.spring;
|
||||||
|
|
||||||
|
import top.fjy8018.designpattern.pattern.behavior.visitor.spring.bean.Building;
|
||||||
|
import top.fjy8018.designpattern.pattern.behavior.visitor.spring.bean.Factory;
|
||||||
|
import top.fjy8018.designpattern.pattern.behavior.visitor.spring.bean.Node;
|
||||||
|
import top.fjy8018.designpattern.pattern.behavior.visitor.spring.bean.School;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 新需求:需要加入绘制Node功能
|
||||||
|
* Node接口作为衔接组件的桥梁,应该尽可能保持稳定,不应频繁更改。
|
||||||
|
* 定义一个新的类DrawService,把所有的draw逻辑都写在这里面
|
||||||
|
*
|
||||||
|
* @author F嘉阳
|
||||||
|
* @date 2022/1/26 10:18
|
||||||
|
*/
|
||||||
|
public class DrawService {
|
||||||
|
|
||||||
|
public void draw(Building building) {
|
||||||
|
System.out.println("draw building");
|
||||||
|
}
|
||||||
|
|
||||||
|
public void draw(Factory factory) {
|
||||||
|
System.out.println("draw factory");
|
||||||
|
}
|
||||||
|
|
||||||
|
public void draw(School school) {
|
||||||
|
System.out.println("draw school");
|
||||||
|
}
|
||||||
|
|
||||||
|
public void draw(Node node) {
|
||||||
|
System.out.println("draw node");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
package top.fjy8018.designpattern.pattern.behavior.visitor.spring.bean;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* TODO:
|
||||||
|
*
|
||||||
|
* @author F嘉阳
|
||||||
|
* @date 2022/1/26 10:18
|
||||||
|
*/
|
||||||
|
public class Building implements Node {
|
||||||
|
@Override
|
||||||
|
public String getName() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getDescription() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
package top.fjy8018.designpattern.pattern.behavior.visitor.spring.bean;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* TODO:
|
||||||
|
*
|
||||||
|
* @author F嘉阳
|
||||||
|
* @date 2022/1/26 10:18
|
||||||
|
*/
|
||||||
|
public class Factory implements Node {
|
||||||
|
@Override
|
||||||
|
public String getName() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getDescription() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
package top.fjy8018.designpattern.pattern.behavior.visitor.spring.bean;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 地图节点
|
||||||
|
*
|
||||||
|
* @author F嘉阳
|
||||||
|
* @date 2022/1/26 10:17
|
||||||
|
*/
|
||||||
|
public interface Node {
|
||||||
|
|
||||||
|
String getName();
|
||||||
|
|
||||||
|
String getDescription();
|
||||||
|
}
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
package top.fjy8018.designpattern.pattern.behavior.visitor.spring.bean;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* TODO:
|
||||||
|
*
|
||||||
|
* @author F嘉阳
|
||||||
|
* @date 2022/1/26 10:18
|
||||||
|
*/
|
||||||
|
public class School implements Node {
|
||||||
|
@Override
|
||||||
|
public String getName() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getDescription() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
package top.fjy8018.designpattern.pattern.behavior.visitor.spring;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import top.fjy8018.designpattern.pattern.behavior.visitor.spring.bean.Factory;
|
||||||
|
import top.fjy8018.designpattern.pattern.behavior.visitor.spring.bean.Node;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* DrawService中只有4个draw方法,参数类型分别是Factory, Building, School和Node,如果调用方传了一个City进来怎么办?
|
||||||
|
* 毕竟调用方可以自己实现一个City类传进来。这种情况下程序该调用什么方法呢?
|
||||||
|
* 我们没有draw(City)方法,为了防止这种情况发生,程序在编译阶段就直接选择使用DrawService::draw(Node)方法。
|
||||||
|
* 无论调用方传了什么实现进来,我们都统一使用DrawService::draw(Node)方法以确保程序安全运行。
|
||||||
|
* 在编译时(而不是运行时)决定调用哪个方法,这就叫做Static/Early Binding。
|
||||||
|
* 这也就解释了我们为什么输出了 draw node 。
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
class DrawServiceTest {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 输出:draw node
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void draw() {
|
||||||
|
draw(new Factory());
|
||||||
|
}
|
||||||
|
|
||||||
|
private void draw(Node node) {
|
||||||
|
DrawService drawService = new DrawService();
|
||||||
|
drawService.draw(node);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user