The following two options come to mind for implementing the business rules check:
1. Place the business rules checks in the constructor of this class and throw an exception if the business rules aren't met.
Expand|Select|Wrap|Line Numbers
- A a = new (A); // can throw an exception
- class A{
- public:
- A::A()
- {
- if( business rules aren't met)
- throw;
- ...
- }
- }
Expand|Select|Wrap|Line Numbers
- a = A::create(); // can throw an exception
- class A {
- private:
- A::A()
- {
- }
- public:
- static A::create()
- {
- if( business rules pass ) {
- a = new(A)
- return a;
- }
- throw exception
- }
However option (1) seems standard fare and has the advantage that anyone looking at the code understands what's happening. When the business rules fail a corresponding dealloc and exception is thrown.
Is there a recommended best practice?
Are there creation patterns that can help answer this question?
Thank you