spring增强访问者模式-改造后-jdk17 Pattern Matching

This commit is contained in:
fujiayang
2022-01-26 11:30:05 +08:00
parent 3f52da2575
commit d81b7beb05
4 changed files with 24 additions and 6 deletions

2
.idea/misc.xml generated
View File

@@ -11,7 +11,7 @@
</list> </list>
</option> </option>
</component> </component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" project-jdk-name="1.8" project-jdk-type="JavaSDK"> <component name="ProjectRootManager" version="2" languageLevel="JDK_17" project-jdk-name="17" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" /> <output url="file://$PROJECT_DIR$/out" />
</component> </component>
</project> </project>

View File

@@ -12,8 +12,8 @@
<properties> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target> <maven.compiler.target>17</maven.compiler.target>
<org.springframework.version>5.1.0.RELEASE</org.springframework.version> <org.springframework.version>5.1.0.RELEASE</org.springframework.version>
</properties> </properties>

View File

@@ -1,8 +1,5 @@
package top.fjy8018.designpattern.pattern.creational.factorymethod; package top.fjy8018.designpattern.pattern.creational.factorymethod;
import sun.misc.Launcher;
import java.util.Collection;
/** /**
* 定义一个创建产品对象的工厂接口,将实际创建工作推迟到子类当中 * 定义一个创建产品对象的工厂接口,将实际创建工作推迟到子类当中

View File

@@ -29,6 +29,14 @@ class DrawServiceTest {
draw(new Factory()); draw(new Factory());
} }
/**
* 输出draw factory
*/
@Test
public void draw17() {
draw17(new Factory());
}
@Test @Test
public void draw2() { public void draw2() {
Visitor drawVisitor = new DrawVisitor(); Visitor drawVisitor = new DrawVisitor();
@@ -52,4 +60,17 @@ class DrawServiceTest {
} }
} }
private void draw17(Node node) {
DrawService drawService = new DrawService();
if (node instanceof Building building) {
drawService.draw(building);
} else if (node instanceof Factory factory) {
drawService.draw(factory);
} else if (node instanceof School school) {
drawService.draw(school);
} else {
drawService.draw(node);
}
}
} }