Skip to content
Prev Previous commit
Next Next commit
完成 访问者模式 Sample
  • Loading branch information
JamesZBL committed Nov 30, 2017
commit 20f3d1edea36d46ef0603a5e3fe2857046cbca09
45 changes: 45 additions & 0 deletions visitor/src/main/java/me/zbl/visitor/Boss.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* MIT License
* <p>
* Copyright (c) 2017 James
* <p>
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* <p>
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
* <p>
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package me.zbl.visitor;

/**
* 老板
*/
public class Boss extends Unit {

public Boss(Unit... children) {
super(children);
}

@Override
public void beVisited(UnitVisitor visitor) {
visitor.visitBoss(this);
super.beVisited(visitor);
}

@Override
public String toString() {
return "老板";
}
}
50 changes: 50 additions & 0 deletions visitor/src/main/java/me/zbl/visitor/BossVisitor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/**
* MIT License
* <p>
* Copyright (c) 2017 James
* <p>
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* <p>
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
* <p>
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package me.zbl.visitor;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* 老板的访问者
*/
public class BossVisitor implements UnitVisitor {

private static final Logger LOGGER = LoggerFactory.getLogger(BossVisitor.class);

@Override
public void visitEngineer(Engineer engineer) {

}

@Override
public void visitBoss(Boss boss) {
LOGGER.info("你好,{}", boss);
}

@Override
public void visitManager(Manager manager) {

}
}
45 changes: 45 additions & 0 deletions visitor/src/main/java/me/zbl/visitor/Engineer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* MIT License
* <p>
* Copyright (c) 2017 James
* <p>
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* <p>
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
* <p>
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package me.zbl.visitor;

/**
* 工程师
*/
public class Engineer extends Unit {

public Engineer(Unit... children) {
super(children);
}

@Override
public void beVisited(UnitVisitor visitor) {
visitor.visitEngineer(this);
super.beVisited(visitor);
}

@Override
public String toString() {
return "工程师";
}
}
50 changes: 50 additions & 0 deletions visitor/src/main/java/me/zbl/visitor/EngineerVisitor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/**
* MIT License
* <p>
* Copyright (c) 2017 James
* <p>
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* <p>
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
* <p>
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package me.zbl.visitor;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* 工程师的访问者
*/
public class EngineerVisitor implements UnitVisitor {

private static final Logger LOGGER = LoggerFactory.getLogger(EngineerVisitor.class);

@Override
public void visitEngineer(Engineer engineer) {
LOGGER.info("你好,{}", engineer);
}

@Override
public void visitBoss(Boss boss) {

}

@Override
public void visitManager(Manager manager) {

}
}
45 changes: 45 additions & 0 deletions visitor/src/main/java/me/zbl/visitor/Manager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* MIT License
* <p>
* Copyright (c) 2017 James
* <p>
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* <p>
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
* <p>
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package me.zbl.visitor;

/**
* 经理
*/
public class Manager extends Unit {

public Manager(Unit... children) {
super(children);
}

@Override
public void beVisited(UnitVisitor visitor) {
visitor.visitManager(this);
super.beVisited(visitor);
}

@Override
public String toString() {
return "经理";
}
}
50 changes: 50 additions & 0 deletions visitor/src/main/java/me/zbl/visitor/ManagerVisitor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/**
* MIT License
* <p>
* Copyright (c) 2017 James
* <p>
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* <p>
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
* <p>
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package me.zbl.visitor;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* 经理的访问者
*/
public class ManagerVisitor implements UnitVisitor {

private static final Logger LOGGER = LoggerFactory.getLogger(ManagerVisitor.class);

@Override
public void visitEngineer(Engineer engineer) {

}

@Override
public void visitBoss(Boss boss) {

}

@Override
public void visitManager(Manager manager) {
LOGGER.info("你好,{}", manager);
}
}
45 changes: 45 additions & 0 deletions visitor/src/main/java/me/zbl/visitor/Unit.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* MIT License
* <p>
* Copyright (c) 2017 James
* <p>
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* <p>
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
* <p>
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package me.zbl.visitor;

/**
* 可访问的单元
*/
public abstract class Unit {

private Unit[] children;

public Unit(Unit... children) {
this.children = children;
}

/**
* 接受访问
*/
public void beVisited(UnitVisitor visitor) {
for (Unit childUnit : children) {
childUnit.beVisited(visitor);
}
}
}
36 changes: 36 additions & 0 deletions visitor/src/main/java/me/zbl/visitor/UnitVisitor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* MIT License
* <p>
* Copyright (c) 2017 James
* <p>
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* <p>
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
* <p>
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package me.zbl.visitor;

/**
* 访问者接口
*/
public interface UnitVisitor {

void visitEngineer(Engineer engineer);

void visitBoss(Boss boss);

void visitManager(Manager manager);
}