I have a class that needs to have several different kinds of sorting
routines on an ArrayList that it needs to conditionally do based upon the
data. I have successfully created a class that implements IComparer and
does the sort correctly for one of these sort types. This class's
definition is like:
// Sort class that implements IComparer so
public class CompareFields1 : IComparer
{
public int Compare(object a, object b)
{
// Code that appears in every sort's Compare routine
if (a == b) return 0;
if (a == null) return -1;
if (b == null) return 1;
if (a.GetType() != b.GetType())
{
throw new ArgumentException();
}
// Now that the inputs are valid, do the actual compare
/*... ...actual compare code here... ...*/
}
}
Now I want to make another sort algorithm (CompareFields2). This definition
will be have the same layout as the above one, but the actual compare code
is going to be different. I would like to try and factor out the common
code, but I can't seem to think of a good class design for it since this
isn't in the constructor. I thought of factoring the code into a utility
class, but that requires me to call the utility class and get the response
back, which is still about 5 lines of code I would need to duplicate. Any
ideas on what I should be doing?
Thanks! 4 2148
DancnDude,
I would take the CompareFields1 class and rename it CompareFieldsBase.
In the Compare method, I would have the common code, and then I would have
the specific compare code call out to a virtual method. Then, have classes
that derive from CompareFieldsBase which would override the virtual method.
Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- ni**************@exisconsulting.com
"DancnDude" <da*******@hotmail.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl... I have a class that needs to have several different kinds of sorting routines on an ArrayList that it needs to conditionally do based upon the data. I have successfully created a class that implements IComparer and does the sort correctly for one of these sort types. This class's definition is like:
// Sort class that implements IComparer so public class CompareFields1 : IComparer { public int Compare(object a, object b) { // Code that appears in every sort's Compare routine if (a == b) return 0; if (a == null) return -1; if (b == null) return 1; if (a.GetType() != b.GetType()) { throw new ArgumentException(); }
// Now that the inputs are valid, do the actual compare /*... ...actual compare code here... ...*/ } }
Now I want to make another sort algorithm (CompareFields2). This
definition will be have the same layout as the above one, but the actual compare code is going to be different. I would like to try and factor out the common code, but I can't seem to think of a good class design for it since this isn't in the constructor. I thought of factoring the code into a utility class, but that requires me to call the utility class and get the response back, which is still about 5 lines of code I would need to duplicate. Any ideas on what I should be doing?
Thanks!
Thanks! That is exactly what I want it to do, but just couldn't think of
the right way to do it. :)
"Nicholas Paldino [.NET/C# MVP]" <ni**************@exisconsulting.com> wrote
in message news:Od**************@TK2MSFTNGP10.phx.gbl... DancnDude,
I would take the CompareFields1 class and rename it CompareFieldsBase. In the Compare method, I would have the common code, and then I would have the specific compare code call out to a virtual method. Then, have
classes that derive from CompareFieldsBase which would override the virtual
method. Hope this helps.
-- - Nicholas Paldino [.NET/C# MVP] - ni**************@exisconsulting.com
"DancnDude" <da*******@hotmail.com> wrote in message news:%2****************@tk2msftngp13.phx.gbl... I have a class that needs to have several different kinds of sorting routines on an ArrayList that it needs to conditionally do based upon
the data. I have successfully created a class that implements IComparer and does the sort correctly for one of these sort types. This class's definition is like:
// Sort class that implements IComparer so public class CompareFields1 : IComparer { public int Compare(object a, object b) { // Code that appears in every sort's Compare routine if (a == b) return 0; if (a == null) return -1; if (b == null) return 1; if (a.GetType() != b.GetType()) { throw new ArgumentException(); }
// Now that the inputs are valid, do the actual compare /*... ...actual compare code here... ...*/ } }
Now I want to make another sort algorithm (CompareFields2). This definition will be have the same layout as the above one, but the actual compare
code is going to be different. I would like to try and factor out the common code, but I can't seem to think of a good class design for it since this isn't in the constructor. I thought of factoring the code into a
utility class, but that requires me to call the utility class and get the
response back, which is still about 5 lines of code I would need to duplicate.
Any ideas on what I should be doing?
Thanks!
No, it's not derived from CompareFields1. They would all be derived from
CompareBase as Nicholas suggested. The part that I couldn't think to do was
having the base's Compare method call a virtual method whose implementation
will only really exist in the derived classes. The virtual method kinda
threw me since I never had a need to use one until now.
Thanks for the suggestion!
"Jay B. Harlow [MVP - Outlook]" <Ja********@email.msn.com> wrote in message
news:#I**************@TK2MSFTNGP12.phx.gbl... DancnDude, Is CompareFields2 derived from CompareFields1?
If it is, use the Template Method Pattern for the "actual compare code here". I would then consider making a new abstract base class that both CompareFields1 & CompareFields2 inherit from, but you can keep CompareFields1 as the base.
public class CompareFields1 : IComparer { public int Compare(object a, object b) { // Code that appears in every sort's Compare routine if (a == b) return 0; if (a == null) return -1; if (b == null) return 1; if (a.GetType() != b.GetType()) { throw new ArgumentException(); }
// Now that the inputs are valid, do the actual compare return OnCompare(a, b)
}
protected virtual int OnCompare(object a, object b) { /*... ...actual compare code here... ...*/ } }
public class CompareField2 : CompareFields1 { protected overrides int OnCompare(object a, object b) { /*... ...actual compare code here... ...*/ } }
I've been reading "Test-Driven Development - By Example" by Kent Beck,
from Addison Wesley. He mentions a Pluggable Selector pattern, that may be
useful here. Using a Delegate in .NET would simplify the Pluggable Selector pattern. With the Pluggable Selector you only have one class, this class would have a method for each sorting routine. I would use a Delegate to
hold that sorting routine.
public class MyComparer : IComparer {
delegate int CompareRoutine(object a, object b);
readonly CompareRoutine OnCompare;
public MyComparer() { OnCompare = new CompareRoutine(CompareFields1); }
public int Compare(object a, object b) { // Code that appears in every sort's Compare routine ... // Now that the inputs are valid, do the actual compare return OnCompare(a, b) }
private virtual int CompareFields1(object a, object b) { /*... ...actual compare code here... ...*/ }
private virtual int CompareFields2(object a, object b) { /*... ...actual compare code here... ...*/ }
}
The problem now becomes how to initialize OnCompare, I would consider
either a Factory Method or enum passed to the constructor to decide which compare routine I was going to use. (The Factory method would send the delegate itself to the constructor, I would have a static method for each sort method...
Hope this helps Jay
"DancnDude" <da*******@hotmail.com> wrote in message news:%2****************@tk2msftngp13.phx.gbl... I have a class that needs to have several different kinds of sorting routines on an ArrayList that it needs to conditionally do based upon
the data. I have successfully created a class that implements IComparer and does the sort correctly for one of these sort types. This class's definition is like:
// Sort class that implements IComparer so public class CompareFields1 : IComparer { public int Compare(object a, object b) { // Code that appears in every sort's Compare routine if (a == b) return 0; if (a == null) return -1; if (b == null) return 1; if (a.GetType() != b.GetType()) { throw new ArgumentException(); }
// Now that the inputs are valid, do the actual compare /*... ...actual compare code here... ...*/ } }
Now I want to make another sort algorithm (CompareFields2). This definition will be have the same layout as the above one, but the actual compare
code is going to be different. I would like to try and factor out the common code, but I can't seem to think of a good class design for it since this isn't in the constructor. I thought of factoring the code into a
utility class, but that requires me to call the utility class and get the
response back, which is still about 5 lines of code I would need to duplicate.
Any ideas on what I should be doing?
Thanks!
That's exactly what I did and it's working wonderfully. :)
"Nicholas Paldino [.NET/C# MVP]" <ni**************@exisconsulting.com> wrote
in message news:OK**************@TK2MSFTNGP11.phx.gbl... DancnDude,
Better yet, for a cleaner implementation, make the CompareFieldsBase class abstract and make the virtual method abstract as well. This way,
your base class could not be instantiated, and you would force the override of the method in derived classes (if you wanted this behavior, that is).
-- - Nicholas Paldino [.NET/C# MVP] - ni**************@exisconsulting.com
"DancnDude" <da*******@hotmail.com> wrote in message news:OI**************@TK2MSFTNGP12.phx.gbl... No, it's not derived from CompareFields1. They would all be derived
from CompareBase as Nicholas suggested. The part that I couldn't think to do was having the base's Compare method call a virtual method whose implementation will only really exist in the derived classes. The virtual method kinda threw me since I never had a need to use one until now.
Thanks for the suggestion!
"Jay B. Harlow [MVP - Outlook]" <Ja********@email.msn.com> wrote in message news:#I**************@TK2MSFTNGP12.phx.gbl... DancnDude, Is CompareFields2 derived from CompareFields1?
If it is, use the Template Method Pattern for the "actual compare code here". I would then consider making a new abstract base class that
both CompareFields1 & CompareFields2 inherit from, but you can keep CompareFields1 as the base.
> public class CompareFields1 : IComparer > { > public int Compare(object a, object b) > { > // Code that appears in every sort's Compare routine > if (a == b) return 0; > if (a == null) return -1; > if (b == null) return 1; > if (a.GetType() != b.GetType()) > { > throw new ArgumentException(); > } > > // Now that the inputs are valid, do the actual compare
return OnCompare(a, b)
> }
protected virtual int OnCompare(object a, object b) { /*... ...actual compare code here... ...*/ } > }
public class CompareField2 : CompareFields1 { protected overrides int OnCompare(object a, object b) { /*... ...actual compare code here... ...*/ } }
I've been reading "Test-Driven Development - By Example" by Kent Beck, from Addison Wesley. He mentions a Pluggable Selector pattern, that may be useful here. Using a Delegate in .NET would simplify the Pluggable Selector pattern. With the Pluggable Selector you only have one class, this
class would have a method for each sorting routine. I would use a Delegate
to hold that sorting routine.
public class MyComparer : IComparer {
delegate int CompareRoutine(object a, object b);
readonly CompareRoutine OnCompare;
public MyComparer() { OnCompare = new CompareRoutine(CompareFields1); }
public int Compare(object a, object b) { > // Code that appears in every sort's Compare routine ... > // Now that the inputs are valid, do the actual compare return OnCompare(a, b) }
private virtual int CompareFields1(object a, object b) { > /*... ...actual compare code here... ...*/ }
private virtual int CompareFields2(object a, object b) { > /*... ...actual compare code here... ...*/ }
}
The problem now becomes how to initialize OnCompare, I would consider either a Factory Method or enum passed to the constructor to decide which compare routine I was going to use. (The Factory method would send the
delegate itself to the constructor, I would have a static method for each sort method...
Hope this helps Jay
"DancnDude" <da*******@hotmail.com> wrote in message news:%2****************@tk2msftngp13.phx.gbl... > I have a class that needs to have several different kinds of sorting > routines on an ArrayList that it needs to conditionally do based
upon the > data. I have successfully created a class that implements IComparer and > does the sort correctly for one of these sort types. This class's > definition is like: > > // Sort class that implements IComparer so > public class CompareFields1 : IComparer > { > public int Compare(object a, object b) > { > // Code that appears in every sort's Compare routine > if (a == b) return 0; > if (a == null) return -1; > if (b == null) return 1; > if (a.GetType() != b.GetType()) > { > throw new ArgumentException(); > } > > // Now that the inputs are valid, do the actual compare > /*... ...actual compare code here... ...*/ > } > } > > > Now I want to make another sort algorithm (CompareFields2). This definition > will be have the same layout as the above one, but the actual
compare code > is going to be different. I would like to try and factor out the common > code, but I can't seem to think of a good class design for it since this > isn't in the constructor. I thought of factoring the code into a utility > class, but that requires me to call the utility class and get the response > back, which is still about 5 lines of code I would need to
duplicate. Any > ideas on what I should be doing? > > Thanks! > >
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Andrew Koenig |
last post by:
Suppose I want to define a class hierarchy that represents expressions, for
use in a compiler or something similar.
We might imagine various kinds of expressions, classified by their top-level...
|
by: Steve Jorgensen |
last post by:
A while back, I started boning up on Software Engineering best practices and
learning about Agile programming. In the process, I've become much more
committed to removing duplication in code at a...
|
by: Steve Jorgensen |
last post by:
Recently, I tried and did a poor job explaining an idea I've had for handling
a particular case of implementation inheritance that would be easy and obvious
in a fully OOP language, but is not at...
|
by: Steve Jorgensen |
last post by:
The classing Visual Basic and VBA support for polymorphism, let's face it, is
a bit on the weak side, and built-in support for inheritance is non-existent.
This little essay is about some patterns...
|
by: Hazz |
last post by:
Are there any good references/articles/books which provide clarity toward my
insecurity still on deciding how to model a complex system? I still feel
uncomfortable with my understanding, even...
|
by: Joe Fallon |
last post by:
I have a WinForm that is a Base class. It has many controls on it and a lot
of generic code behind.
When I inherit the form I override 5 methods and now each child form has the
same look and feel...
|
by: Mark O'Flynn |
last post by:
I would like some advice regarding implementing inheritance using vb.net.
I have an application written in vb6 that I am rewritting from the ground up
in vb.net to take full advantage of .net,...
|
by: J.M. |
last post by:
I have a question concerning inheritance: can an abstract (parent) class
have an abstract object? I would like to make a concrete child inherit from
this class by inheriting from this object. Let...
|
by: RSH |
last post by:
Hi,
I am working on some general OOP constructs and I was wondering if I could
get some guidance.
I have an instance where I have a Base Abstract Class, and 4 Derived
classes. I now need to...
|
by: John |
last post by:
Hi All,
Although C# has Generics, it still does not support the generic
programming paradigm. Multiple inheritance is required to support real
generic programming. Here is a simple design pattern...
|
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,...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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,...
|
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...
| |