0

My question relates to this topic here: Are classes with only a single (public) method a problem?

There I read in the comments often something like that:

It is no longer object oriented. Because those classes don't represent anything, they are just vessels for the functions.

So my question is, how to write that the object oriented way?

In the topic I linkened the main question is, if it is a good design having just one public method in a class, which have no state.

Let's say I have the following two classes

class A
{
    String field1;
    String field2;
    String field3;        
}

class B { String field1; String field2;

String fieldK;
String fieldM;

}

Now I want to have some/two "behaviour";

    private String behaviourA()
    {
        //works with field1 and field2
    }
private String behaviourB()
{
    //works with field1 and fieldK
}

class B should have behaviourA and behaviourB class A can only have behaviourA

I don't want to copy-paste behaviourA into both classes. So iI could use inheritance. But if class A and B already inherits from something else, then I can't use it, if no multiple inheritance is availiable.

So my solution would be an extra class Z, which contains behaviourA and inject/or hard-coded (the injection detail is not relevant for my question) this into both classes A and B, but class Z is in that way just a "functionObject" with no state.

Something like that, from that people say, that this isn't object oriented.

I don't say that this is bad. (SOLID and object oriented design are somewhat close, ask @Robert Bräutigam ^^ And I'am on his side in that thinking.)

But my question is here now, how poeple, who say, that classes with no state and only behaviour/one method are not object oriented, would refactor my example not having multi inheritance and not having copy paste of code to get an object oriented design in their opinion.

  • 1
    The question as currently phrased seems to be a misunderstanding of at least one of the concepts that it references. Rather than dictionary define all of the constituent parts of your question; what benefit do you think object-oriented code provides that is not tied to state? – Flater Mar 06 '24 at 02:32
  • There was an small mistake in my example, I fixed that. I don't know? My question is how to get this code into something like that is tied to state not having any more that Function/Strategy-Objects, because there it seems to exists a solution, because people said that this is not object oriented, and so I hope to get an example from that people who can show this the object oriented way, from which I don't now, if that way even exists for the described "example"-problem – Robin Kreuzer Mar 06 '24 at 02:41
  • 3
    A question's meaning hinges on how you understand the words that form the question, and your question is coming across as confused more than it is targeted. It would be decidedly more efficient for you to look up educational materials on what object-oriented programming actually is, rather than throwing things at the wall to see what sticks. People told you something is not [..], then ask those people what they meant by [..] instead of having internet strangers read your probably imperfect recollection of their feedback and then guess what the other person may have been thinking. – Flater Mar 06 '24 at 02:50
  • There were more than "one" people^^ So I guess , that a lof of people think that way. But yes this question is directed for that people-circle. Of course if you dont share their opinion, you can't answer my question. But maybe to include also the other people (like you^^), I can also ask, how you would design my example. Do you think its ok that way (on a best-practice-view), or would you do there something else? Now i must go to bed, maybe i can change my question a little bit tomorrow, if there are no people from that meant circle^^ – Robin Kreuzer Mar 06 '24 at 02:58
  • None of this is a targeted question in a way that StackExchange's Q&A format expects posted questions to be. I can only repeat my advice to look up actual resource material on object-oriented design instead of trying to divine what it could be based on different people's feedback. I have voted to close the question as it is in dire need of focus before it can be meaningfully answered. – Flater Mar 06 '24 at 03:00
  • The quote in my question was from this answer here: https://softwareengineering.stackexchange.com/a/225920/347781

    30 people think, that he is right, so I think there must be some people, who can answer my question. Also @Doc Brown seems to be a little bit that circle-like. So the question to him could be, how he would design it, if not following the modularity-principles he worked out in his answer in that linked topic

    – Robin Kreuzer Mar 06 '24 at 03:03
  • 2
    You're using this question to spawn a discussion thread about another posted (10 year old) answer. This is off-topic. The way you are trying to engage with the community here does not align with the community guidelines on what constitutes a good on-topic question. "I know people on this site who might know more about this" is not a valid basis for a question. – Flater Mar 06 '24 at 03:18
  • 5
    You are conflating a few things here. First, "One public method"does not mean "no state", these two properties are orthogonal. Second, there are different schools-of-thought about what OO is or should be, so just because one person says some code is not "OO", another one does not necessarily share this perspective. Third, it makes IMHO no sense to practice refactoring using meaningless classes A, B with meaningless fields and methods. A meaningful, self-contained and working (!) real-world example would be a much better start for a question. – Doc Brown Mar 06 '24 at 07:02
  • 3
    This question seems to ride on semantics of words, which are always subjective and context-dependent, such is the nature of English Language. (see https://softwareengineering.stackexchange.com/questions/46592/so-what-did-alan-kay-really-mean-by-the-term-object-oriented - Alan Kay's definition is about messaging, not objects). Also, people often conflate the terms "OO" with "object-based" - https://stackoverflow.com/questions/15430004/core-difference-between-object-oriented-and-object-based-language – Ben Cottrell Mar 06 '24 at 07:57
  • @DocBrown or the others, do you know then, how to design it in another way, to get something that match their (who said that it is not object oriented) "school-of-thought" about what OO is? – Robin Kreuzer Mar 06 '24 at 23:04
  • @RobinKreuzer: sorry, but I cannot see any effort of yours to replace this meaningless example by a meaningful one. And I don't design any code on the basis of "some ideologists may say it is not object oriented" - I design code on the basis of what makes sense. When some functional construct makes most sense, I use that. When my programming language only allows functional constructs by utilizing classes as a workaround, I use that. – Doc Brown Mar 07 '24 at 05:57

1 Answers1

1

The premise is just wrong. It is fine to have object with no internal state and can be perfectly "object oriented".

A common example is in the Strategy pattern, where the strategy-object encapsulate some behavior or algorithm separately from the data it operates on. A strategy object can fulfill its role fine without any internal state.

To be fair, there is no exact, universally agreed definition of "object oriented". But I don't know of any reasonable interpretation which would say the strategy pattern was not object-oriented.

In your particular case, it would be fine to have behaviourA and behaviourB as two stateless objects (or function objects) and then use them in class A and B as appropriate.

I can't explain why someone would claim this is not object-oriented, but if I had to guess it is probably a misunderstanding due to the fact that you don't necessarily need an object. If the object in question have only a single method you might also be able to use a stand-alone function or lambda instead. In many cases this would be simpler. But there could also be good reasons to use an object. E.g. if your are implementing an interface or have to support inheritance, or if you anticipate the need for more methods.

JacquesB
  • 59,334
  • 1
    Bad example. Strategy objects have at least one internal state information - their specific type, which is necessary to decide about the called "behaviour". Moreover, the strategy pattern allows all kind of stateful implementations, these things are orthogonal to each other. – Doc Brown Mar 06 '24 at 07:13
  • 1
    @DocBrown It allows stateful implementations (e.g. the strategy could have options, internal cache etc.) but it does not require it. A stateless strategy would be perfectly fine. – JacquesB Mar 06 '24 at 07:18
  • Read my first sentence again. If you want a really stateless object, you need to forbid virtual inheritance. – Doc Brown Mar 06 '24 at 07:28
  • @DocBrown: It is pretty obvious from the question that "state" in this context refers to object fields, not methods. – JacquesB Mar 06 '24 at 07:38
  • 1
    It it pretty obvious the OP is confused and did not express themselves very well, hence I would be careful whem jumping to conclusions what they meant with state. – Doc Brown Mar 06 '24 at 09:03
  • yeah i know that the strategy fall into this (similar(just similar for Doc Brown) ) class. But it bothers me, that in the linked topic a lot of people said (with good upvotes), that this is not object oriented. Thats ok for them, they can say this. But my question for them is then, how they would do it else, to get a solution that match they meaning of object orientend. – Robin Kreuzer Mar 06 '24 at 23:03
  • @RobinKreuzer I can't second guess the reasoning of others, but I think the argument is that an object with a single method could be represented as a free function or lambda instead. This is a reasonable argument. But it does not follow that having an object with a single method is not "object oriented" or that it is something to avoid. In some cases using a free function would be simpler, but in other cases (e.g if the object implement an interface or you anticipate adding more methods or dependencies) using an object with a single method is the way to go. – JacquesB Mar 07 '24 at 07:06
  • @RobinKreuzer: the accepted answer with>100 upvotes does not say something like that. The second highest voted answer essentially says "though it should not be called OO (to the answerer's understanding of OO), the code is fine". So I guess you are overthinking this. – Doc Brown Mar 07 '24 at 08:49
  • @RobinKreuzer, in my view, the conventional definition of an object is that it is a data structure which contains a table of methods. An individual object need have no internal fields (except the necessary function pointers, of course), and there may be only one entry in the object's method table. – Steve Mar 07 '24 at 16:10
  • 2
    "Strategy objects have at least one internal state information - their specific type" For the term "state" to have any utility, I don't think it can include an objects own type. Otherwise everything ends up "stateful", and nothing is "stateless". Not a useful term anymore in that case. – Alexander Mar 07 '24 at 17:21
  • @Alexander: this is plain wrong, When using the strategy pattern, the type information is the essential state which decides which of the different possible selections of behaviour. Objects without state, however, exist, those are "final" classes (Java) or sealed classes (C#) with no member attributes, And those are indeed just "vessels for functions". Maybe you are conflating "state" with "mutable state"? – Doc Brown Mar 08 '24 at 05:45
  • @DocBrown Final/sealed classes just mean you can't derive subtypes. Instances still contain type information just like other objects, otherwise they couldn't support polymorphism. – JacquesB Mar 08 '24 at 06:41
  • @DocBrown I'm trying to follow your train of thought, but I'm just not seeing i. final classes can still implement interfaces and thus have polymorphism, based on their own type. Are they stateful, too? Could you give me an example of a stateless object in Java or C#? – Alexander Mar 08 '24 at 13:55
  • @Alexander: the OP was talking about a class Z with just public one function. Make that class final, assume it has no member attributes and don't let it inherit from any other class or interface. Or go a step further and make the class static. Do you think that is still Object-Oriented? Some people may say yes, others no. But even if one says "no", is it worth to shoehorn this into a "more OO-like approach"? IMHO that makes no sense. – Doc Brown Mar 08 '24 at 15:14
  • "Do you think that is still Object-Oriented?" No, but I do think it's stateless. I would say that a final class with one public method which implements an interface requirement is both stateless (I don't count the type of this as a kind of "state" otherwise being "stateless" is an unattainable, useless term) and OO (because it participates in polymorphism). – Alexander Mar 08 '24 at 15:30
  • I see some merit to the idea that the type of an object x counts as state, but only from the perspective of some other "parent" object that holds reference to it. The parent might make a polymorphic method call to x, where the type of x is the state that is the deciding factor in how the program progresses. But IMO that state is more logically a part of the parent than than x itself (ignoring the details of memory layout, where it's clearly a part of x). I think most people would call x stateless. – Alexander Mar 08 '24 at 15:32
  • 1
    @DocBrown: A static class is a completely different thing since they can't be instantiated - they are not objects. Final/sealed classes can be instantiated and there is nothing special about those objects - the final/sealed modifiers on the class is just a compile-time check that prevent other classes from inheriting. You can call GetType() on any object and it will return the instance type. So if type info counts as state, then every object have state - sealed or not, having fields or not. I agree with Alexander that this is just not a common or useful definition of object state. – JacquesB Mar 08 '24 at 17:35