473,394 Members | 1,722 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.

Which design pattern is good for this?

I need to write a program validate a text file in CSV format. So I will have a

class DataType

and a lot of of derived class for various type, e.g. IntType, StringType,
FloatType, MoneyType, ... etc.

For each column of a type, it may accept null/empty value. or not. It may
have various max length for StringType, IntType,... etc.

And for each column, it may have certain range checking, like some column of
IntType can only between 1 to 25. Some StringType column can only be certain
values.....

Which design patter is best for this? A dictionary with decorate design
pattern? sound too heavy....
Oct 25 '07 #1
8 2334
On Oct 25, 3:03 am, ydbn <y...@discussions.microsoft.comwrote:
I need to write a program validate a text file in CSV format. So I will have a

class DataType

and a lot of of derived class for various type, e.g. IntType, StringType,
FloatType, MoneyType, ... etc.

For each column of a type, it may accept null/empty value. or not. It may
have various max length for StringType, IntType,... etc.

And for each column, it may have certain range checking, like some column of
IntType can only between 1 to 25. Some StringType column can only be certain
values.....

Which design patter is best for this? A dictionary with decorate design
pattern? sound too heavy....
how about the WTF?! design pattern?

seriously, a better pattern is DRY: why implement the aforementioned
classes when you could simply do, say, Int.Parse( text ) for a given
chunk of text inside a try block?

Oct 25 '07 #2
On Oct 25, 6:23 am, namekuseijin <namekusei...@gmail.comwrote:
Which design patter is best for this? A dictionary with decorate design
pattern? sound too heavy....

how about the WTF?! design pattern?

seriously, a better pattern is DRY: why implement the aforementioned
classes when you could simply do, say, Int.Parse( text ) for a given
chunk of text inside a try block?
Because it provides encapsulation of parsing and validation. Instead
of having a giant switch statement (or something similar) the OP can
define the columns, and then just keep calling Parse etc. Sounds
reasonable to me.

Now, as for your suggestion: if you're going to try to parse something
and catch exceptions, the TryParse methods are better than calling
Parse inside a try block.

Jon

Oct 25 '07 #3
On Oct 25, 6:03 am, ydbn <y...@discussions.microsoft.comwrote:
I need to write a program validate a text file in CSV format. So I will have a

class DataType

and a lot of of derived class for various type, e.g. IntType, StringType,
FloatType, MoneyType, ... etc.

For each column of a type, it may accept null/empty value. or not. It may
have various max length for StringType, IntType,... etc.
So for a given file, you'll have a list of column definitions, each
containing a parser and a validator, correct? I'd imagine it *may* be
worth combining the parsing and validation - I wouldn't have thought
there'd be many cases where the validator can be used with lots of
different parsers, for instance.
And for each column, it may have certain range checking, like some column of
IntType can only between 1 to 25. Some StringType column can only be certain
values.....

Which design patter is best for this? A dictionary with decorate design
pattern? sound too heavy....
I can't see how a decorator would fit in here. I'd just define an
appropriate interface, and then (once) create a list of column
definitions for your CSV file, each of which implements the interface.
Then either do the splitting at the "top level" and parse each part,
or allow each parser to "take" however much data they need from the
line, from a given position, returning how much source data they've
used up, and the resulting data. The column definitions themselves
should be immutable, unchanged by the process of parsing an entry -
that way they stay reusable.

Now, what do you need to *do* with the data when you've got it? That
will dictate the design of how the results are stored.

Jon

Oct 25 '07 #4
On 25 out, 05:18, "Jon Skeet [C# MVP]" <sk...@pobox.comwrote:
Because it provides encapsulation of parsing and validation. Instead
of having a giant switch statement (or something similar) the OP can
define the columns, and then just keep calling Parse etc. Sounds
reasonable to me.
a single method/function definition with the "switch statement"
provides just enough encapsulation to the job at hand. Why waste time
implementing several redundant classes?
Now, as for your suggestion: if you're going to try to parse something
and catch exceptions, the TryParse methods are better than calling
Parse inside a try block.
wow, somehow sounds like they do exactly that underneath...

seriously, a better pattern seems to be KISS...

Oct 25 '07 #5
namekuseijin <na**********@gmail.comwrote:
On 25 out, 05:18, "Jon Skeet [C# MVP]" <sk...@pobox.comwrote:
Because it provides encapsulation of parsing and validation. Instead
of having a giant switch statement (or something similar) the OP can
define the columns, and then just keep calling Parse etc. Sounds
reasonable to me.

a single method/function definition with the "switch statement"
provides just enough encapsulation to the job at hand. Why waste time
implementing several redundant classes?
They're not redundant, IMO - they're encapsulating behaviour, and in a
flexible way. Individual objects are then responsible for defining how
a column behaves, in all aspects of parsing and validating.

Without separate objects for each column, where would you put rules for
lengths, optional/mandatory values, potentially minimum/maximum values
for numbers etc? Is that all going to be part of the giant switch
statement too?

I have no problem with having many small classes, each doing a
particular thing well. I far prefer that to having giant methods.
Now, as for your suggestion: if you're going to try to parse something
and catch exceptions, the TryParse methods are better than calling
Parse inside a try block.

