From a3194941288b93fdad77d6504be8049fb4678ce0 Mon Sep 17 00:00:00 2001 From: IvisTang Date: Wed, 7 Jan 2026 20:24:46 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E8=AE=BF=E9=97=AE=E8=80=85?= =?UTF-8?q?=E6=A8=A1=E5=BC=8F=E7=A4=BA=E4=BE=8B=EF=BC=8C=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E5=BD=A2=E7=8A=B6=E3=80=81=E8=AE=BF=E9=97=AE=E8=80=85=E5=8F=8A?= =?UTF-8?q?=E5=85=B7=E4=BD=93=E5=AE=9E=E7=8E=B0=EF=BC=8C=E5=A2=9E=E5=BC=BA?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=E5=8F=AF=E8=AF=BB=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go/visitor/main.go | 80 ++++++++++++++++++++++++++++++++++++++++ python/visitor/main.py | 81 +++++++++++++++++++++++++++++++++++++++++ ts/src/visitor/index.ts | 67 ++++++++++++++++++++++++++++++++++ 3 files changed, 228 insertions(+) create mode 100644 go/visitor/main.go create mode 100644 python/visitor/main.py create mode 100644 ts/src/visitor/index.ts diff --git a/go/visitor/main.go b/go/visitor/main.go new file mode 100644 index 0000000..08986d5 --- /dev/null +++ b/go/visitor/main.go @@ -0,0 +1,80 @@ +package main + +import "fmt" + +/* +实例: +汤宝宝坚信搭积木是人类的未来,因此开办了一家积木厂。 +*/ + +// 抽象Node接口 Shape +type Shape interface { + Accept(visitor Visitor) + ClaimShape() +} + +type Circle struct{} + +func (c *Circle) Accept(visitor Visitor) { + visitor.VisitCircle(c) +} + +func (c *Circle) ClaimShape() { + fmt.Println("I am a Circle") +} + +type Square struct{} + +func (s *Square) Accept(visitor Visitor) { + visitor.VisitSquare(s) +} + +func (s *Square) ClaimShape() { + fmt.Println("I am a Square") +} + +type Triangle struct{} + +func (t *Triangle) Accept(visitor Visitor) { + visitor.VisitTriangle(t) +} + +func (t *Triangle) ClaimShape() { + fmt.Println("I am a Triangle") +} + +// 抽象NodeVisitor接口 Visitor +type Visitor interface { + VisitCircle(c *Circle) + VisitSquare(s *Square) + VisitTriangle(t *Triangle) +} + +type ShapeVisitor struct{} + +func (sv *ShapeVisitor) VisitCircle(c *Circle) { + fmt.Print("Visiting Circle: ") + c.ClaimShape() +} + +func (sv *ShapeVisitor) VisitSquare(s *Square) { + fmt.Print("Visiting Square: ") + s.ClaimShape() +} + +func (sv *ShapeVisitor) VisitTriangle(t *Triangle) { + fmt.Print("Visiting Triangle: ") + t.ClaimShape() +} + +func main() { + fmt.Println("========== 访问者模式 ==========") + shapes := []Shape{&Circle{}, &Square{}, &Triangle{}} + visitor := &ShapeVisitor{} + + for _, shape := range shapes { + shape.Accept(visitor) + } + fmt.Println() + fmt.Println("======== 访问者模式结束 ========") +} diff --git a/python/visitor/main.py b/python/visitor/main.py new file mode 100644 index 0000000..2203819 --- /dev/null +++ b/python/visitor/main.py @@ -0,0 +1,81 @@ +""" +实例: +汤宝宝坚信搭积木是人类的未来,因此开办了一家积木厂。 +""" + +from abc import ABC, abstractmethod + + +# 抽象Node接口 Shape +class Shape(ABC): + @abstractmethod + def accept(self, visitor: "Visitor"): + pass + + @abstractmethod + def claim_shape(self): + pass + + +class Circle(Shape): + def accept(self, visitor): + visitor.visit_circle(self) + + def claim_shape(self): + print("I am a Circle") + + +class Square(Shape): + def accept(self, visitor): + visitor.visit_square(self) + + def claim_shape(self): + print("I am a Square") + + +class Triangle(Shape): + def accept(self, visitor): + visitor.visit_triangle(self) + + def claim_shape(self): + print("I am a Triangle") + + +# 抽象Visitor接口 +class Visitor(ABC): + @abstractmethod + def visit_circle(self, circle: Circle): + pass + + @abstractmethod + def visit_square(self, square: Square): + pass + + @abstractmethod + def visit_triangle(self, triangle: Triangle): + pass + + +# 具体Visitor实现 +class ShapeVisitor(Visitor): + def visit_circle(self, circle: Circle): + print("Visiting Circle:", end=" ") + circle.claim_shape() + + def visit_square(self, square: Square): + print("Visiting Square:", end=" ") + square.claim_shape() + + def visit_triangle(self, triangle: Triangle): + print("Visiting Triangle:", end=" ") + triangle.claim_shape() + + +if __name__ == "__main__": + print("========== 访问者模式 ==========") + shapes = [Circle(), Square(), Triangle()] + visitor = ShapeVisitor() + for shape in shapes: + shape.accept(visitor) + + print("\n======== 访问者模式结束 ========") diff --git a/ts/src/visitor/index.ts b/ts/src/visitor/index.ts new file mode 100644 index 0000000..3783abb --- /dev/null +++ b/ts/src/visitor/index.ts @@ -0,0 +1,67 @@ +/* +实例: +汤宝宝坚信搭积木是人类的未来,因此开办了一家积木厂。 +*/ + +// 抽象Node接口 Shape +interface Shape { + accept(visitor: Visitor): void; + claimShape(): string; +} + +class Circle implements Shape { + accept(visitor: Visitor): void { + visitor.visitCircle(this); + } + claimShape(): string { + return "I am a Circle"; + } +} + +class Square implements Shape { + accept(visitor: Visitor): void { + visitor.visitSquare(this); + } + claimShape(): string { + return "I am a Square"; + } +} + +class Triangle implements Shape { + accept(visitor: Visitor): void { + visitor.visitTriangle(this); + } + claimShape(): string { + return "I am a Triangle"; + } +} + +// 抽象Visitor接口 +interface Visitor { + visitCircle(circle: Circle): void; + visitSquare(square: Square): void; + visitTriangle(triangle: Triangle): void; +} + +class ShapeVisitor implements Visitor { + visitCircle(circle: Circle): void { + console.log("Visiting Circle: ", circle.claimShape()); + } + visitSquare(square: Square): void { + console.log("Visiting Square: ", square.claimShape()); + } + visitTriangle(triangle: Triangle): void { + console.log("Visiting Triangle: ", triangle.claimShape()); + } +} + +(function () { + console.log("========== 访问者模式 =========="); + const shapes: Shape[] = [new Circle(), new Square(), new Triangle()]; + const visitor: Visitor = new ShapeVisitor(); + + shapes.forEach((shape) => { + shape.accept(visitor); + }); + console.log("\n======== 访问者模式结束 ========"); +})();