473,326 Members | 2,255 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Object Design

I've a number of objects (classes) defined in an application which I'm using
to model packets on a network. I have a class hierarchy of Packet (inherited
by) EthernetPacket (inherited by) IPPacket (inherited by) TCPPacket.

I've some control logic which takes in a data stream and determines which
kind of packet to create. Should this logic be within the constructors of the
packet classes - so I create a Packet and within the contructor of the packet
it itself determines if it's an ethernet, ip or tcp packet.

At the root of this is can a base class change itself into a subclass or
does the determination of the class type have to be down outside of the class
structure.

What I'm finding now is that the logic that determines if a packet is a TCP
packet is encapsulated within it's base class (the IP packet) and therefore I
need to firstly create an IP packet - call the method of the IP object to
determine if this is a TCP packet and then create a TCP packet (and delete
the IP packet) - is this the way to do it or should I recast the IP packet as
a TCP packet.

Sorry if this is confusing.
tia
Feb 3 '07 #1
4 1160


"Rob" <Ro*@discussions.microsoft.comwrote in message
news:F3**********************************@microsof t.com...
I've a number of objects (classes) defined in an application which I'm
using
to model packets on a network. I have a class hierarchy of Packet
(inherited
by) EthernetPacket (inherited by) IPPacket (inherited by) TCPPacket.

I've some control logic which takes in a data stream and determines which
kind of packet to create. Should this logic be within the constructors of
the
packet classes - so I create a Packet and within the contructor of the
packet
it itself determines if it's an ethernet, ip or tcp packet.

At the root of this is can a base class change itself into a subclass or
does the determination of the class type have to be down outside of the
class
structure.

What I'm finding now is that the logic that determines if a packet is a
TCP
packet is encapsulated within it's base class (the IP packet) and
therefore I
need to firstly create an IP packet - call the method of the IP object to
determine if this is a TCP packet and then create a TCP packet (and delete
the IP packet) - is this the way to do it or should I recast the IP packet
as
a TCP packet.

Sorry if this is confusing.
No this is a common problem. A superclass constructor cannot actually
create a subclass. Just can't. You need to use a factory method, often a
static method on the superclass type that makes a runtime decision about
which subtype to create an and return.

Something like:

public class Packet
{
public static Packet NextPacket(DataStream s)
{
if (whatever)
{
return new IPPacket(s);
}
else
{
return new TCPPacket(s);
}

}
. . ..
}

David

Feb 3 '07 #2
Hi,

You won't be able to change the type from within the type's constructor.

It sounds to me like you need a class factory. Create a class that can
create packets based on the specified stream:

// C# 2.0 example

public static class PacketFactory
{
public static Packet CreatePacket(Stream stream)
{
// TODO: determine type of stream and return appropriate packet
}
}

Start from the least-derived type and work your way towards the most derived
type. If the stream is Tcp then return a TcpPacket. Else, if while
checking for tcp you discovered that the stream was IP then create an
IPPacket. Else, if while checking for ip, you discovered that the stream
was ethernet then create an EthernetPacket.

Just call the PacketFactory.CreatePacket method anytime you need a packet in
code based on a stream.

--
Dave Sexton
http://davesexton.com/blog
http://www.codeplex.com/DocProject (Sandcastle in Visual Studio)

"Rob" <Ro*@discussions.microsoft.comwrote in message
news:F3**********************************@microsof t.com...
I've a number of objects (classes) defined in an application which I'm
using
to model packets on a network. I have a class hierarchy of Packet
(inherited
by) EthernetPacket (inherited by) IPPacket (inherited by) TCPPacket.

I've some control logic which takes in a data stream and determines which
kind of packet to create. Should this logic be within the constructors of
the
packet classes - so I create a Packet and within the contructor of the
packet
it itself determines if it's an ethernet, ip or tcp packet.

At the root of this is can a base class change itself into a subclass or
does the determination of the class type have to be down outside of the
class
structure.

What I'm finding now is that the logic that determines if a packet is a
TCP
packet is encapsulated within it's base class (the IP packet) and
therefore I
need to firstly create an IP packet - call the method of the IP object to
determine if this is a TCP packet and then create a TCP packet (and delete
the IP packet) - is this the way to do it or should I recast the IP packet
as
a TCP packet.

Sorry if this is confusing.
tia

Feb 3 '07 #3
Thanks - I thought it might require this.

"David Browne" wrote:
>

"Rob" <Ro*@discussions.microsoft.comwrote in message
news:F3**********************************@microsof t.com...
I've a number of objects (classes) defined in an application which I'm
using
to model packets on a network. I have a class hierarchy of Packet
(inherited
by) EthernetPacket (inherited by) IPPacket (inherited by) TCPPacket.

I've some control logic which takes in a data stream and determines which
kind of packet to create. Should this logic be within the constructors of
the
packet classes - so I create a Packet and within the contructor of the
packet
it itself determines if it's an ethernet, ip or tcp packet.