wow, somehow sounds like they do exactly that underneath...
No, they don't. They avoid the exception being thrown in the first
place.
seriously, a better pattern seems to be KISS...
You think try/catch/ignore expression is simpler than using a method
which tells you whether or not the value was parsed correctly? I
disagree.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Oct 25 '07 #6
On 25 out, 16:33, Jon Skeet [C# MVP] <sk...@pobox.comwrote:
They're not redundant, IMO - they're encapsulating behaviour, and in a
flexible way. Individual objects are then responsible for defining how
a column behaves, in all aspects of parsing and validating.
my whole point was to point out that "IntType, StringType,
FloatType, MoneyType" are all builtin types already, even with a
handy
Parse method!

that's why it's redundant.
Without separate objects for each column, where would you put rules for
lengths, optional/mandatory values, potentially minimum/maximum values
for numbers etc? Is that all going to be part of the giant switch
statement too?
the giant switch will likely be way shorter than implementing the
useless,
redundant classes for this one-shot problem.

Oct 26 '07 #7
namekuseijin <na**********@gmail.comwrote:
On 25 out, 16:33, Jon Skeet [C# MVP] <sk...@pobox.comwrote:
They're not redundant, IMO - they're encapsulating behaviour, and in a
flexible way. Individual objects are then responsible for defining how
a column behaves, in all aspects of parsing and validating.

my whole point was to point out that "IntType, StringType,
FloatType, MoneyType" are all builtin types already, even with a
handy Parse method!
that's why it's redundant.
None of them contain settings for allowing the name of the column,
nullability, other validation etc.

That's part of what would be contained within the column definition,
and some of that varies by type.
Without separate objects for each column, where would you put rules for
lengths, optional/mandatory values, potentially minimum/maximum values
for numbers etc? Is that all going to be part of the giant switch
statement too?

the giant switch will likely be way shorter than implementing the
useless, redundant classes for this one-shot problem.
There's more to elegant design than counting lines of code.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Oct 26 '07 #8
On Oct 24, 11:03 pm, ydbn <y...@discussions.microsoft.comwrote:
I need to write a program validate a text file in CSV format. So I will have a

class DataType

and a lot of of derived class for various type, e.g. IntType, StringType,
FloatType, MoneyType, ... etc.

For each column of a type, it may accept null/empty value. or not. It may
have various max length for StringType, IntType,... etc.

And for each column, it may have certain range checking, like some column of
IntType can only between 1 to 25. Some StringType column can only be certain
values.....

Which design patter is best for this? A dictionary with decorate design
pattern? sound too heavy....
A friend of mine had me implement a CSV/SSV/XML parser in terms of the
IDataReader interface. It made the project he was working on a breeze.

It also gave him the ability to add column-specific constraints with a
lot more ease. Putting the code in the IDataReader made the project so
small and easy that 3 distinct parsers were done in a day's time.

The DataReader class has a base abstract class that allows you to
specify how the data columns are parsed (this is the only "tricky"
part). The base class provides intuitive conversions from the text
file data to the requested type.

Personally, I treated the derived reader as a business object-like
creature and create Properties for things like Name, Date, Company,
Favorite Ice Cream which would retrieve the correct column and perform
the correct data conversions and do constraint tests. Many people use
IDataReader for their business objects - this is no different.

public DateTime Date
{
get
{
DateTime date = this.GetDate(1); // get date from text file
1st column
// do checks on date
return date;
}
}

I would more than love to send you my class if you are interested.
However it is at work and you will need to wait till Monday or
Tuesday. Just 'Reply to Author'.

Thanks,
Travis

Oct 27 '07 #9

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

Similar topics

4
by: olivier | last post by:
Hi, sorry if it appears twice , i m looking for a good site that explain lot of design pattern, i mean the name, why the pattern exists and if possible good example to explain how to use and why...
8
by: Alex Vinokur | last post by:
Any links to get started with Design Patterns, including samples? Thanks in advance. Alex Vinokur email: alex DOT vinokur AT gmail DOT com http://mathforum.org/library/view/10978.html...
5
by: Coder-X | last post by:
Hi, i have a few questions i would like to ask : 1 - Where can i find good design patterns resources for .NET ? 2 - What's the best design pattern for a windows database application (...
11
by: FluffyCat | last post by:
In Febraury - April of 2002 I put together in Java examples of all 23 of the classic "Gang Of Four" design patterns for my website. Partly I wanted to get a better understanding of those patterns....
4
by: Frazer | last post by:
hi are there any good samples illustrating design patterns in C#? builder, adapter, facade etc. i found a few but the reviews of that article were bad. eg...
13
by: John Salerno | last post by:
Here are a few I'm considering: Design Patterns Explained : A New Perspective on Object-Oriented Design (2nd Edition) (Software Patterns Series) by Alan Shalloway Design Patterns C# by...
4
by: Guch Wu | last post by:
I want to design an image processing class as follow: class Image { Image Data; General Image Operations; read(filename, File_Type); write(filename, File_Type); };
22
by: Krivenok Dmitry | last post by:
Hello All! I am trying to implement my own Design Patterns Library. I have read the following documentation about Observer Pattern: 1) Design Patterns by GoF Classic description of Observer....
19
by: adriancico | last post by:
Hi I am working on a python app, an outliner(a window with a TreeCtrl on the left to select a document, and a RichTextBox at the right to edit the current doc). I am familiarized with OOP...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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?
0
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,...
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...

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.