473,394 Members | 1,761 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,394 software developers and data experts.

OO design advice - virtual constant?

I have a class hierarchy representing data importers, each of which
reads lines from a particular type of comma-separated data file and
creates corresponding entries in a database. There is an abstract base
class, Importer, which contains the file-reading logic, and a series
of inherited classes - CarrierImporter, RouteImporter, etc. - that
validate and process specific file types.

To help identify valid files, each type of import file has a distinct
line prefix (for instance, "CARRI" for carriers, and "LRTIN" for
routes). This is obviously constant for any one class, but it needs to
be virtual because (i) it is different for each class and (ii) it is
used polymorphically in the Importer base class. Since 'const' and
'readonly' fields cannot be virtual, I have used a read-only property:

public virtual string LinePrefix { get; }
.... and then ...
public override string LinePrefix { get { return "CARRI"; } } // ...
etc.

The read-only property - when I really want a "virtual constant" -
seems like a bit of a hack. Additionally, I would like to make
LinePrefix static, so that I can access it from outside without having
to create an instance. However, static fields can't be virtual either!

Is there a better pattern for this kind of thing, where a virtual
field is constant for any single class but varies between classes?

P.
Nov 16 '05 #1
5 3917
Paul,

Yes, there is. Define an attribute and then attach it to the specific
type. The attribute would be something like "LinePrefixAttribute" or
something like that, and the constructor would take the prefix. Then, you
would have a read only property of type string which would return the
prefix.

This would allow you to get the attribute, and then the prefix, without
having to create an instance of the type, and you would be able to set the
prefix for each specific type.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Paul E Collins" <fi******************@CL4.org> wrote in message
news:cm**********@titan.btinternet.com...
I have a class hierarchy representing data importers, each of which reads
lines from a particular type of comma-separated data file and creates
corresponding entries in a database. There is an abstract base class,
Importer, which contains the file-reading logic, and a series of inherited
classes - CarrierImporter, RouteImporter, etc. - that validate and process
specific file types.

To help identify valid files, each type of import file has a distinct line
prefix (for instance, "CARRI" for carriers, and "LRTIN" for routes). This
is obviously constant for any one class, but it needs to be virtual
because (i) it is different for each class and (ii) it is used
polymorphically in the Importer base class. Since 'const' and 'readonly'
fields cannot be virtual, I have used a read-only property:

public virtual string LinePrefix { get; }
... and then ...
public override string LinePrefix { get { return "CARRI"; } } // ... etc.

The read-only property - when I really want a "virtual constant" - seems
like a bit of a hack. Additionally, I would like to make LinePrefix
static, so that I can access it from outside without having to create an
instance. However, static fields can't be virtual either!

Is there a better pattern for this kind of thing, where a virtual field is
constant for any single class but varies between classes?

P.

Nov 16 '05 #2
There are two ways that I might handle this.

1) You could give the subclasses complete freedom by having a non-static
method called CanHandleContent(Stream s) which returns a boolean. This
method would do whatever it needs to determine if this is appropriate
content. If it returns true, then you can pass this to the method that
processes the content.
2) Have each Importer "registered", meaning that the class type is
maintained in some collection like a Hashtable. Once the classes are
registered, then you can have a method called GetImporter(Stream s) which
will return an instance based on the passed content. This is similar to how
the WebRequest class works. You will notice how WebRequest has a method for
registering a prefix and creating an object instance. You could even
combine item 1 with this to create a truly flexible system.


"Paul E Collins" <fi******************@CL4.org> wrote in message
news:cm**********@titan.btinternet.com...
I have a class hierarchy representing data importers, each of which
reads lines from a particular type of comma-separated data file and
creates corresponding entries in a database. There is an abstract base
class, Importer, which contains the file-reading logic, and a series
of inherited classes - CarrierImporter, RouteImporter, etc. - that
validate and process specific file types.

To help identify valid files, each type of import file has a distinct
line prefix (for instance, "CARRI" for carriers, and "LRTIN" for
routes). This is obviously constant for any one class, but it needs to
be virtual because (i) it is different for each class and (ii) it is
used polymorphically in the Importer base class. Since 'const' and
'readonly' fields cannot be virtual, I have used a read-only property:

public virtual string LinePrefix { get; }
... and then ...
public override string LinePrefix { get { return "CARRI"; } } // ...
etc.

The read-only property - when I really want a "virtual constant" -
seems like a bit of a hack. Additionally, I would like to make
LinePrefix static, so that I can access it from outside without having
to create an instance. However, static fields can't be virtual either!

Is there a better pattern for this kind of thing, where a virtual
field is constant for any single class but varies between classes?

P.

Nov 16 '05 #3
Lot simpler then my suggestion.

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:Or**************@TK2MSFTNGP12.phx.gbl...
Paul,

Yes, there is. Define an attribute and then attach it to the specific
type. The attribute would be something like "LinePrefixAttribute" or
something like that, and the constructor would take the prefix. Then, you
would have a read only property of type string which would return the
prefix.

