实现访问者模式示例,添加形状、访问者及具体实现,增强代码可读性
This commit is contained in:
parent
e5be1b62c2
commit
a319494128
|
|
@ -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("======== 访问者模式结束 ========")
|
||||
}
|
||||
|
|
@ -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======== 访问者模式结束 ========")
|
||||
|
|
@ -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======== 访问者模式结束 ========");
|
||||
})();
|
||||
Loading…
Reference in New Issue