Should a class validate itself or create another class to validate it?

后端 未结 8 869
梦毁少年i
梦毁少年i 2021-02-01 17:30

Let\'s say I have a class like:

class NavigationData
{
  float roll;
  float pitch;
  double latitude;
  double longitude;
}

and if I want to c

8条回答
  •  忘了有多久
    2021-02-01 18:09

    Your class should be designed such a way and provide such methods that validate() is always true:

    • after any public constructor invoked
    • before and after any public method is invoked
    • (in C++-land) before destructor is invoked

    Such validate() methods are called class invariants and are important part of Design by Contract.

提交回复
热议问题