473,385 Members | 1,359 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,385 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 3916
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: 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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.