4

I would like to have a class which contains only public functions, For example:

class foo
{
    public:
        int f1(param1, param2) ;
        void f2(param1, param2);
};

The Class doesn't have state, it just manipulate input parameters, Behaves like auxiliary class,

Is it a good design ? or need special pattern for that ? What is the name of the pattern?

user3498424
  • 47
  • 1
  • 6

3 Answers3

8

Short answer: use functions inside a namespace.

namespace foo
{
    int f1(param1, param2);
    void f2(param1, param2);
}

However, if your application has a OO design, you should ask yourself why you need a bunch of functions that just manipulate input parameters. Believe me: your code is trying to tell you something :-)

In your snippet of code, you used dummy names (foo, f1, f2, param1, param2), so I cannot guess what your functions do. But maybe your functions operate on the same parameter: in that case you can promote the parameter to become a class.

Before:

bool IsLeapYear( int date );
void IncrementDay( int date, int numOfDays );

After:

class Date
{
    public:
        bool IsLeapYear() const;
        void IncremetDay( int numOfDays );
    private:
        int daysFromOrigin;
};

If your functions take different parameters, instead, you can consider to allocate your functions to existing classes on your OO design (on the assumption you do want a OO design!).

Finally, if your functions are unrelated, you can consider to move each of them in existing classes even as static methods (it really depends on the meaning of your functions).

As for your final question "Is it a good design?", well it depends on what design you're working on:

  • object oriented design: likely it's not a good OO design, but without more context it's difficult to say
  • procedural design: it's ok
  • functional design: it's ok

While this may or may not be a good design according to your paradigm, certainly that's not a good C++ idiom: you should use free functions inside a namespace instead of a class with only static methods.

Daniele Pallastrelli
  • 2,430
  • 1
  • 21
  • 37
  • @doctorlove I put in bold font the statement "if your application has a OO design". If you have a functional application instead, you've not got a OO design. In that case, you can go for my short answer :-) – Daniele Pallastrelli Jul 08 '14 at 08:37
  • I can offer you one real life good example of OO design that a class with only public functions is a good design. Lets say you have created a class named switcher that takes an interface class as a member this interface class must have the functions void on() and void off() .An interface class named led may be stateless but an interface class named toaster might not be ... – Spyros Mourelatos Oct 03 '20 at 11:56
  • Also because comment's can't be big that's my example https://godbolt.org/z/1cq5Ea – Spyros Mourelatos Oct 03 '20 at 12:13
7

Just make them free standing functions. Not everything in C++ has to be in a class. If you look at the STL, for example, you'll see that it has lots of free standing functions.

Sean
  • 60,939
  • 11
  • 97
  • 136
1

Make sure you declare your methods static if you want to call them without and instance of the class.

Then you can call them with the syntax:

foo::f1(param1, param2)

Having them in a class rather than a struct or having them freestanding makes you program more OO, it makes it easy to model (with UML or like) and create relationships between other parts of the system (through inheritance, polymorphism etc).

if the rest of your program is OO then it makes it consistent with the rest of the program.

So i'd say yes, it's a good design.

Sam Fare
  • 51
  • 3
  • I'd say free functions in a namespace is more usual in C++ – doctorlove Jul 08 '14 at 08:17
  • 1
    I've seen many projects like that, yes, but with more modern ones it tends not to be the case. depends a lot on the project I suppose, Perhaps the best good advice would be to see what else is in the code base before making a change. If the OP is starting from scratch I would always recommend a 100% OO approach. – Sam Fare Jul 08 '14 at 08:21
  • 2
    Using a class composed of just static member functions instead of a namespace is bad design. There's nothing "OO" about that and C++ isn't even an OO language. – Simple Jul 08 '14 at 08:23
  • 2
    Could you justify why you think it's bad design? C++ may not be Ruby of java, but still supports objects. even languages that don't support native OO syntax can be and are used in accordance with OO principles, so there's no excuse in 2014 not to be using it. – Sam Fare Jul 08 '14 at 08:28
  • It's a complete myth that having free functions takes you away from OO. OO does _not_ mean "everything is a member function". – Lightness Races in Orbit Jul 08 '14 at 13:31