mardi 11 août 2015

Class encapsulation: how to prepare code for adding new classes?

I have the following code:

class FooType1 : public FooInterface, public BarInterface1 {}
class FooType2 : public FooInterface, public BarInterface2 {}
class FooType3 : public FooInterface, public BarInterface3 {}

FooInterface service1 = boost::shared_ptr<FooType1>(new FooType1());
FooInterface service2 = boost::shared_ptr<FooType2>(new FooType2());
FooInterface service3 = boost::shared_ptr<FooType3>(new FooType3());

new Host(service1, service2, service3);

Host::Host(boost::shared_ptr<BarInterface1> service1, 
           boost::shared_ptr<BarInterface2> service2,
           boost::shared_ptr<BarInterface3> service3) {
    obj1 = barInterface1;
    obj2 = barInterface2;
    obj3 = barInterface3;
}

I need to add FooType4, FooType5, etc. in a way analogoues to FooType1 through FooType3. In the Host constructor I have to assign the proper service to the proper object (1 to 1, 2 to 2, etc.). Given that I know there will be many new services added, how can I properly encapsulate this?

I thought about Vector<FooInterface>, because in a few places I have to perform some actions on all services so a "for each" loop will be helpful. How I can get the objects from a vector for the Host constructor in a reasonable way? Maybe some design pattern?

Aucun commentaire:

Enregistrer un commentaire