仅当对象处于特定状态时,操作才有效
Operations are valid only if object is in particular state
我的应用程序中有很多类型的对象。所有状态都可以具有以下状态:UpToDate,OutOfDate,NotComputed。仅当对象的状态为 UpToDate 时,才允许对象的所有操作。如果对象的状态为 NotComputed 或 OutOfDate,则唯一允许的操作是 Compute 和 ChangeComputingParams。在 gui 中,我想仅在允许的情况下显示对对象的操作。所以我有操作验证器对象,对于返回对象上的操作是否应显示在 gui 上的每个对象。问题是,每次我向某个对象添加新操作时,我都需要转到它的 OperationValidator 类,并在那里添加新函数。这一定是更好的方法。例:
class Object1OperationValidator
{
Object1OperationValidator(Object1& object1)
{
mObject1 = object1;
}
bool CanDoCompute()
{
return true;
}
bool CanDoChangeComputationParams()
{
return true;
}
bool CanDoOperation1()
{
if(mObject1.State() != UpToDate )
return false;
else
return true;
}
.....
bool CanDoOperationN()
{
if(mObject1.State() != UpToDate )
return false;
else
return true;
}
}
您可以将模板化类与验证函数的实现一起使用:
template <class T>
void CheckBaseOperations
{
public:
CheckBaseOperations(T * obj)
:inst(obj)
{}
bool CheckOperation()
{
//...
return inst->State() != X;
}
public:
T * inst;
};
//----------------------------------------------------------------------
//for generic types
template <class T>
void CheckOperations : public CheckBaseOperations<T>
{
public:
CheckOperations(T * obj)
:CheckBaseOperations<T>(obj)
{}
};
//----------------------------------------------------------------------
template <> //specific validation for a certain type.
void CheckOperations <YourType> : public CheckBaseOperations<YourType>
{
public:
CheckOperations(YourType * obj)
:CheckBaseOperations<YourType>(obj)
{}
bool CheckOperationForYourType()
{
//...
return inst->State() != X;
}
};
如果需要,您还可以拥有专门针对特定类型的CheckOperations
相关文章:
- 为什么在popback()操作之后,它仍然打印完整的矢量
- 欧拉项目#8答案是大以获得有效答案
- 重载操作程序时出错>>用于类中的字符串 memebr
- 调整大小后指向元素值的指针unordered_map有效?
- 为什么是0;C++中的有效语句
- 最高有效数字侧的第N位
- 如何有效地操作满足给定谓词的向量中的所有项目?
- 如何有效地使用 std::async 对指针数组执行操作
- 狂想曲C++状态图 - 事件(似乎有效)与触发操作(似乎什么都不做)
- LoadLibrary失败带有错误4250:此操作仅在应用程序容器的上下文中有效
- 需要帮助了解C 中有效的C弦操作
- 是否可以仅使用位操作有效地完成图像屏蔽
- 仅当对象处于特定状态时,操作才有效
- 将引用分配给引用是一个有效的操作
- Q操作快捷方式并不总是有效的
- 是否存在通过引用选择元素和通过指针操作选择元素都有效的情况
- 为什么检查分配操作的结果有效,但不能与其他条件相结合
- 在读/写操作中使用c++ 11 unordered_map的最有效方法
- 在内存中操作std::string的二维动态数组的最有效方法是什么?
- 需要在c++中有效操作内存的建议