M
Mike
Hello NG,
i am just learning various Design Patterns and now i am not sure, if
this design is correct (Builder) or if i should use an other pattern.
I have various classes (here ChildA and ChildB) derived from class Base.
Now i want to create an object, but i don't want to know which class to
instantiate.
All classes have "pseudo"-methods/codes to keep it very simple.
------------------ cut here ------------------
class Base
{
public:
virtual ~Base() {}; // hack!
virtual start() = 0;
virtual stop() = 0;
// with empty methods only for default implementation
void create (const char* last, const char* first) {} ;
void create (const char* last) {} ;
};
// --------------------------------------------
class ChildA : public Base
{
public:
ChildA();
void start();
void stop();
// ok, child_a implements this create method
void create(const char* name)
{ /* do something */}
};
// --------------------------------------------
class ChildB : public Base
{
public:
ChildB();
void start();
void stop();
// ok, child_a implements the other create method
void create (const char* name1, const char* name2)
{ /* do something else */ }
};
// --------------------------------------------
class Builder
{
public:
Base* createObject(int type)
{
if (type = 1)
return new ChildA;
if (type = 2)
return new ChildB;
// exception handling
...
...
}
} ;
// --------------------------------------------
int main()
{
Base base = Builder::createObject(2); // ChildB
base->create("Doe", "Johnny");
base->start();
/* do the rest */
...
...
}
------------------ cut here ------------------
Thanks a lot for your answers
Mike
i am just learning various Design Patterns and now i am not sure, if
this design is correct (Builder) or if i should use an other pattern.
I have various classes (here ChildA and ChildB) derived from class Base.
Now i want to create an object, but i don't want to know which class to
instantiate.
All classes have "pseudo"-methods/codes to keep it very simple.
------------------ cut here ------------------
class Base
{
public:
virtual ~Base() {}; // hack!
virtual start() = 0;
virtual stop() = 0;
// with empty methods only for default implementation
void create (const char* last, const char* first) {} ;
void create (const char* last) {} ;
};
// --------------------------------------------
class ChildA : public Base
{
public:
ChildA();
void start();
void stop();
// ok, child_a implements this create method
void create(const char* name)
{ /* do something */}
};
// --------------------------------------------
class ChildB : public Base
{
public:
ChildB();
void start();
void stop();
// ok, child_a implements the other create method
void create (const char* name1, const char* name2)
{ /* do something else */ }
};
// --------------------------------------------
class Builder
{
public:
Base* createObject(int type)
{
if (type = 1)
return new ChildA;
if (type = 2)
return new ChildB;
// exception handling
...
...
}
} ;
// --------------------------------------------
int main()
{
Base base = Builder::createObject(2); // ChildB
base->create("Doe", "Johnny");
base->start();
/* do the rest */
...
...
}
------------------ cut here ------------------
Thanks a lot for your answers
Mike