Implementing visitor mode in C + +

1. Situation and intention

  we store a lot of food in the freezer. For mothers, opening the freezer is mainly to cook with vegetables and meat, and for children, it is mainly to take ice cream and drinks.
  in daily development, there must be some classes related to data structures, such as the model for designing data storage. However, there are many kinds of data in this class. For different visitors, we should present different data, not all data, that is, isolation.
   in our daily development, how to realize this feature isolation for different visitors—— Visitor mode.

2. Visitor mode

   represents an operation that acts on each element in an object structure, so that a new operation can be defined without changing each element.
   the operation here is not the behavior of the object of this class, but refers to access. The access behavior of relevant element objects is concentrated in one visitor object rather than scattered in one element class. The responsibilities of the class are clearer and comply with the principle of single responsibility.
  let's take a look at it with code.

3. Freezer

  first of all, the premise of visitor mode is that the class structure is fixed. If it is not fixed, you need to change the visitor's class again. For example, the accounts of a company represented by a class have provided income and expenses. This is fixed. If there are tax operations later, this class must be changed, which will lead to the subsequent visitor class to be changed.
  let's define the freezer first.

class DPVisitor;
class DPFreezer {
public:
	virtual void accept(DPVisitor& visitor) = 0;
};
/*
	The premise of visitor mode is that the structure of the class is determined, that is, the objects in the class are determined, for example, the level of the refrigerator is determined
	Just look at the visitors behind.
*/
class DPFreshkeeping : public DPFreezer {
public:
	virtual void accept(DPVisitor& visitor);
};

class DPRefrigeration : public DPFreezer {
public:
	virtual void accept(DPVisitor& visitor);
};

  then we define visitors.

class DPFreezer;
class DPVisitor{
public:
	virtual void visitorFreshkeeping(DPFreezer& freezer) = 0;
	virtual void visitorRefrigeration(DPFreezer& freezer) = 0;
	// For example, the refrigerator class has added a soft frozen layer, so the visitor class here should also be changed
};

class DPVisitorMom : public DPVisitor {
public:
	virtual void visitorFreshkeeping(DPFreezer& freezer);
	virtual void visitorRefrigeration(DPFreezer& freezer);
};


class DPVisitorKids : public DPVisitor {
public:
	virtual void visitorFreshkeeping(DPFreezer& freezer);
	virtual void visitorRefrigeration(DPFreezer& freezer);
};

Let's look at the implementation of the core.

void DPVisitorMom::visitorFreshkeeping(DPFreezer& freezer) {
	// Add action
	printf("Mother visited the fresh-keeping layer and showed the vegetables\n");
}

void DPVisitorMom::visitorRefrigeration(DPFreezer& freezer) {
	// Add action
	printf("Mother visited the cold storage to show meat and dumplings\n");
}


void DPVisitorKids::visitorFreshkeeping(DPFreezer& freezer) {
	// Add action
	printf("The children visited the fresh-keeping layer and showed milk and juice\n");
}

void DPVisitorKids::visitorRefrigeration(DPFreezer& freezer) {
	// Add action
	printf("The child visited the cold storage and showed the ice cream\n");
}
// If you want to continue the add operation
// Then create a visitor

Just look at the call

int main() {

	DPFreezer* freshkeeping = new DPFreshkeeping();
	DPFreezer* refrigeration = new DPRefrigeration();
	DPVisitor* mom = new DPVisitorMom();
	DPVisitor* kid = new DPVisitorKids();

	freshkeeping->accept(*mom);
	freshkeeping->accept(*kid);
	refrigeration->accept(*mom);
	refrigeration->accept(*kid);

	return 0;
}

4. Summary

   well, it's time to summarize again. Students who haven't read the previous command mode are welcome to read: [command mode].
  these two models belong to the "behavior change" model, which mainly solves the problem that the change of the behavior of the object itself will lead to the change of the object itself. Simply put, adding a new behavior will change the original. For example, in the previous command mode blog, adding a new command will change the command() method if the command mode is not used. These two models are to cope with this change, inhibit the expansion of change and maintain stability.

Keywords: C++ Design Pattern

Added by freedomsam on Sun, 30 Jan 2022 05:24:53 +0200