spring增强访问者模式-改造前(early binding)解决
This commit is contained in:
@@ -2,8 +2,10 @@ package top.fjy8018.designpattern.pattern.behavior.visitor.spring;
|
|||||||
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
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.Factory;
|
||||||
import top.fjy8018.designpattern.pattern.behavior.visitor.spring.bean.Node;
|
import top.fjy8018.designpattern.pattern.behavior.visitor.spring.bean.Node;
|
||||||
|
import top.fjy8018.designpattern.pattern.behavior.visitor.spring.bean.School;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* DrawService中只有4个draw方法,参数类型分别是Factory, Building, School和Node,如果调用方传了一个City进来怎么办?
|
* DrawService中只有4个draw方法,参数类型分别是Factory, Building, School和Node,如果调用方传了一个City进来怎么办?
|
||||||
@@ -26,7 +28,18 @@ class DrawServiceTest {
|
|||||||
|
|
||||||
private void draw(Node node) {
|
private void draw(Node node) {
|
||||||
DrawService drawService = new DrawService();
|
DrawService drawService = new DrawService();
|
||||||
|
if (node instanceof Building) {
|
||||||
|
Building building = (Building) node;
|
||||||
|
drawService.draw(building);
|
||||||
|
} else if (node instanceof Factory) {
|
||||||
|
Factory factory = (Factory) node;
|
||||||
|
drawService.draw(factory);
|
||||||
|
} else if (node instanceof School) {
|
||||||
|
School school = (School) node;
|
||||||
|
drawService.draw(school);
|
||||||
|
} else {
|
||||||
drawService.draw(node);
|
drawService.draw(node);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user