Re: Design issue with constructor arguments




Caroline M wrote:
Mark Nicholls wrote:

So the problem is that class A cannot see class X, so it has to
recursively delegate to a class that can see it?

Yes thats right.

OK, there may be nothing wrong, it may be a symptom of your (correct)
partitioning of the software to minimize coupling

Yeah, pretty much. I started out with only 3 classes but as the system
grew I gradually partioned the work out a little into smaller classes
that each did one thing.

It feels slightly wrong only because you say that C & D have no real
interest in X except to pass this parameter in, so why can't A see X
directly?

It may help if I explained what these classes did. I'm developing a
messaging application that sends and receives xml-based messages over
TCP. The front end class is a server and the processor class is the
message processor that knows what to do with the messages. Initially, I
had just the server and the messager processor and all was good.

OK


Then once I learned a bit more about the requirements I realised that
my server had to manage multiple connections for a device so I created
a class for the device. Then each device has to manage individual
conversations on several ports and any message passing must be
carefully synchronised so I created another class to manage each port
and this is how the design grew. Down the bottom I have a connection
class which manages a single socket connection on a single port. It has
queues of messages which it needs to feed off to this message
processor. The reason the processor is created by the server is that
the server also provides the client interface for allowing those
callbacks to be registered.

OK


(it may be that you've done everything sensibly, and it's just a
reflection of you minmising coupling).

Well it was more a case of managing collections. The server has a
device manager which manages several devices and each device manages
several connections. There's probably not as many classes in between as
I first thought but still, it's something I come across quite a lot.

The other solution that springs to mind is to make the processor a
singleton, but this server code is multithreaded, and that just makes
me twitchy!

I agree....multi threading is inherently evil....(I've spent a few
hours removing it from peoples apps)....if you need it, you need it,
sensibly you don't want any interaction between threads if possible.


OK so......(in psuedo C#...if that makes sense.)

// device inteface
interface IDevice
{
....
}

// manages list of devices
class Server
{
IList<Device> devices.....;
}

// device A
class DeviceA : IDevice
{
IList<Connection> connections....;
}

// device B
class DeviceB : IDevice
{
IList<Port> ports....;
}


// manage a connection
class Port
{
Connection connection....;

}

// connection class
class Connection
{
}

roughly correct?

There are two extreme ways of partitioning this......in layers......in
columns....and the constructors parameters are dependent on which way
you do it...neither way is 'better'.....just different....and there's
different ways of doing each.

Ummmmmm....I don't see where the callbacks come in....but that doesn't
really matter....

so you're going.......the equivalent of....?

class A
{
public A(a1param,a2param) {...}
}

class B
{
A a;

public B(b1param,b2param,a1param,a2param)
{
a = new A(a1param,a2param);
....
}
}

class C
{
B b;

public C(c1param,c2param,b1param,b2param,a1param,a2param)
{
b = new B(b1param,b2param,a1param,a2param);
....
}
}


but why not go.....

class A
{
public A(a1param,a2param) {...}
}

class B
{
A a;

public B(b1param,b2param,A a)
{
this.a = a;
....
}
}

class C
{
B b;

public C(c1param,c2param,B b)
{
this.b = b;
....
}
}

now OK, whatever instantiates C now has to know how to instantiate A's,
and B's....so rather than;

C c = new C(c1param,c2param,b1param,b2param,a1param,a2param);

we need to go

A a = new A(a1param,a2param);
B b = new B(b1param,b2param,a);
C c = new C(c1param,c2param,b);

more complex, but C and B, are ignorant of the parameters used to
create A.
C no longer has to reference the concrete class B...only it's
interface.
B no longer has to reference the concrete class A, only it's interface.

something else does.....yes...you cannot eliminate the complexity, only
move it.

The dependency tree for the classes is now much flatter....whatever
builds the A,B's and C's is dependent on seeing the concrete
classes.....all other classes need only reference the interfaces to the
classes in the 'lower' layer.....(someone will say DIP now and I'll
scream...thats another story.....the key is you are moving dependencies
(not inverting them)).

If I've got the wrong end of the stick then ignore me.

.



Relevant Pages

  • Re: Problem configuring NAT to share Internet Connection
    ... One of my NICs in the server connect to a DSL ... modem and it connects to internet. ... > interface, that connects to the DSL modem, LAN interface, that connects to ... >> 7.- To connect server to Internet, I create a new network connection. ...
    (microsoft.public.win2000.ras_routing)
  • Re: Cannot get NAT to route in RRAS
    ... ADSL Link was set as the Public interface in NAT, ... The static route also adds in fine using the ADSL Link interface, ... separate DNS server handles client’s requests, ... > Internet connection. ...
    (microsoft.public.win2000.ras_routing)
  • Re: What signal tells my app that my DHCP lease just renewed?
    ... A client application that has a long-established connection. ... The redirection server would maintain a TCP connection ... A server application that is bound to a specific interface. ... In the case of network reconfiguration (such as an interface being ...
    (comp.os.linux.networking)
  • Re: VPN Disconnects
    ... Microsoft Windows 2000 Advanced Server ... A demand-dial Point-to-Point Tunneling Protocol (PPTP) connection between ... does not match the remote server's Demand-Dial interface. ...
    (microsoft.public.isa.vpn)
  • Re: Outgoing POP3 email missing/lost/not received
    ... Funny thing is that I have had this ISP for 8 years and it has always been ... It looks like when you last ran CEICW, you set the ISP's mail server to: ... Internet Connection Wizard. ... After the wizard completes, the following network connection ...
    (microsoft.public.windows.server.sbs)