Re: Class Design Alternatives
On Dec 21, 3:48 pm, James Kanze <james.ka...@gmail.com> wrote:
On Dec 20, 7:42 pm, sunderjs <sunde...@gmail.com> wrote:
This is from a typical telecom software implementation. I have three
subsystems (x, y, z) which exchange data amongst them. The arrangement
is such that x talks to y over interface xy. y subsystem them talks to
z over yz interface. In a typical scenario, y would receive a set of
parameters from x (over xy). Some of these are meant for z subsys as
well. So y needs to send these plus some more parameters to z.
The implementation options for this kind of arrangement can be :
1. Define separate classes (with access methods for each of the
individual parameters) at each xy and yz interface. Let the common
subsys layer (here y subsys) copy the relevant parameters from x to
that on the interface with z. The problem here is overhead of copy
operation that can be expensive spl. for telecom s/w case.
2. Other option is to define aclassthat has get/set methods for all
parameters (xy + yz) and let each individual subsystemclass(x/y/z)
call the relevant methods. The problem here is z has access to member
functions which are not even relevant to it.
The xy and yz are separate interfaces. That should answer the
question at thedesignlevel. And I don't see where that would
necessitate a copy: the xy interface object could easily contain
a yz interface object, or a pointer to one, and that object can
be passed on to z without copy (providing lifetimes are
sufficient).
--
James Kanze (GABI Software) email:james.ka...@gmai=
l.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Date=
nverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34- H=
ide quoted text -
- Show quoted text -
As mentioned in the problem text, some of the parameters from xy
interface object needs to be passed to yz interface object. One
obvious way is to copy these from xy to yz object. To avoid it one
can
form a superset object containing xy + yz parameters. But then as per
the design, access to this superset object should be restricted to
each of the interfaces. Defining a base class interfaces for xy and
yz
and then deriving a class from these seems a better option.
Something like:
class common
{
public:
virtual void getA() = 0;
virtual void setA() = 0;
};
class xy:public common
{
public:
virtual void getB() = 0;
virtual void setB() = 0;
};
class yz:public common
{
public:
virtual void getC() = 0;
virtual void setC() = 0;
};
//define a superset class derived from xy & yz
//shall contain all data elements
class totalSet:public xy, public yz
{
//contain implementation of the interfaces
};
//subsystem class
class X
{
void doSomething(xy& obj1)
{
obj1.setA();
obj1.setC(); //Error: can't access
}
};
class Z
{
void doSomething(yz& obj2)
{
obj2.getA(); //permissible
obj2.setB(); //Error:can't access
}
};
int main()
{
totalset t1;
Z z;
X x;
x.doSomething(t1); //only xy interface exposed
z.doSomething(t1); //only yz interface exposed
}