
typedef struct Engine Engine;
typedef struct Wheel Wheel;
typedef struct Car Car;
//Visitor: C has no overloading, so each element
//has its own function in the table
typedef struct {
void (*visitEngine)(const Engine *engine);
void (*visitWheel)(const Wheel *wheel);
void (*visitCar)(const Car *car);
} CarVisitor;
//Element: the base struct is the first field of each element
typedef struct Element {
void (*accept)(const struct Element *self, const CarVisitor *v);
} Element;
//ConcreteElement
struct Engine {
Element base;
};
//the second dispatch: the element picks the function of the visitor
void engineAccept(const Element *self, const CarVisitor *v) {
v->visitEngine((const Engine *)self);
}
//ConcreteElement
struct Wheel {
Element base;
int number;
};
void wheelAccept(const Element *self, const CarVisitor *v) {
v->visitWheel((const Wheel *)self);
}
//ConcreteElement: the car keeps its parts inside, no malloc
struct Car {
Element base;
Engine engine;
Wheel wheels[4];
};
void carAccept(const Element *self, const CarVisitor *v) {
const Car *car = (const Car *)self;
const Element *items[] = {
&car->engine.base,
&car->wheels[0].base, &car->wheels[1].base,
&car->wheels[2].base, &car->wheels[3].base};
for (size_t i = 0; i < sizeof items / sizeof items[0]; i++) {
items[i]->accept(items[i], v);
}
v->visitCar(car);
}
Car carMake(void) {
Car car = {.base = {carAccept}, .engine = {{engineAccept}}};
for (int i = 0; i < 4; i++) {
car.wheels[i] = (Wheel){{wheelAccept}, i + 1};
}
return car;
}
//ConcreteVisitor
//C23: an unused parameter may stay without a name
void testEngine(const Engine *) {
printf("test engine\n");
}
void testWheel(const Wheel *wheel) {
printf("test wheel #%d\n", wheel->number);
}
void testCar(const Car *) {
printf("test car\n");
}
//ConcreteVisitor
void repairEngine(const Engine *) {
printf("repair engine\n");
}
void repairWheel(const Wheel *wheel) {
printf("repair wheel #%d\n", wheel->number);
}
void repairCar(const Car *) {
printf("repair car\n");
}
//Client: a ConcreteVisitor is a table of its own functions
Car car = carMake();
CarVisitor v1 = {testEngine, testWheel, testCar};
CarVisitor v2 = {repairEngine, repairWheel, repairCar};
//the first dispatch: the element's own accept
car.base.accept(&car.base, &v1);
//printed: test engine
// test wheel #1
// test wheel #2
// test wheel #3
// test wheel #4
// test car
car.base.accept(&car.base, &v2);
//printed: repair engine
// repair wheel #1
// repair wheel #2
// repair wheel #3
// repair wheel #4
// repair car
Represent an operation to be performed on the elements of an object structure. Visitor lets you define a new operation without changing the classes of the elements on which it operates.Read more问题。一份文档由段落、图片和表格组成。程序必须能导出文档、统计字数并检查拼写。如果把每个操作都作为方法加到每一种组成部分上,每增加一个操作,这些部分的类就膨胀一次。而且一个操作的代码还会分散在许多类中。 思路。每个操作都变成一个单独的对象,称为访问者。访问者为每一种元素各准备一个方法。每个元素有一个方法,它接收一个访问者,并调用访问者中对应自己种类的方法。这样,每一对元素和操作都会运行正确的代码,无需检查类型。要增加一个新操作,只需编写一个新的访问者;元素类保持不变。 如何阅读示例。元素分为几种。每个具体访问者是一个操作,为每一种元素各有一个单独的方法。客户端代码把访问者交给元素,每个元素再把它送到对应自己种类的方法。在大多数示例中,一个元素包含其他元素,并把访问者交给自己的每个组成部分。随后两个访问者依次经过同样的这些部分,各自执行自己的操作。在某些语言中,元素是带有类型标签的普通数据,访问者是一个函数,为每一种元素各有一个版本。 何时使用。当元素类的集合保持稳定、而针对它们的新操作经常出现时,使用访问者模式。经常增加新的元素种类时不要使用它:每增加一种元素,每个访问者都需要一个新方法。 常见错误。在一个通用的访问者方法内部用条件判断检查元素的类型。这样,新的元素种类会在毫无警告的情况下被遗漏。应为每一种元素提供它自己的方法。 |