Re: Improving ugly(?) design: Data validation
On 21 Jun, 21:51, Rune Allnor <all...@tele.ntnu.no> wrote:
On 17 Jun, 22:14, Rune Allnor <all...@tele.ntnu.no> wrote:
Hi all.
I have this class to validate a string according to a
specification contained in a string.
I have come up with a design for my validation, which
combines a few of the ideas that have been presented
in this thread with some of my own ideas (code below,
in case somebody are interested).
Bah! Lots of bad stuff going on in the code I posted yesterday.
Below is a cleaned-up version. So much for coding on Saturday
afternoon...
Rune
/////////////////////////////////////////////////////////
#include <iostream>
#include <memory>
class EbClass; // Error base class
class E1Class; // Error type 1 class
class E2Class; // Error type 2 class
class HandlerClass{
public:
virtual bool handleReport(std::auto_ptr<EbClass>);
virtual void handleReport(E1Class*);
virtual void handleReport(E2Class*);
};
class EbClass{
public:
virtual void setHandler(HandlerClass*) = 0;
};
class E1Class : public EbClass {
public:
virtual void setHandler(HandlerClass*);
};
class E2Class : public EbClass {
public:
virtual void setHandler(HandlerClass*);
};
class ValidatorClass{
private:
HandlerClass* handler_;
std::auto_ptr<EbClass> error1(int) const;
std::auto_ptr<EbClass> error2(int) const;
public:
ValidatorClass(HandlerClass* h):handler_(h){};
bool operator()(const int) const;
};
std::auto_ptr<EbClass> ValidatorClass::error1(int i) const
{
// Check #1 fails is i == 1
if (i==1) return std::auto_ptr<EbClass>(new E1Class());
return std::auto_ptr<EbClass>();
}
std::auto_ptr<EbClass> ValidatorClass::error2(int i) const
{
// Check #2 fails is i == 2
if (i==2) return std::auto_ptr<EbClass>(new E2Class());
return std::auto_ptr<EbClass>();
}
bool ValidatorClass::operator()(int i) const
{
if (!handler_->handleReport(error1(i))) return false;
if (!handler_->handleReport(error2(i))) return false;
return true;
}
bool HandlerClass::handleReport(std::auto_ptr<EbClass> e)
{
if (e.get()) // A valid pointer indicates error
{
e->setHandler(this);
return false;
}
return true;
}
void HandlerClass::handleReport(E1Class* e)
{
// Insert report handling code,
// like issuing error messages here
std::cerr << "Error 1 detected: ";
}
void HandlerClass::handleReport(E2Class* e)
{
// Insert report handling code,
// like issuing error messages here
std::cerr << "Error 2 detected: ";
}
void E1Class::setHandler(HandlerClass* h)
{
h->handleReport(this);
}
void E2Class::setHandler(HandlerClass* h)
{
h->handleReport(this);
}
int main()
{
HandlerClass h;
ValidatorClass v(&h);
for (int i=0;i<6;++i)
{
if (v(i))
{std::cout<< i << " is OK" << std::endl; }
else
{std::cout<< i << " is invalid" << std::endl; }
}
return(0);
}
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]