3

Let's say I have a List<Apple> object, and each apple has a color.

I implement another object that is constructed with its input the list of apple objects. I might implement functionality on this object like "Get me the number of green apples.", and I can call that without knowing the internal representation of that object.

What would you call this guy? It seems like basic OO, but I'm having trouble thinking of a descriptive name.

7
  • 2
    What is this Object? If it's, say, a basket then Basket would do. If you have a SchoolChild and you have some container for a Collection<SchoolChild> then maybe School. Pick something that describes the role of the Object. Mar 14, 2013 at 18:08
  • AppleSupervisor? Gardener? AppleOverseer? SteveJobs?
    – ghdalum
    Mar 14, 2013 at 18:09
  • Uncle Bob recommends against meaningless terms like "Manager", "Processor" etc - I tend to agree with him. Mar 14, 2013 at 18:12
  • do a google search for "visitor pattern". Implement a visitor.
    – DwB
    Mar 14, 2013 at 18:14
  • 1
    A "bag of utility methods"? I'd make all these methods static and call it a utility class. For the class name itself, it would be either AppleUtilities or Apples (based on standard Arrays and Collections classes in the JDK).
    – Cyrille Ka
    Mar 14, 2013 at 18:34

1 Answer 1

0

Since you are talking about a List<Apple> odds are that you are also talking about a Collection<Apple> (lists inherit collections).

If you want to emphasize the "collection" then I would name the "collection". Apples traditionally were sold in Bushels, but you aren't concerned with measurement, so I would use the more generic (not size dependent) term "basket".

public class Basket {

   private int greenCount;

   public void addApple(Apple apple) {
     if (apple.isGreen()) {
        greenCount++;
     }
   }

   public int getGreenAppleCount() {
      return greenCount;
   }

}

This appropriately captures the responsibility of counting the green apples within the basket.

Utilities, on the other hand, seem to divorce the responsibility of a task from a particular type-bound object. For example, let's look at a hypothetical AppleCounterUtil.

public class AppleCounterUtil {

    public int getCount(Collection<Apple> apples, AppleCondition condition) {
        int count = 0;
        for (Apple apple : apples) {
            if (condition.isSatisfied(apple)) {
               count++;
            }
        }
    }

}

and now we have a nice utility which really doesn't maintain a count, but recalculates the count each time it is needed from possibly different lists of apples.

The key point is that the latter example is not object oriented because there is no conceptual object. The rationale for stating there is no object is because the class containing the utility lacks state. Objects are collections of data and the closely related code. When you have only code, one can make a reasonable argument that you have a name spaced function and not an object.

To see the difference once more, let's compare the two snippets of code. Using our first "Basket" approach:

...
int greenOnes = basket.getGreenAppleCount();
basket.addApple(new GreenApple());
greenOnes = basket.getGreenAppleCount();
...

becomes very clunky, but since we are object oriented, we can easily add a Basket Listener interface to the Basket

public class Basket {

   public void addListener(BasketListner listener) {
     ...
   }

   public void addApple(Apple apple) {
     ...
     for (BasketListnener listner : listeners) {
       listener.appleAdded(this);
     }
   }

}

... some other class ... implements BasketListener {

   int greenOnes = 0;

   ...

   public void appleAdded(Basket basket) {
      greenOnes = basket.getGreenAppleCount();
   }
}

while with the utility oriented technique

...
int greenOnes = AppleCounterUtil.getCount(apples, new GreenCountCondition());
apples.addApple(new GreenApple());
greenOnes = AppleCounterUtil.getCount(apples, new GreenCountCondition());
...

looks similar on the surface, until you attempt to add in more functionality. At the attempt to add in a "apple list listener" you quickly realize that the AppleCounterUtil isn't responsible for maintaining a count, and can't be listened to. Nor is the "list of apples" responsible for maintaining the count, and generalized lists typically present the wrong interfaces for listening.

Unfortunately, when one gets utility oriented, they typically try to solve the problem by adding in more utility methods. This can eventually mean that specific concerns (who manages the group of apples) can get distributed among many utilities without any utility having the sole responsibility of the task. Each utility only provides some "feature" that can be calculated on demand.

Calculate on demand features, in some cases, can become dependent on other calculate on demand features, such that you get a functional coupling between the two (even if there isn't a direct code coupling). This effective coupling means that if you fail to call a certain set of utilities in order, or omit a required utility call, your code breaks. An extreme case is where Objects eventually lose their behavior to a degree that they become data structures, or lack behaviors you would normally associate with the object. It is a object-oriented design anti-pattern known as "anemic objects".

Not the answer you're looking for? Browse other questions tagged or ask your own question.