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

Utility classes?

I usually find that a number of unrelated classes require the same kind of
operations. However, I don't want to duplicate code in multiple places. In
Java, I've seen those "Utility Classes", which are basically static
(singleton) classes created to encapsulate related operations in one class.
These methods can be called from other classes that need same functionaliy.

Is there a similar concept in C++?

I know that if two classes require the same operations, it might be
reasonable to derive them from a base class and move those operations to the
base class. However, in my case, I really considered this and found that my
classes represent really different concepts, albeit requiring a number of
similar operations. So, creating a utility class seemed reasonable.

I'd like to here your suggestions about this.

Thanks,
Bora
Jul 22 '05 #1
7 2258
Bora wrote:
I usually find that a number of unrelated classes require the same kind of
operations. However, I don't want to duplicate code in multiple places. In
Java, I've seen those "Utility Classes", which are basically static
(singleton) classes created to encapsulate related operations in one class.
These methods can be called from other classes that need same functionaliy.

Is there a similar concept in C++?
You can use namespaces and just regular functions, or you can create a
singleton that contains these functions. Really depends on what your
needs are.
I know that if two classes require the same operations, it might be
reasonable to derive them from a base class and move those operations to the
base class. However, in my case, I really considered this and found that my
classes represent really different concepts, albeit requiring a number of
similar operations. So, creating a utility class seemed reasonable.


C++ allows for multiple inheritance but in my opinion it should be
avoided. There are many cases where it becomes necissary to use MI,
like for instance when using the observer pattern, but short of these
necesities MI should not be used. In my opinion, and I am not alone, it
is better to repeat code than to create a messy inheritance tree through
suplerfuous use of MI.

Unless you need some special stuff I would recommend using namespaces.
Namespaces will encapsulate your functions without the weight of having
a class, not that it is that much anyway.

NR

Jul 22 '05 #2

"Bora" <no****@nospam.none> wrote in message
news:bq**********@fred.mathworks.com...
I usually find that a number of unrelated classes require the same kind of
operations. However, I don't want to duplicate code in multiple places. In Java, I've seen those "Utility Classes", which are basically static
(singleton) classes created to encapsulate related operations in one class. These methods can be called from other classes that need same functionaliy.
Is there a similar concept in C++?

I know that if two classes require the same operations, it might be
reasonable to derive them from a base class and move those operations to the base class. However, in my case, I really considered this and found that my classes represent really different concepts, albeit requiring a number of
similar operations. So, creating a utility class seemed reasonable.


Well, if you give people a hammer they'll certainly find a nail. Inheritance
is a good & clever concept but it should be reserved to things that are
related to each other. Thus inheriting only to share some implementation is
not the right way IMHO. There are numerous discussions about when to inherit
and what to inherit (see Effective C++ by Scott Meyers, Exceptional C++ by
Herb Sutter and so on....). What you can do is that you either implement the
functionality in terms of free functions or put them into a class as static
functions. For example

class CMath {
public:
static double Pi() { return 4.0 * atan(1.0); };
static double SinSquared( double x ) { return sin(x) * sin(x); };
};

HTH
Chris
Jul 22 '05 #3
"Bora" <no****@nospam.none> wrote in message news:<bq**********@fred.mathworks.com>...
I usually find that a number of unrelated classes require the same kind of
operations. However, I don't want to duplicate code in multiple places. In
Java, I've seen those "Utility Classes", which are basically static
(singleton) classes created to encapsulate related operations in one class.
These methods can be called from other classes that need same functionaliy.

Is there a similar concept in C++?
certanly:

Class MyUtils
{
public:
static void DoStuff();
static void DoMoreStuff();
};

that's a class with only static members which serves as a utility
class. you never need to create instances of this class since you
never need them since all the methods are static. this is not a
singleton in the conventional way of being a singleton since there
isn't even one instance of this class. to call methods of this class
you do:
MyUtils::DoStuff();

you can even enforce the fact that there should be no instances
created doing the following:

Class MyUtils
{
public:
static void DoStuff();

private:
MyUtils(); // private constructor with NO implementation in the cpp.
}

this will cause a compiler error to whoever trys to instantiate this
class.
good luck.

I know that if two classes require the same operations, it might be
reasonable to derive them from a base class and move those operations to the
base class. However, in my case, I really considered this and found that my
classes represent really different concepts, albeit requiring a number of
similar operations. So, creating a utility class seemed reasonable.

I'd like to here your suggestions about this.

Thanks,
Bora

Jul 22 '05 #4
Bora wrote:
I usually find that a number of unrelated classes require the same kind of
operations. However, I don't want to duplicate code in multiple places. In
Java, I've seen those "Utility Classes", which are basically static
(singleton) classes created to encapsulate related operations in one class.
These methods can be called from other classes that need same functionaliy.

Is there a similar concept in C++?

I know that if two classes require the same operations, it might be
reasonable to derive them from a base class and move those operations to the
base class. However, in my case, I really considered this and found that my
classes represent really different concepts, albeit requiring a number of
similar operations. So, creating a utility class seemed reasonable.

I'd like to here your suggestions about this.

Thanks,
Bora


