是否有任何现成的模式或库可以为某个类实现 Memento 模式,而不是重新发明轮子?
我有这样的类:
class MyClass{
int myField1;
int myField2;
MyMemento* getMemento(){
auto m = new MyMemento();
m->myField1 = myField1;
m->myField2 = myField2;
}
void restore(MyMemento* m){
myField1 = m->myField1;
myField2 = m->myField2;
}
}
class MyMemento{
public:
int myField1;
int myField2;
}
我有很多像 MyClass 这样的类,每个类都有自己独特的成员变量。但是创建/恢复备忘录的函数的模式是相同的,实际的备忘录对象只包含与每个类匹配的变量。
对于每个类来说,这似乎非常繁琐,所以想知道是否有一些现成的类、库或模式可以用来实现这个?
I have classes like this:
class MyClass{
int myField1;
int myField2;
MyMemento* getMemento(){
auto m = new MyMemento();
m->myField1 = myField1;
m->myField2 = myField2;
}
void restore(MyMemento* m){
myField1 = m->myField1;
myField2 = m->myField2;
}
}
class MyMemento{
public:
int myField1;
int myField2;
}
I have many classes like MyClass, each with their own unique member variables. But the pattern of the functions for creating / restoring the memento is the same, and the actual memento object simply contains variables matching each class.
This seems very tedious to implement for each class, so was wondering if there is some readymade class, library or pattern I can use to implement this instead ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论