473,785 Members | 2,878 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3936
Paul,

Yes, there is. Define an attribute and then attach it to the specific
type. The attribute would be something like "LinePrefixAttr ibute" 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.co m

"Paul E Collins" <fi************ ******@CL4.org> wrote in message
news:cm******** **@titan.btinte rnet.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
correspondin g 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 CanHandleConten t(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(Str eam 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.btinte rnet.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.c om> wrote in
message news:Or******** ******@TK2MSFTN GP12.phx.gbl...
Paul,

Yes, there is. Define an attribute and then attach it to the specific
type. The attribute would be something like "LinePrefixAttr ibute" 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.co m

"Paul E Collins" <fi************ ******@CL4.org> wrote in message
news:cm******** **@titan.btinte rnet.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
correspondin g 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.c om>
wrote:
Yes, there is. Define an attribute and then attach it
to the specific type. The attribute would be something
like "LinePrefixAttr ibute" 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 "LinePrefixAttr ibute" 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.co m

"Paul E Collins" <fi************ ******@CL4.org> wrote in message
news:cm******** **@titan.btinte rnet.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
correspondin g 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
10285
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 possible, so I need "something like" a virtual function template. I read in some threads that there is a workaround using the visitor pattern. I read therefore in the book "Modern c++ Design" about this pattern, but couldnt find any help to my...
5
2217
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 I've been achieving this by having an abstract base class which defines an interface, and inheriting a concrete class which defines the implementation. Clients of the library deal only with the base class and request objects of that type from a...
13
2388
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. Two sub-classes have the relationship to communicate back and forth through this pointer. The pointer is responsible inside Top class for allocating and deallocating two sub-classes. CMemory class is responsible to allocate and deallocate memory...
7
1825
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 follows... I need a class to represent a variable, with an associated data type and/or value. Though, I don't want it to be a variant type, and not a
4
1989
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 design and implimentation. Currently I have a portable "ScriptSession" class which contains the mechanics of looping with a user prompt, parsing a sentence and handling syntax errors, etc., and I wan this to be a class I can use for any script...
31
2000
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 food, thus; food / \ / \ meat veg
5
1305
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 price. So I would be able to ask a weapon object about the name and the price.
3
1820
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 abstract parent with four basic functions: Open, Read, Write, and Close. // Note alot of the stuff has been left out // just to give a basic picture class IO { public:
3
2023
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 the server. However, our customer requests that both of these two objectives to be supported by the design of the solution: 1. The photos should be encoded as JPG, and resized (if larger than a
0
9645
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10329
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10152
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10092
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8974
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7500
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5381
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3650
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.