This would allow you to get the attribute, and then the prefix, without having to create an instance of the type, and you would be able to set the
prefix for each specific type.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Paul E Collins" <fi******************@CL4.org> wrote in message
news:cm**********@titan.btinternet.com...
I have a class hierarchy representing data importers, each of which reads
lines from a particular type of comma-separated data file and creates
corresponding entries in a database. There is an abstract base class,
Importer, which contains the file-reading logic, and a series of inheritedclasses - CarrierImporter, RouteImporter, etc. - that validate and processspecific file types.

To help identify valid files, each type of import file has a distinct line prefix (for instance, "CARRI" for carriers, and "LRTIN" for routes). This is obviously constant for any one class, but it needs to be virtual
because (i) it is different for each class and (ii) it is used
polymorphically in the Importer base class. Since 'const' and 'readonly'
fields cannot be virtual, I have used a read-only property:

public virtual string LinePrefix { get; }
... and then ...
public override string LinePrefix { get { return "CARRI"; } } // ... etc.
The read-only property - when I really want a "virtual constant" - seems
like a bit of a hack. Additionally, I would like to make LinePrefix
static, so that I can access it from outside without having to create an
instance. However, static fields can't be virtual either!

Is there a better pattern for this kind of thing, where a virtual field is constant for any single class but varies between classes?

P.


Nov 16 '05 #4
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com>
wrote:
Yes, there is. Define an attribute and then attach it
to the specific type. The attribute would be something
like "LinePrefixAttribute" or something like that, and
the constructor would take the prefix. Then, you would
have a read only property of type string which would
return the prefix.


That sounds like an interesting idea. I'll try it out. Thanks!

P.
Nov 16 '05 #5
I think in this case, attribute is even an overkill. since each specific
derived importer class only recognize one line prefix, just pass that string
literal to the base class constructor from the derived class constructor.

"Nicholas Paldino [.NET/C# MVP]" wrote:
Paul,

Yes, there is. Define an attribute and then attach it to the specific
type. The attribute would be something like "LinePrefixAttribute" or
something like that, and the constructor would take the prefix. Then, you
would have a read only property of type string which would return the
prefix.

This would allow you to get the attribute, and then the prefix, without
having to create an instance of the type, and you would be able to set the
prefix for each specific type.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Paul E Collins" <fi******************@CL4.org> wrote in message
news:cm**********@titan.btinternet.com...
I have a class hierarchy representing data importers, each of which reads
lines from a particular type of comma-separated data file and creates
corresponding entries in a database. There is an abstract base class,
Importer, which contains the file-reading logic, and a series of inherited
classes - CarrierImporter, RouteImporter, etc. - that validate and process
specific file types.

To help identify valid files, each type of import file has a distinct line
prefix (for instance, "CARRI" for carriers, and "LRTIN" for routes). This
is obviously constant for any one class, but it needs to be virtual
because (i) it is different for each class and (ii) it is used
polymorphically in the Importer base class. Since 'const' and 'readonly'
fields cannot be virtual, I have used a read-only property:

public virtual string LinePrefix { get; }
... and then ...
public override string LinePrefix { get { return "CARRI"; } } // ... etc.

The read-only property - when I really want a "virtual constant" - seems
like a bit of a hack. Additionally, I would like to make LinePrefix
static, so that I can access it from outside without having to create an
instance. However, static fields can't be virtual either!

Is there a better pattern for this kind of thing, where a virtual field is
constant for any single class but varies between classes?

P.


Nov 16 '05 #6

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

Similar topics

9
by: Sebastian Faust | last post by:
Hi, I have a design problem about which I am thinking now for a while and still couldnt find any help in deja. What I need is something like a virtual function template. I know that this is not...
5
by: Carl Bevil | last post by:
I'm creating a library for internal use that relies on third-party code. I don't want clients of this library to know anything about the third party code, when compiling or running. Generally...
13
by: Bryan Parkoff | last post by:
I have created three classes according to my own design. First class is called CMain. It is the Top Class. Second class and third class are called CMemory and CMPU. They are the sub-classes....
7
by: bartek | last post by:
Hello, I've been pondering with this for quite some time now, and finally decided to ask here for suggestions. I'm kind of confused, actually... Maybe I'm thinking too much... Brain dump...
4
by: Ian Giblin | last post by:
I am an experienced C programmer, learning C++ by writinging a mathematical toolkit in the framework of a script interpreter. I am posting here to ask for advice (or references) on the object...
31
by: grahamo | last post by:
This came up in an interview I did a while ago and I wanted to know the correct answer. The setup is this; If I have a base class "food" and also two classes "meat" and "veg" that inherit from...
5
by: Tony Johansson | last post by:
Hello!! Assume we have the following a base class called Weapon and three derived classes called MooseRifle, Winchester and Shotgun. These weapons have the followings attributes a name and a...
3
by: fernandez.dan | last post by:
I'm still learning how to use Object Oriented concepts. I'm have a basic question dealing with design. I have two classes that deal with I/O pertaining to network and usb that inherit from an...
3
by: Sorin Dolha | last post by:
Hello, We intend to create an ASP.NET-based Web application (hosted on Internet Information Services, or IIS) and one feature of the application needs to allow the end user to upload photos to...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.