Utility classes?

B

Bora

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
 
C

Chris Theis

Bora said:
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
 
N

Noah Roberts

Bora said:
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
 
S

shoosh4242

Bora said:
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.
 
T

Thomas Matthews

Bora said:
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
 
J

jeffc

Bora said:
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.
 
S

Stephen M. Webb

Bora said:
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?
 
P

puppet_sock

Bora said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,150
Messages
2,570,853
Members
47,394
Latest member
Olekdev

Latest Threads

Top