Check out the Boost library (http://www.boost.org)
especially the package for arithmetic operators which provides a
template for adding those pesky ==, !=, <, >, <=, >= operators
will reduced annoyance.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book

Jul 22 '05 #5

"Bora" <no****@nospam.none> wrote in message
news:bq**********@fred.mathworks.com...
I usually find that a number of unrelated classes require the same kind of
operations. However, I don't want to duplicate code in multiple places. In Java, I've seen those "Utility Classes", which are basically static
(singleton) classes created to encapsulate related operations in one class. These methods can be called from other classes that need same functionaliy.
Is there a similar concept in C++?

I know that if two classes require the same operations, it might be
reasonable to derive them from a base class and move those operations to the base class. However, in my case, I really considered this and found that my classes represent really different concepts, albeit requiring a number of
similar operations. So, creating a utility class seemed reasonable.


You might want to do a search on "mix in" or "mixin" classes.
Jul 22 '05 #6
"Bora" <no****@nospam.none> wrote in message news:<bq**********@fred.mathworks.com>...
I usually find that a number of unrelated classes require the same kind of
operations. However, I don't want to duplicate code in multiple places. In
Java, I've seen those "Utility Classes", which are basically static
(singleton) classes created to encapsulate related operations in one class.
These methods can be called from other classes that need same functionaliy.

Is there a similar concept in C++?

I know that if two classes require the same operations, it might be
reasonable to derive them from a base class and move those operations to the
base class. However, in my case, I really considered this and found that my
classes represent really different concepts, albeit requiring a number of
similar operations. So, creating a utility class seemed reasonable.

I'd like to here your suggestions about this.


What you've encountered in Java a an attempt to work around the
limitations of the language to provide necessary functionality. Such
a workaround is not necessary in C++.

If you find you have operations on data that are not specific to those
data, then you implement that as namespace-level functions. You may
find you need to template those functions to handle different types
differently, and specialize those templates for special cases.

A good example of how this is done is the standard library that comes
with C++. It contains a slew of algorithms that operate on data.
They aren't utility classes, they're functions.

The concept of a class in C++ is simpler than in Java. In C++ a class
is a collection of related data and functions that operate on those
data. If you have functions that don't operate on the data contained
in a class, why should those functions be contained in the class?

--
Stephen M. Webb
st**********@bregmasoft.com
Jul 22 '05 #7
"Bora" <no****@nospam.none> wrote in message news:<bq**********@fred.mathworks.com>...
I usually find that a number of unrelated classes require the same kind of
operations. However, I don't want to duplicate code in multiple places. In
Java, I've seen those "Utility Classes", which are basically static
(singleton) classes created to encapsulate related operations in one class.
These methods can be called from other classes that need same functionaliy.

Is there a similar concept in C++?

I know that if two classes require the same operations, it might be
reasonable to derive them from a base class and move those operations to the
base class. However, in my case, I really considered this and found that my
classes represent really different concepts, albeit requiring a number of
similar operations. So, creating a utility class seemed reasonable.


There are many options, and it depends on the context how you handle it.

For example, if the utility functionality does not require any access
to the data members of a class, it may be appropriate to put it in a
global function. The archetype there is numerical functions like sin,
exp, and so on. You pass them a single arg, or possibly a very small
number of args, and they send back the result. This is also most
reasonable when you don't want any retention of data from one call
to the next. You might include them in a class if they needed to have
retention of data. In that case, they could either be static or
non-static, depending on if you need independent sequences of calls.

If you are looking for something that handles generic classes, you
should consider a template. It is often possible to implement an
algorithm in a template, then pass it any class to which the
algorithm makes sense. There will likely still be some overhead on
the classes that are processed this way. For examples, look at
the standard library algorithm templates.
Socks
Jul 22 '05 #8

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

Similar topics

0
by: Zane Thomas [.NET/C# MVP] | last post by:
If, like me, your connection is being swamped downloading copies of the Swen Worm and bounce messages, you might find the utility I wrote today useful. Source for the utility is distributed with...
0
by: james | last post by:
My fellow VB.NET developers, Ever wonder why MS didn't include a "Class Builder" in Visual Studio .NET like they did in other versions of visual studio? So did we.
1
by: Dhanak | last post by:
I have around 1000 aspx files that contains standard lines intended for debugging purpose.Now before we put these to production we need to remove these lines from all files. Instead of doing it...
2
by: Christoph Haas | last post by:
Evening, I'm currently working on a larger Python project that consists of multiple programs and packages. As I need a few utility functions time and again I moved them all into a Utility...
7
by: wolfing1 | last post by:
Is there a way to see what CSS classes in a page are not defined? (like if I want to find mispelled class names, etc)
27
by: Neil | last post by:
Anyone have a recommendation for a good inexpensive or free utility for developing table schemas.
4
by: Jeff User | last post by:
Hi all Developing C# web apps, .net1.1 I have gotten in habit of placing commonly used (interface) functions in my base page. However, some apps I work on use a seperate "Utility" class...
6
by: Marco | last post by:
One of the things I like about C++ is that it doesn't force you to create fake "objects" in the OO sense. We had a debate at the office about the "right" way to encapsulate related utility...
37
by: Phlip | last post by:
1230987za wrote: Kanze is a classically-trained "unit tester". In some circles "unit" is a QA concept - specifically, if a test fails, you only need to inspect one unit. So "units" are...
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?
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...
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
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...

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.