While the Abstract Factory claims to create families of products it seems that it isn't usable when the family being created isn't homogenous in its component composition. Buy this it is meant that Abs Factory is good when each component can vary in its implemention but each factory produces a family consisting of the same type of components and each family has same components that vary only in implementation. I wish not to include some components when a component does not apply. I don't want to include component C in Fac4 but I'm forced to do so. How can I have a flexible factory where I'm not forced to include every component? Keep in mind that I would like to use the base in client code to take advantage of polymorphism.
#include <iostream>
using namespace std;
class IA
{
public:
virtual ~IA() {cout << "IA::dtor()" << endl;}
virtual void foo() = 0;
virtual void bar() = 0;
};
class A1 : public IA
{
public:
A1() {cout << "A1::ctor()" << endl;}
virtual ~A1() {cout << "A1::dtor()" << endl;}
virtual void foo() {cout << "A1::foo()" << endl;}
virtual void bar() {cout << "A1::bar()" << endl;}
};
class A2 : public IA
{
public:
A2() {cout << "A2::ctor()" << endl;}
virtual ~A2() {cout << "A2::dtor()" << endl;}
virtual void foo() {cout << "A2::foo()" << endl;}
virtual void bar() {cout << "A2::bar()" << endl;}
};
class IB
{
public:
virtual ~IB() {cout << "IB::dtor()" << endl;}
virtual void foo() = 0;
virtual void bar() = 0;
};
class B1 : public IB
{
public:
B1() {cout << "B1::ctor()" << endl;}
virtual ~B1() {cout << "B1::dtor()" << endl;}
virtual void foo() {cout << "B1::foo()" << endl;}
virtual void bar() {cout << "B1::bar()" << endl;}
};
class B2 : public IB
{
public:
B2() {cout << "B2::ctor()" << endl;}
virtual ~B2() {cout << "B2::dtor()" << endl;}
virtual void foo() {cout << "B2::foo()" << endl;}
virtual void bar() {cout << "B2::bar()" << endl;}
};
class IC
{
public:
virtual ~IC() {cout << "IC::dtor()" << endl;}
virtual void foo() = 0;
virtual void bar() = 0;
};
class C1 : public IC
{
public:
C1() {cout << "C1::ctor()" << endl;}
virtual ~C1() {cout << "C1::dtor()" << endl;}
virtual void foo() {cout << "C1::foo()" << endl;}
virtual void bar() {cout << "C1::bar()" << endl;}
};
class C2 : public IC
{
public:
C2() {cout << "C2::ctor()" << endl;}
virtual ~C2() {cout << "C2::dtor()" << endl;}
virtual void foo() {cout << "C2::foo()" << endl;}
virtual void bar() {cout << "C2::bar()" << endl;}
};
class C3 : public IC
{
public:
C3() {cout << "C3::ctor()" << endl;}
virtual ~C3() {cout << "C3::dtor()" << endl;}
virtual void foo() {cout << "C3::foo()" << endl;}
virtual void bar() {cout << "C3::bar()" << endl;}
};
class IFac
{
public:
virtual ~IFac() {cout << "IFac::dtor()" << endl;}
virtual IA* createA() = 0;
virtual IB* createB() = 0;
virtual IC* createC() = 0;
};
//This product uses only 1st gen components ... OK
class Fac1 : public IFac
{
public:
Fac1() {cout << "Fac1::ctor()" << endl;}
virtual ~Fac1() {cout << "Fac1::dtor()" << endl;}
virtual IA* createA() {return new A1;}
virtual IB* createB() {return new B1;}
virtual IC* createC() {return new C1;}
};
//This product uses only 2nd gen components ... OK
class Fac2 : public IFac
{
public:
Fac2() {cout << "Fac2::ctor()" << endl;}
virtual ~Fac2() {cout << "Fac2::dtor()" << endl;}
virtual IA* createA() {return new A2;}
virtual IB* createB() {return new B2;}
virtual IC* createC() {return new C2;}
};
//This product uses components from 3 generations ... OK
class Fac3 : public IFac
{
public:
Fac3() {cout << "Fac3::ctor()" << endl;}
virtual ~Fac3() {cout << "Fac3::dtor()" << endl;}
virtual IA* createA() {return new A1;}
virtual IB* createB() {return new B2;}
virtual IC* createC() {return new C3;}
};
//This product uses components from 2 generations but not all components apply to product ... HOW?
class Fac4 : public IFac
{
public:
Fac4() {cout << "Fac4::ctor()" << endl;}
virtual ~Fac4() {cout << "Fac4::dtor()" << endl;}
virtual IA* createA() {return new A1;}
virtual IB* createB() {return new B2;}
virtual IC* createC() {return nullptr;} //component does not apply but forced to include!!!
};
int main()
{
IFac* fac = new Fac4;
IA* a = fac->createA();
a->foo();
a->bar();
delete a;
IB* b = fac->createB();
b->foo();
b->bar();
delete b;
//Want to avoid seeing this function because it does not apply.
//Should not be able to see and call createC()
IC* c = fac->createC();
if(c == nullptr)
cout << "C does not apply" << endl;
else
{
c->foo();
c->bar();
delete c;
}
delete fac;
return 0;
}
Aucun commentaire:
Enregistrer un commentaire