In my design i have a lot of objects which have a single Run() method, which when called performs what the object should do. Is that a valid interface design? It seems as though most objects I normally use have more than 1 method.
Asked
Active
Viewed 149 times
-1
1 Answers
3
An object with a single method is just a (first-class) procedure / function. An object with a single method and some instance variables is just a (first-class) closure ("lambda").
This is how Java programers did lambdas before Java had lambdas, and is in fact how Java itself does lambdas.
There is nothing wrong with lambdas. The only thing I would say is that if your language has lambdas, then for reasons of readability, maintainability, and clarity, you should use those instead of using something that is the same as a lambda but isn't a lambda.
The Strategy Software Design Pattern is based around a protocol with a single method, for example.

Jörg W Mittag
- 103,514
new Foo(a,b).Run();
that might be better simply doing a function as infooRun(a,b);
– Erik Eidt Feb 19 '22 at 16:05Run
method is not a problem on its own. The interesting question is how your object processes inputs and output. If this is all processed by side effects to other objects (or to external datasources like a database), then I bet your code is pretty procedural and hard to test. – Doc Brown Feb 24 '22 at 13:29