访问者模式Visitor Pattern
中属于行为型模式,针对于对象结构中的元素,定义在不改变该对象的前提下访问结构中元素的新方法,元素的执行算法可以随着访问者改变而改变,主要意图在于将数据结构与数据操作分离,解决稳定的数据结构和易变的操作耦合问题。
在面向对象编程和软件工程中,访问者设计模式是一种将算法与它所运行的对象结构分离的方法,这种分离的实际结果是能够在不修改现有对象结构的情况下向现有对象结构添加新操作,这是遵循开放封闭原则的一种方式。
// 以动物园模拟为例,我们有几种不同种类的动物,它们能够发出不同的声音。
class Monkey {
shout() {
console.log("Ooh oo aa aa!");
}
accept(operation) {
operation.visitMonkey(this);
}
}
class Lion {
roar() {
console.log("Roaaar!");
}
accept(operation) {
operation.visitLion(this);
}
}
class Dolphin {
speak() {
console.log("Tuut tuttu tuutt!");
}
accept(operation) {
operation.visitDolphin(this);
}
}
const speak = {
visitMonkey(monkey) {
monkey.shout();
},
visitLion(lion) {
lion.roar();
},
visitDolphin(dolphin) {
dolphin.speak();
}
}
;(function(){
const monkey = new Monkey();
const lion = new Lion();
const dolphin = new Dolphin();
monkey.accept(speak); // Ooh oo aa aa!
lion.accept(speak); // Roaaar!
dolphin.accept(speak); // Tuut tutt tuutt!
})();
// 我们可以通过对动物具有继承层次结构来简单地做到这一点
// 但是每当必须向动物添加新动作时,我们就必须修改动物。
// 而现在我们不必更改它们。
// 例如,假设我们被要求将跳跃行为添加到动物中,我们可以简单地通过创建一个新的访客来添加它。
const jump = {
visitMonkey(monkey) {
console.log("Jumped 20 feet high! on to the tree!");
},
visitLion(lion) {
console.log("Jumped 7 feet! Back on the ground!");
},
visitDolphin(dolphin) {
console.log("Walked on water a little and disappeared");
}
}
;(function(){
const monkey = new Monkey();
const lion = new Lion();
const dolphin = new Dolphin();
monkey.accept(speak); // Ooh oo aa aa!
monkey.accept(jump); // Jumped 20 feet high! on to the tree!
lion.accept(speak); // Roaaar!
lion.accept(jump); // Jumped 7 feet! Back on the ground!
dolphin.accept(speak); // Tuut tutt tuutt!
dolphin.accept(jump); // Walked on water a little and disappeared
})();
https://github.com/WindrunnerMax/EveryDay
https://www.runoob.com/design-pattern/visitor-pattern.html
https://github.com/sohamkamani/javascript-design-patterns-for-humans#-visitor
https://www.bookstack.cn/read/design-pattern-in-javascript/design-pattern-visitor-pattern-README.md
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。