At the root of this is can a base class change itself into a subclass or
does the determination of the class type have to be down outside of the
class
structure.

What I'm finding now is that the logic that determines if a packet is a
TCP
packet is encapsulated within it's base class (the IP packet) and
therefore I
need to firstly create an IP packet - call the method of the IP object to
determine if this is a TCP packet and then create a TCP packet (and delete
the IP packet) - is this the way to do it or should I recast the IP packet
as
a TCP packet.

Sorry if this is confusing.

No this is a common problem. A superclass constructor cannot actually
create a subclass. Just can't. You need to use a factory method, often a
static method on the superclass type that makes a runtime decision about
which subtype to create an and return.

Something like:

public class Packet
{
public static Packet NextPacket(DataStream s)
{
if (whatever)
{
return new IPPacket(s);
}
else
{
return new TCPPacket(s);
}

}
. . ..
}

David

Feb 3 '07 #4
Thanks - I thought it might require this kind of processing.

"Dave Sexton" wrote:
Hi,

You won't be able to change the type from within the type's constructor.

It sounds to me like you need a class factory. Create a class that can
create packets based on the specified stream:

// C# 2.0 example

public static class PacketFactory
{
public static Packet CreatePacket(Stream stream)
{
// TODO: determine type of stream and return appropriate packet
}
}

Start from the least-derived type and work your way towards the most derived
type. If the stream is Tcp then return a TcpPacket. Else, if while
checking for tcp you discovered that the stream was IP then create an
IPPacket. Else, if while checking for ip, you discovered that the stream
was ethernet then create an EthernetPacket.

Just call the PacketFactory.CreatePacket method anytime you need a packet in
code based on a stream.

--
Dave Sexton
http://davesexton.com/blog
http://www.codeplex.com/DocProject (Sandcastle in Visual Studio)

"Rob" <Ro*@discussions.microsoft.comwrote in message
news:F3**********************************@microsof t.com...
I've a number of objects (classes) defined in an application which I'm
using
to model packets on a network. I have a class hierarchy of Packet
(inherited
by) EthernetPacket (inherited by) IPPacket (inherited by) TCPPacket.

I've some control logic which takes in a data stream and determines which
kind of packet to create. Should this logic be within the constructors of
the
packet classes - so I create a Packet and within the contructor of the
packet
it itself determines if it's an ethernet, ip or tcp packet.

At the root of this is can a base class change itself into a subclass or
does the determination of the class type have to be down outside of the
class
structure.

What I'm finding now is that the logic that determines if a packet is a
TCP
packet is encapsulated within it's base class (the IP packet) and
therefore I
need to firstly create an IP packet - call the method of the IP object to
determine if this is a TCP packet and then create a TCP packet (and delete
the IP packet) - is this the way to do it or should I recast the IP packet
as
a TCP packet.

Sorry if this is confusing.
tia


Feb 3 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: ggg | last post by:
I'm looking for a complete project/application done with heavy use of of object-oriented programming & design. Preferably something well documented and/or commented so that I can pick it apart...
1
by: Robert Hathaway | last post by:
COMP.OBJECT FAQ Version II Beta now Available http://www.objectfaq.com/oofaq2 ================================================== - Latest Important Information on Object Technology - What's New...
6
by: NewToDotNet | last post by:
I am getting "Object reference not set to an instance of an object. " when I attempt to open a C# windows service class in design view, although I was able to initially create the service and open...
9
by: Keith Rowe | last post by:
Hello, I am trying to reference a Shockwave Flash Object on a vb code behind page in an ASP.NET project and I receive the following error: Guid should contain 32 digits with 4 dashes...
11
by: DrUg13 | last post by:
In java, this seems so easy. You need a new object Object test = new Object() gives me exactly what I want. could someone please help me understand the different ways to do the same thing in...
2
by: Jeff | last post by:
I'm getting an Object Reference error before I even run my app, and I'm not sure where to look to find the cause. I'd appreciate your help. When I open my Windows Application project, the...
4
by: Carl J. Van Arsdall | last post by:
It seems the more I come to learn about Python as a langauge and the way its used I've come across several discussions where people discuss how to do things using an OO model and then how to design...
12
by: Andrew Poulos | last post by:
With the following code I can't understand why this.num keeps incrementing each time I create a new instance of Foo. For each instance I'm expecting this.num to alert as 1 but keeps incrementing. ...
1
by: Allan Ebdrup | last post by:
I get the error: "Cannot create an object of type 'CustomWizard' from its string representation 'CustomWizard1' for the CustomWizard Property." when I view my custom server web control in...
7
by: joproulx | last post by:
Hi, I was wondering if there was a way with Reflection to find dynamically if an object was referencing indirectly another object. A simple example would be: Object1 | --Object2 |
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.