Re: Utility classes?

From: Noah Roberts (nroberts_at_dontemailme.com)
Date: 12/05/03


Date: Fri, 05 Dec 2003 00:46:54 -0800

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



Relevant Pages

  • Re: Global variables
    ... > (generally from external file), that need to be shared in the application. ... I think using Singleton may be a different issue. ... the behavior doing the creating may be invoked multiple times. ... sending emails before I speculate on where to glue the SMTP server ...
    (comp.object)
  • Re: Singleton Pattern
    ... one should /only/ use Singleton when the problem solution has multiple paths that could lead to violating the problem space referential integrity requirement in. ...
    (comp.object)
  • Re: Thread-safe Collection
    ... but after that it would only be supporting multiple readers. ... I wanted the dictionary items in a common memory store (to avoid ... If you pin a singleton somewhere, ... If you want to have fast parallel reads from multiple STAs, ...
    (microsoft.public.vb.com)
  • Re: Static Methods in DAO
    ... methods, or a single shared object (i.e. singleton), how can multiple ... code in a method at the exact same time if there is one shared object ...
    (comp.lang.java.programmer)