1

Say that creating/calculating all the properties of fruit needs a lot of logic, and you just want to have a very simple immutable POCO object in the end.

So i.e. it doesn't have any methods, making it really easy to reason about, and also easily and clearly serializable.

Right now I have something names like this:

class BananaCalulator { public Banana Calculate() }
class Banana {}
class AppleCalulator { public Apple Calculate() }
class Apple {}

Is this a common pattern? Is there a more common terminology instead of Calculator?

  • 2
    I'd say Factory ... – Silviu Burcea Apr 4 '15 at 12:30
  • I think we'd need to know a bit more about what these two classes do in order to answer this. For instance, is Apple an interface that AppleCalculator implements? Then it's factory as @SilviuBurcea said. Is AppleCalculator an old/bad API that you're trying to wrap in something more sensible? Then it's probably wrapper or adapter. – Ixrec Apr 4 '15 at 12:34
  • BananaCalculator has a lot of logic that I don't want to have in the Banana result, because after it has been calculated all this code (and extra methods) makes it harder to reason about - they will be an unnecessary distraction. – Dirk Boer Apr 4 '15 at 12:39
  • @SilviuBurcea, thanks for your answer. Wiki says though: "In class-based programming, the factory method pattern is a creational pattern which uses factory methods to deal with the problem of creating objects without specifying the exact class of object that will be created." That's not applicable in this case. I do exactly know what class to create. – Dirk Boer Apr 4 '15 at 12:40
  • 1
    @DirkBoer Are you trying to say that BananaCalculator has some methods that return a Banana? And there is no inheritance or polymorphism going on with either class? If so then I'd agree with amon that Builder is much closer than Factory. – Ixrec Apr 4 '15 at 14:15
4

The Gang of Four knows two patterns which describe objects which do nothing but create other objects.

One is the Factory.

The other is the Builder.

The difference between the two is that a Factory receives all arguments with a single method-call and usually can be reused to create more objects of the same type, a Builder has a number of methods which define the properties of the created object until a build-method is called. The Builder is often (but not necessarily) not reusable afterwards.

BananaFactory factory = new BananaFactory();
Banana b1 = factory.createBanana(100, 200);
Banana b2 = factory.createBanana(110, 210);

BananaBuilder builder = new BananaBuilder();
builder.setLength(100);
builder.setWeight(200),
Banana b3 = builder.build();
  • For the factory, it's often actually a (static) getBananaFactory(). Or in languages supporting free functions, a free function to get the factory object or even a free function being the factory. – Deduplicator Apr 4 '15 at 23:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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